Skip to content

Commit

Permalink
fix: binary sensors, units and undefined labels (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
varet80 authored Nov 30, 2020
1 parent ac4ed9a commit 23f8cb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CHANGELOG.md
README.md
.github/ISSUE_TEMPLATE
dist
store
store
19 changes: 11 additions & 8 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require('path')
const reqlib = require('app-root-path').require
const utils = reqlib('/lib/utils.js')
const EventEmitter = require('events')
const { AlarmSensorType, BinarySensorType } = require('zwave-js')
const { AlarmSensorType } = require('zwave-js')
const { CommandClasses } = require('@zwave-js/core')
const Constants = reqlib('/lib/Constants.js')
const debug = reqlib('/lib/debug')('Gateway')
Expand Down Expand Up @@ -1265,17 +1265,19 @@ Gateway.prototype.discoverValue = function (node, vId) {
case CommandClasses['Binary Sensor']:
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/commandclass/BinarySensorCC.ts#L41

let sensorTypeName = BinarySensorType[valueId.property]
// change the sensorTypeName to use directly valueId.property, as the old way was returning a number
// add a comment which shows the old way of achieving this value. This change fixes the Binary Sensor
// discovery
let sensorTypeName = valueId.property

if (sensorTypeName) {
sensorTypeName = this.mqtt.cleanName(
sensorTypeName.toLocaleLowerCase()
)
}

// TODO: Implement all BinarySensorTypes
cfg = hassCfg['binary_sensor_' + sensorTypeName]

// if cannot discover anything, assume contact type
cfg = cfg ? copy(cfg) : copy(hassCfg.binary_sensor_contact)

if (valueConf) {
Expand Down Expand Up @@ -1361,7 +1363,8 @@ Gateway.prototype.discoverValue = function (node, vId) {
sensor: 'battery',
objectId: 'level',
props: {
device_class: 'battery'
device_class: 'battery',
unit_of_measurement: '%' // this is set if Driver doesn't offer unit of measurement
}
}
} else return
Expand All @@ -1373,9 +1376,9 @@ Gateway.prototype.discoverValue = function (node, vId) {

Object.assign(cfg.discovery_payload, sensor.props || {})

if (valueId.units) {
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/config/config/scales.json
cfg.discovery_payload.unit_of_measurement = valueId.units
// 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
}

// check if there is a custom value configuration for this valueID
Expand Down
2 changes: 1 addition & 1 deletion lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ function updateValueMetadata (zwaveNode, zwaveValue, zwaveValueMeta) {
readable: zwaveValueMeta.readable,
writeable: zwaveValueMeta.writeable,
description: zwaveValueMeta.description,
label: zwaveValueMeta.label,
label: zwaveValueMeta.label || (zwaveValue.propertyName + ' (property)'), // when label is missing, re use propertyName. Usefull for webinterface
default: zwaveValueMeta.default
}

Expand Down

0 comments on commit 23f8cb3

Please sign in to comment.