From 7dbf43fda45e12694b5ab7586feb5080cd1b7725 Mon Sep 17 00:00:00 2001 From: Ekaterina Volkova <97599621+ekateluv@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:23:10 +0300 Subject: [PATCH] Fix nested json parsing (#11) --- .gitignore | 2 ++ debian/changelog | 6 +++++ wb-zigbee2mqtt.js | 65 ++++++++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccb5a4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.eslintrc +.prettierrc \ No newline at end of file diff --git a/debian/changelog b/debian/changelog index 9533e8e..9d5dc06 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +wb-zigbee2mqtt (1.3.3) stable; urgency=medium + + * Fix update and state controls parsing (nested json objects) + + -- Ekaterina Volkova Tue, 09 Jan 2024 18:03:13 +0300 + wb-zigbee2mqtt (1.3.2) stable; urgency=medium * Add homepage to deb package info. No functional changes diff --git a/wb-zigbee2mqtt.js b/wb-zigbee2mqtt.js index b5d6b42..7f8565f 100644 --- a/wb-zigbee2mqtt.js +++ b/wb-zigbee2mqtt.js @@ -1,5 +1,5 @@ var base_topic = 'zigbee2mqtt'; -var topicType = JSON.stringify({ +var controlsTypes = { battery: 'value', linkquality: 'value', temperature: 'temperature', @@ -13,7 +13,7 @@ var topicType = JSON.stringify({ occupancy_level: 'value', power: 'power', voltage: 'voltage', -}); +}; defineVirtualDevice('zigbee2mqtt', { title: 'Zigbee2mqtt', @@ -137,33 +137,40 @@ defineRule('Permit join', { }); })(); -function initTracker(ctrlName) { - trackMqtt(base_topic + '/' + ctrlName, function (obj) { - JSON.parse(obj.value, function (k, v) { - if (k != '') { - var obj = JSON.parse(topicType); - var ks = Object.keys(obj); - var resultIndex = ks.indexOf(k, 0); - if (resultIndex >= 0) { - if (!getDevice(ctrlName).isControlExists(k)) { - getDevice(ctrlName).addControl(k, { - type: obj[ks[resultIndex]], - value: v, - readonly: true, - }); - } - dev[ctrlName][k] = v; - } else { - if (!getDevice(ctrlName).isControlExists(k)) { - getDevice(ctrlName).addControl(k, { - type: 'text', - value: v, - readonly: true, - }); - } - dev[ctrlName][k] = v != null ? v.toString() : ''; - } +function getControlType(controlName, controlsTypes) { + return controlName in controlsTypes ? controlsTypes[controlName] : 'text'; +} + +function getContolValue(contolName, controlValue, controlsTypes) { + if (contolName in controlsTypes) return controlValue; + if (controlValue == null) return ''; + if (typeof controlValue === 'object') { + return JSON.stringify(controlValue); + } + return controlValue.toString(); +} + +function initTracker(deviceName) { + trackMqtt(base_topic + '/' + deviceName, function (obj) { + var device = JSON.parse(obj.value); + for (var controlName in device) { + if (controlName == '') { + continue; } - }); + + if (!getDevice(deviceName).isControlExists(controlName)) { + getDevice(deviceName).addControl(controlName, { + type: getControlType(controlName, controlsTypes), + value: getContolValue(controlName, device[controlName], controlsTypes), + readonly: true, + }); + } else { + dev[deviceName][controlName] = getContolValue( + controlName, + device[controlName], + controlsTypes + ); + } + } }); }