mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-07-12 06:38:53 +00:00
Home Assistant: expose binary with SET access as switch (#7760)
* Add support for binary attributes with SET access - discover as switch in HA * Code linting * Update npm-shrinkwrap.json * Update homeassistant.js Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
@@ -16,7 +16,8 @@ const sensorClick = {
|
||||
},
|
||||
};
|
||||
|
||||
const ACCESS_STATE = 1;
|
||||
const ACCESS_STATE = 0b001;
|
||||
const ACCESS_SET = 0b010;
|
||||
const defaultStatusTopic = 'homeassistant/status';
|
||||
|
||||
/**
|
||||
@@ -343,16 +344,40 @@ class HomeAssistant extends Extension {
|
||||
presence: {device_class: 'presence'},
|
||||
};
|
||||
|
||||
discoveryEntry = {
|
||||
type: 'binary_sensor',
|
||||
object_id: expose.endpoint ? `${expose.name}_${expose.endpoint}` : `${expose.name}`,
|
||||
discovery_payload: {
|
||||
value_template: `{{ value_json.${expose.property} }}`,
|
||||
payload_on: expose.value_on,
|
||||
payload_off: expose.value_off,
|
||||
...(lookup[expose.name] || {}),
|
||||
},
|
||||
};
|
||||
/**
|
||||
* If Z2M binary attribute has SET access then expose it as `switch` in HA
|
||||
* There is also a check on the values for typeof boolean to prevent invalid values and commands
|
||||
* silently failing - commands work fine but some devices won't reject unexpected values.
|
||||
* https://github.com/Koenkk/zigbee2mqtt/issues/7740
|
||||
*/
|
||||
if (expose.access & ACCESS_SET) {
|
||||
discoveryEntry = {
|
||||
type: 'switch',
|
||||
object_id: expose.endpoint ?
|
||||
`switch_${expose.name}_${expose.endpoint}` :
|
||||
`switch_${expose.name}`,
|
||||
discovery_payload: {
|
||||
value_template: `{{ value_json.${expose.property} }}`,
|
||||
payload_on: typeof expose.value_on == 'boolean' ? 'true' : expose.value_on,
|
||||
payload_off: typeof expose.value_off == 'boolean' ? 'false' : expose.value_off,
|
||||
command_topic: true,
|
||||
command_topic_prefix: expose.endpoint ? expose.endpoint : undefined,
|
||||
command_topic_postfix: expose.property,
|
||||
...(lookup[expose.name] || {}),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
discoveryEntry = {
|
||||
type: 'binary_sensor',
|
||||
object_id: expose.endpoint ? `${expose.name}_${expose.endpoint}` : `${expose.name}`,
|
||||
discovery_payload: {
|
||||
value_template: `{{ value_json.${expose.property} }}`,
|
||||
payload_on: expose.value_on,
|
||||
payload_off: expose.value_off,
|
||||
...(lookup[expose.name] || {}),
|
||||
},
|
||||
};
|
||||
}
|
||||
} else if (expose.type === 'numeric') {
|
||||
const lookup = {
|
||||
battery: {device_class: 'battery', state_class: 'measurement'},
|
||||
|
||||
Reference in New Issue
Block a user