Skip to content

Commit

Permalink
fix(discovery): define units for scene and configuration values
Browse files Browse the repository at this point in the history
and for scene values, use the correct template because the value is
a JSON object that also includes the units, but nests the actual
value one level deeper
  • Loading branch information
ccutrer committed Sep 19, 2024
1 parent 572035a commit 649e117
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions api/lib/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ export default class Gateway {
node,
node.values[v],
) as string,
)
)

Check failure on line 913 in api/lib/Gateway.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Replace `··` with `↹`
: null
}

Expand Down Expand Up @@ -1336,6 +1336,10 @@ export default class Gateway {
valueId.property,
valueId.propertyKey,
)
if (valueId.value?.unit) {
cfg.discovery_payload.value_template =
"{{ value_json.value.value | default('') }}"
}
break
case CommandClasses['Binary Sensor']: {
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/commandclass/BinarySensorCC.ts#L41
Expand Down Expand Up @@ -1647,9 +1651,25 @@ export default class Gateway {
sensor.objectId,
)

let unit = null
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/config/config/scales.json
if (valueId.unit) {
cfg.discovery_payload.unit_of_measurement = valueId.unit
unit = valueId.unit
} else if (valueId.value?.unit) {
unit = valueId.value.unit
}

if (unit) {
// Home Assistant requires time units to be abbreviated
// https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L408
if (unit === 'seconds') {
unit = 's'
} else if (unit === 'minutes') {
unit = 'min'
} else if (unit === 'hours') {
unit = 'h'
}
cfg.discovery_payload.unit_of_measurement = unit
}

Object.assign(cfg.discovery_payload, sensor.props || {})
Expand Down Expand Up @@ -1708,6 +1728,7 @@ export default class Gateway {
if (valueId.max !== 100) {
cfg.discovery_payload.max = valueId.max
}

Check failure on line 1731 in api/lib/Gateway.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Delete `↹↹↹↹↹↹↹`
break
default:
return
Expand Down

0 comments on commit 649e117

Please sign in to comment.