Skip to content

Commit

Permalink
feat(hass): use binary sensors when notifications has only two states (
Browse files Browse the repository at this point in the history
…#396)

* Add binary_sensor from notification CC

* fix more bits and pieces

* more corrections

* more small changes

* add two extra properties

* Make flow better

* fixes flow, which broke non binary not. sensors

* Add changes and documentation on the state

* Apply suggested improvements

* remove un-needed function

* revert for...in from for..of as we use the index to resolve

* Update lib/Gateway.js

Co-authored-by: Daniel Lando <[email protected]>

* changes on comments

* Add Water Alarm under sensor status

Co-authored-by: Daniel Lando <[email protected]>
Co-authored-by: V. Aretakis <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2021
1 parent 0cb5d5c commit 557ec80
Showing 1 changed file with 95 additions and 20 deletions.
115 changes: 95 additions & 20 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,25 @@ function getMappedStateTemplate (state, defaultValueKey) {
return `{{ {${map.join(',')}}[value_json.value] | default(${defaultValue}) }}`
}

/**
* Generates payload for Binary use from a state object
*
* @param {import('../types').HassDevice} cfg Hass discovery Configuration
* @param {import('../types').Z2MValueId} valueId The device ValueID
* @param {number} offStateValue The value number to consider off state
*/
function setBinaryPayloadFromSensor (cfg, valueId, offStateValue = 0) {
const stateKeys = valueId.states.map(s => s.value)
// Set on/off state from keys
if (stateKeys[0] === offStateValue) {
cfg.discovery_payload.payload_off = stateKeys[0]
cfg.discovery_payload.payload_on = stateKeys[1]
} else {
cfg.discovery_payload.payload_off = stateKeys[1]
cfg.discovery_payload.payload_on = stateKeys[0]
}
return cfg
}
/**
* Retrives the value of a property from the node valueId
*
Expand Down Expand Up @@ -1610,28 +1629,84 @@ Gateway.prototype.discoverValue = function (node, vId) {
if (cmdClass === CommandClasses.Basic && valueId.property !== 'event') {
return
}
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/commandclass/NotificationCC.ts
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/config/config/notifications.json
cfg = copy(hassCfg.sensor_generic)
cfg.object_id = utils.joinProps(
'notification',
valueId.property,
valueId.propertyKey
)

// TODO: Improve the icons for different propertyKeys!
switch (valueId.propertyKey) {
case 'Motion sensor status':
cfg.discovery_payload.icon = 'mdi:motion-sensor'
break
default:
cfg.discovery_payload.icon = 'mdi:alarm-light'
}
// Try to define Binary sensor
if (valueId.list) {
cfg.discovery_payload.value_template = getMappedStateTemplate(
valueId.states,
valueId.default
)
if (valueId.states.length === 2) {
let off = 0 // set default off to 0.
cfg = copy(hassCfg.binary_sensor_contact)
cfg.object_id = valueId.propertyKey
switch (valueId.propertyKeyName) {
case 'Access Control':
cfg.discovery_payload.device_class = 'lock'
off = 23 // Closed state
break
case 'Cover status':
cfg.discovery_payload.device_class = 'opening'
break
case 'Door state':
cfg.discovery_payload.device_class = 'door'
off = 1 // Door closed on payload 1
break
case 'Dust in device status':
case 'Over-current status':
case 'Over-load status':
case 'Hardware status':
cfg.discovery_payload.device_class = 'problem'
break
case 'Heat sensor status':
cfg.discovery_payload.device_class = 'heat'
break
case 'Motion sensor status':
cfg.discovery_payload.device_class = 'motion'
break
case 'Alarm status':
cfg.discovery_payload.device_class = 'connectivity'
break
// sensor status has multiple Properties. therefore is good to work
// on property basis... user friendly
case 'Sensor status':
switch (valueId.propertyName) {
case 'Smoke Alarm':
cfg.discovery_payload.device_class = 'smoke'
break
case 'Water Alarm':
cfg.discovery_payload.device_class = 'moisture'
break
default:
cfg.discovery_payload.device_class = 'None'
}
cfg.object_id = valueId.propertyName
break
case 'Water Alarm':
cfg.discovery_payload.device_class = 'moisture'
break
default:
cfg.discovery_payload.device_class = 'None'
}
cfg = setBinaryPayloadFromSensor(cfg, valueId, off)
} else {
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/commandclass/NotificationCC.ts
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/config/config/notifications.json
cfg = copy(hassCfg.sensor_generic)
cfg.object_id = utils.joinProps(
'notification',
valueId.property,
valueId.propertyKey
)
// TODO: Improve the icons for different propertyKeys!
switch (valueId.propertyKey) {
case 'Motion sensor status':
cfg.discovery_payload.icon = 'mdi:motion-sensor'
break
default:
cfg.discovery_payload.icon = 'mdi:alarm-light'
}
cfg.discovery_payload.value_template = getMappedStateTemplate(
valueId.states,
valueId.default
)
}
}
break
case CommandClasses['Multilevel Sensor']:
Expand Down

0 comments on commit 557ec80

Please sign in to comment.