From 39138317e9d0b04ef9ba9fb1ebdcad282892431a Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Wed, 29 Jan 2020 15:09:33 +0100 Subject: [PATCH] fix: Improved meter and sensorMultilevel hass auto discovery #213 --- hass/configurations.js | 69 ++++++++++++++++++++++++++++++- lib/Constants.js | 92 ++++++++++++++++++++++++++++++++++++++++++ lib/Gateway.js | 72 ++++----------------------------- 3 files changed, 167 insertions(+), 66 deletions(-) diff --git a/hass/configurations.js b/hass/configurations.js index 77a7c8f11d4..c1cffe776e7 100755 --- a/hass/configurations.js +++ b/hass/configurations.js @@ -208,9 +208,9 @@ module.exports = { value_template: '{{ value_json.value }}' } }, - 'sensor_power': { + 'sensor_electricity': { type: 'sensor', - object_id: 'power', + object_id: 'electricity', discovery_payload: { unit_of_measurement: 'Watt', icon: 'mdi:flash', @@ -226,6 +226,71 @@ module.exports = { value_template: '{{ value_json.value }}' } }, + 'sensor_direction': { + type: 'sensor', + object_id: 'direction', + discovery_payload: { + unit_of_measurement: '°', + icon: 'mdi:compass', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_sun': { + type: 'sensor', + object_id: 'sun', + discovery_payload: { + icon: 'mdi:white-balance-sunny', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_weight': { + type: 'sensor', + object_id: 'weight', + discovery_payload: { + icon: 'mdi:weight', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_co2': { + type: 'sensor', + object_id: 'co2', + discovery_payload: { + icon: 'mdi:periodic-table-co2', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_air': { + type: 'sensor', + object_id: 'air', + discovery_payload: { + icon: 'mdi:air-filter', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_sound': { + type: 'sensor', + object_id: 'sound', + discovery_payload: { + icon: 'mdi:volume-high', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_time': { + type: 'sensor', + object_id: 'time', + discovery_payload: { + icon: 'mdi:clock-outline', + value_template: '{{ value_json.value }}' + } + }, + 'sensor_heart': { + type: 'sensor', + object_id: 'heart', + discovery_payload: { + icon: 'mdi:heart', + value_template: '{{ value_json.value }}' + } + }, 'sensor_action': { type: 'sensor', object_id: 'action', diff --git a/lib/Constants.js b/lib/Constants.js index 837c0441f11..702cf6196bd 100644 --- a/lib/Constants.js +++ b/lib/Constants.js @@ -1,4 +1,96 @@ module.exports = { + meterType (index) { // https://github.com/OpenZWave/open-zwave/blob/0d94c9427bbd19e47457578bccc60b16c6679b49/cpp/src/command_classes/Meter.cpp#L74 + var cfg = null + if (index >= 16 && index < 32) { + cfg = 'sensor_gas' + } else if (index < 48) { + cfg = 'sensor_water' + } else { + cfg = 'sensor_electricity' + } + return cfg + }, + sensorType (index) { // https://github.com/OpenZWave/open-zwave/blob/0d94c9427bbd19e47457578bccc60b16c6679b49/config/SensorMultiLevelCCTypes.xml + switch (index) { + case 1: + case 11: + case 23: + case 24: + case 34: + case 62: + case 63: + case 64: + case 65: + case 72: + case 73: + case 74: + case 75: + case 76: + case 77: + case 80: + return 'sensor_temperature' + case 3: + return 'sensor_illuminance' + case 4: + case 15: + case 16: + case 28: + case 29: + return 'sensor_electricity' + case 5: + case 41: + return 'sensor_humidity' + case 6: + case 52: + case 53: + case 54: + case 8: // pressure + case 9: // pressure + return 'sensor_speed' + case 7: + case 21: // angle position + return 'sensor_direction' + case 10: + case 27: // uv index + return 'sensor_sun' + case 12: // rain rate + case 13: // tide level + case 19: // capacity + case 56: + case 57: + return 'sensor_water' + case 14: + case 46: + case 47: + case 48: + case 49: + case 51: + case 66: + case 67: + case 68: + return 'sensor_weight' + case 17: + case 40: // carbon monoxide + case 55: + return 'sensor_co2' + case 18: + return 'sensor_air' + case 22: + return 'sensor_frequency' + case 30: + return 'sensor_sound' + case 33: + return 'sensor_time' + case 44: + case 45: + case 50: + case 60: + case 69: + return 'sensor_heart' + default: + return null + } + }, comandClass (cmd) { switch (cmd) { case 0x00: diff --git a/lib/Gateway.js b/lib/Gateway.js index 3bde049f75f..f42b6e32bcc 100755 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -4,7 +4,7 @@ const reqlib = require('app-root-path').require const EventEmitter = require('events') -const comandClass = reqlib('/lib/Constants.js').comandClass +const Constants = reqlib('/lib/Constants.js') const debug = reqlib('/lib/debug')('Gateway') const inherits = require('util').inherits const hassCfg = reqlib('/hass/configurations.js') @@ -359,6 +359,7 @@ function copy (obj) { * @param {String} units The valueID units * @returns An array with compatible configurations */ +// eslint-disable-next-line no-unused-vars function typeByUnits (units) { var cfg = null @@ -373,7 +374,7 @@ function typeByUnits (units) { } else if (/\b(c|f)\b/gi.test(units)) { cfg = ['sensor_temperature'] } else if (/\b(w|v|watt|volt|kw|kwh|kw\/h)\b/gi.test(units)) { - cfg = ['sensor_power'] + cfg = ['sensor_electricity'] } else if (/\b(bar|pa|g\/m3|mmhg)\b/gi.test(units)) { cfg = ['sensor_humidity'] } @@ -569,7 +570,7 @@ Gateway.prototype.valueTopic = function (node, valueId, returnObject) { switch (this.config.type) { case 1: // named topic.push(node.name ? node.name : NODE_PREFIX + valueId.node_id) - topic.push(comandClass(valueId.class_id)) + topic.push(Constants.comandClass(valueId.class_id)) if (valueId.instance > 1) { topic.push('instance_' + valueId.instance) @@ -755,7 +756,7 @@ Gateway.prototype.discoverValue = function (node, valueId) { const nodeName = getNodeName(node) - var type = comandClass(valueId.class_id) + var type = Constants.comandClass(valueId.class_id) var cfg, payload switch (type) { @@ -848,70 +849,13 @@ Gateway.prototype.discoverValue = function (node, valueId) { // https://github.com/OpenZWave/open-zwave/blob/master/config/Localization.xml#L885 if (type === 'sensor_multilevel') { - let index = valueId.index > 255 ? valueId.index - 255 : valueId.index - switch (index) { - case 1: // air temp - case 23: // water temperature - case 24: // soil temperature - case 72: // return air temperature - case 73: // supply air temperature - case 74: // condenser coil temperature - case 75: // evaporator coil temperature - case 76: // liquid line temperature - case 77: // discharge temperature - case 80: // defrost temperature - cfg = 'sensor_temperature' - break - case 3: // luminance - cfg = 'sensor_illuminance' - break - case 5: // humidity - cfg = 'sensor_humidity' - break - case 15: // voltage - case 4: // power - cfg = 'sensor_power' - break - case 17: // carbon dioxide - case 40: // carbon monoxide - case 55: // smoke density - cfg = 'sensor_gas_density' - break - case 57: // water pressure - case 45: // blood pressure - case 8: // athmosferic pressure - case 9: // barometric pressure - cfg = 'sensor_pressure' - break - } + cfg = Constants.sensorType(valueId.index) } else if (type === 'meter') { // https://github.com/OpenZWave/open-zwave/blob/master/config/Localization.xml#L680 - let index = valueId.index - if (index <= 9 || index === 48 || index === 64) cfg = 'sensor_power' - else { - switch (index) { - case 32: - case 33: - case 34: - cfg = 'sensor_water' - break - case 16: - case 17: - cfg = 'sensor_gas_density' - break - case 19: - case 35: - cfg = 'sensor_pulse' - } - } + cfg = Constants.meterType(valueId.index) } if (valueId.units) { cfg.discovery_payload.unit_of_measurement = valueId.units - let validCfgs = typeByUnits(valueId.units) - - if (validCfgs.length > 0 && validCfgs.indexOf(cfg) < 0) { - cfg = validCfgs[0] - } } cfg = cfg ? copy(hassCfg[cfg]) : copy(hassCfg.sensor_generic) @@ -941,7 +885,7 @@ Gateway.prototype.discoverValue = function (node, valueId) { cfg.discovery_payload.device_class = 'timestamp' break case 'energy_production': - cfg = copy(hassCfg.sensor_power) + cfg = copy(hassCfg.sensor_electricity) break default: return }