Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hass): use binary sensors when notifications has only two states #396

Merged
merged 19 commits into from
Feb 1, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,33 @@ function getMappedStateTemplate (state, defaultValueKey) {
return `{{ {${map.join(',')}}[value_json.value] | default(${defaultValue}) }}`
}

/**
* Converts an States Array of maps to map
*
* @param {array} states States Array of Maps
*/
function getMapFromArrayOfStates (states) {
varet80 marked this conversation as resolved.
Show resolved Hide resolved
const map = new Map()
for (const listKey in states) {
varet80 marked this conversation as resolved.
Show resolved Hide resolved
map.set(states[listKey].value, states[listKey].text)
}

return map
}
function getBinaryPayloadFromSensor (cfg, devicePayload, off = 0) {
varet80 marked this conversation as resolved.
Show resolved Hide resolved
const stateKeys = Array.from(
getMapFromArrayOfStates(devicePayload.states).keys()
)
varet80 marked this conversation as resolved.
Show resolved Hide resolved
// Set on/off state from keys
if (stateKeys[0] === off) {
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,6 +1637,36 @@ Gateway.prototype.discoverValue = function (node, vId) {
if (cmdClass === CommandClasses.Basic && valueId.property !== 'event') {
return
}

// Try to define Binary sensor
if (valueId.hasOwnProperty('states')) {
varet80 marked this conversation as resolved.
Show resolved Hide resolved
if (valueId.states.length === 2) {
let off = 0 // set default off to 0
cfg = copy(hassCfg.binary_sensor_contact)
switch (valueId.propertyKeyName) {
case 'Motion sensor status':
cfg.discovery_payload.device_class = 'motion'
break
case 'Cover status':
cfg.discovery_payload.device_class = 'opening'
break
case 'Water Alarm':
cfg.discovery_payload.device_class = 'moisture'
break
case 'Access Control':
cfg.discovery_payload.device_class = 'lock'
off = 23
break
case 'Door state':
cfg.discovery_payload.device_class = 'door'
off = 1
break
}
cfg = getBinaryPayloadFromSensor(cfg, valueId, off)
cfg.object_id = valueId.propertyKey
}
break
}
// 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)
Expand All @@ -1618,7 +1675,6 @@ Gateway.prototype.discoverValue = function (node, vId) {
valueId.property,
valueId.propertyKey
)

// TODO: Improve the icons for different propertyKeys!
switch (valueId.propertyKey) {
case 'Motion sensor status':
Expand Down