Skip to content

Commit

Permalink
feat(hass): translate Notification CC values to string (#105)
Browse files Browse the repository at this point in the history
* add notification part 1

* add translation from states to value_template

* support both number and text

* add motion sensor icon

* add new icon for motion sensor

* add TODO for icons. Add logic on mapped default value

* patch default value, to show value_json.value when uknown mapping entry is posted

* remove extra bracket

* fix linting issues

Co-authored-by: V. Aretakis <[email protected]>
  • Loading branch information
varet80 and aretakisv authored Dec 23, 2020
1 parent f1bf0b4 commit 0bc3d5e
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,33 @@ function getMappedValuesTemplate (modeMap, defaultValue) {
)}}[value_json.value] | default('${defaultValue}') }}`
}

/**
* Calculate the correct template string to use for templates with state
* list based on gateway settings and mapped mode values
*
* @param {Object} state The object list which is translated to map
* @param {String} defaultValueKey The key to use for default value
* @returns {String} The template to use for the template
*/
function getMappedStateTemplate (state, defaultValueKey) {
const map = []
let defaultValue = 'value_json.value'
for (const listKey in state) {
map.push(
`${
typeof state[listKey].value === 'number'
? state[listKey].value
: '"' + state[listKey].value + '"'
}: "${state[listKey].text}"`
)
if (state[listKey].value === defaultValueKey) {
defaultValue = `'${state[listKey].text}'`
}
}

return `{{ {${map.join(',')}}[value_json.value] | default(${defaultValue}) }}`
}

/**
* Retrives the value of a property from the node valueId
*
Expand Down Expand Up @@ -1488,7 +1515,21 @@ Gateway.prototype.discoverValue = function (node, vId) {
} else {
cfg.object_id = 'notification_' + valueId.property
}
cfg.discovery_payload.icon = 'mdi:alarm-light'

// 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'
}
if (valueId.list) {
cfg.discovery_payload.value_template = getMappedStateTemplate(
valueId.states,
valueId.default
)
}
break
case CommandClasses['Multilevel Sensor']:
case CommandClasses.Meter:
Expand Down

0 comments on commit 0bc3d5e

Please sign in to comment.