From 23f8cb3632140919b298003379724154ea3eaec2 Mon Sep 17 00:00:00 2001 From: V Aretakis Date: Mon, 30 Nov 2020 13:48:09 +0100 Subject: [PATCH] fix: binary sensors, units and undefined labels (#36) --- .prettierignore | 3 ++- lib/Gateway.js | 19 +++++++++++-------- lib/ZwaveClient.js | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.prettierignore b/.prettierignore index e3f74e3ad48..b6822880335 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ CHANGELOG.md +README.md .github/ISSUE_TEMPLATE dist -store \ No newline at end of file +store diff --git a/lib/Gateway.js b/lib/Gateway.js index fed7f149d72..0ef5843a238 100755 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -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') @@ -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) { @@ -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 @@ -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 diff --git a/lib/ZwaveClient.js b/lib/ZwaveClient.js index 54ad503e30d..db869aa110d 100644 --- a/lib/ZwaveClient.js +++ b/lib/ZwaveClient.js @@ -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 }