Skip to content

Commit

Permalink
feat: Support settable Home Assistant text (#18114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk authored Jun 25, 2023
1 parent ea193f1 commit 9474452
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,25 +885,41 @@ export default class HomeAssistant extends Extension {
});
}
} else if (firstExpose.type === 'text' || firstExpose.type === 'composite' || firstExpose.type === 'list') {
// Deprecated: remove text sensor
const settableText = firstExpose.type === 'text' && firstExpose.access & ACCESS_SET;
const lookup: {[s: string]: KeyValue} = {
action: {icon: 'mdi:gesture-double-tap'},
programming_mode: {icon: 'mdi:calendar-clock'},
program: {value_template: `{{ value_json.${firstExpose.property}|default("") ` +
`| truncate(254, True, '', 0) }}`},
};
if (firstExpose.access & ACCESS_STATE) {
const lookup: {[s: string]: KeyValue} = {
action: {icon: 'mdi:gesture-double-tap'},
programming_mode: {icon: 'mdi:calendar-clock'},
program: {value_template: `{{ value_json.${firstExpose.property}|default("") ` +
`| truncate(254, True, '', 0) }}`},
};

const discoveryEntry: DiscoveryEntry = {
type: 'sensor',
object_id: firstExpose.property,
mockProperties: [{property: firstExpose.property, value: null}],
discovery_payload: {
value_template: `{{ value_json.${firstExpose.property} }}`,
enabled_by_default: !settableText,
...lookup[firstExpose.name],
},
};
discoveryEntries.push(discoveryEntry);
}
if (settableText) {
discoveryEntries.push({
type: 'text',
object_id: firstExpose.property,
mockProperties: [], // Already mocked above in case access STATE is supported
discovery_payload: {
value_template: `{{ value_json.${firstExpose.property} }}`,
command_topic_prefix: endpoint,
command_topic: true,
command_topic_postfix: firstExpose.property,
...lookup[firstExpose.name],
},
});
}
} else {
throw new Error(`Unsupported exposes type: '${firstExpose.type}'`);
}
Expand Down

0 comments on commit 9474452

Please sign in to comment.