Skip to content

Commit

Permalink
Fix nested json parsing (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekateluv authored Jan 17, 2024
1 parent 8aa99ee commit 7dbf43f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.eslintrc
.prettierrc
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-zigbee2mqtt (1.3.3) stable; urgency=medium

* Fix update and state controls parsing (nested json objects)

-- Ekaterina Volkova <ekaterina.volkova@wirenboard.ru> 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
Expand Down
65 changes: 36 additions & 29 deletions wb-zigbee2mqtt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var base_topic = 'zigbee2mqtt';
var topicType = JSON.stringify({
var controlsTypes = {
battery: 'value',
linkquality: 'value',
temperature: 'temperature',
Expand All @@ -13,7 +13,7 @@ var topicType = JSON.stringify({
occupancy_level: 'value',
power: 'power',
voltage: 'voltage',
});
};

defineVirtualDevice('zigbee2mqtt', {
title: 'Zigbee2mqtt',
Expand Down Expand Up @@ -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
);
}
}
});
}

0 comments on commit 7dbf43f

Please sign in to comment.