Skip to content

Commit

Permalink
Unify exposed actions for HA
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Oct 21, 2024
1 parent ea878b0 commit c8043ba
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ interface Discovered {
discovered: boolean;
}

interface ActionData {
action: string;
button?: string;
scene?: string;
}

const ACTION_BUTTON_PATTERN: string = '^(?<button>[a-z]+)_(?<action>(?:press|hold)(?:_release)?)$';
const ACTION_SCENE_PATTERN: string = '^(?<action>recall|scene)_(?<scene>[0-2][0-9]{0,2})$';

const SENSOR_CLICK: Readonly<DiscoveryEntry> = {
type: 'sensor',
object_id: 'click',
Expand Down Expand Up @@ -1153,9 +1162,20 @@ export default class HomeAssistant extends Extension {
discovery_payload: {
name: endpoint ? /* istanbul ignore next */ `${firstExpose.label} ${endpoint}` : firstExpose.label,
state_topic: true,
state_topic_postfix: 'action',
event_types: firstExpose.values.map((v) => v.toString()).filter((v) => !v.includes('*')),
value_template: `{ "event_type": "{{value}}" }`,
event_types: this.prepareActionEventTypes(firstExpose.values),

// TODO: Implement parsing for all event types.
value_template:
`{%- set buttons = value_json.action|regex_findall_index(${ACTION_BUTTON_PATTERN.replaceAll(/\?<([a-z]+)>/g, '?P<$1>')}) -%}` +
`{%- set scenes = value_json.action|regex_findall_index(${ACTION_SCENE_PATTERN.replaceAll(/\?<([a-z]+)>/g, '?P<$1>')}) -%}` +
`{%- if buttons -%}\n` +
` {%- set d = dict(event_type = "{{buttons[1]}}", button = "{{buttons[0]}}_button" -%}\n` +
`{%- elif scenes -%}\n` +
` {%- set d = dict(event_type = "{{scenes[0]}}", scene = "{{scenes[1]}}" -%}\n` +
`{%- else -%}\n` +
` {%- set d = dict(event_type = "{{value_json.action}}" ) -%}\n` +
`{%- endif -%}\n` +
`{{d|to_json}}`,
...ENUM_DISCOVERY_LOOKUP[firstExpose.name],
},
});
Expand Down Expand Up @@ -2185,4 +2205,28 @@ export default class HomeAssistant extends Extension {

return bridge;
}

private parseActionValue(action: string): ActionData {
const buttons = action.match(ACTION_BUTTON_PATTERN);
if (buttons && buttons.groups && buttons.groups.action) {
return {...buttons.groups, action: buttons.groups.action};
}

const scenes = action.match(ACTION_SCENE_PATTERN);
if (scenes && scenes.groups && scenes.groups.action) {
return {...scenes.groups, action: scenes.groups.action};
}

const wildcard = action.match('^(?<action>recall|scene)_*$');
if (wildcard && wildcard.groups && wildcard.groups.action) {
logger.debug('Found wildcard action ' + wildcard.groups.action);
return {action: wildcard.groups.action, scene: 'wildcard'};
}

return {action};
}

private prepareActionEventTypes(values: zhc.Enum['values']): string[] {
return utils.arrayUnique(values.map((v) => this.parseActionValue(v.toString()).action).filter((v) => !v.includes('*')));
}
}

0 comments on commit c8043ba

Please sign in to comment.