From 6d24a49062049d9bafb73aae695a3af57ad949cb Mon Sep 17 00:00:00 2001 From: Stoinov Date: Mon, 18 Mar 2019 19:17:29 +0200 Subject: [PATCH] Expand attribute output. #493 (#1277) * Expand attribute output. #493 If attribute is actually an Object, expand it in the format `topic/key-subkey` in order to keep the same amount of topic levels for predictable parsing. This only expands the first level of contained object. If more expansion are needed a more robust method should be implemented rather than just nesting `if`s. * Fixing whitespace fixing https://travis-ci.org/Koenkk/zigbee2mqtt/builds/507616980 * first time splitting line... obviously Not sure what the proper approach is, but I guess we can put all params on new line. * identation... really... How is that `eslint`? any other requests? --- lib/controller.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/controller.js b/lib/controller.js index ba5eceeb6..411e27cae 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -245,7 +245,14 @@ class Controller { this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options); } else if (settings.get().experimental.output === 'attribute') { Object.keys(messagePayload).forEach((key) => { - this.mqtt.publish(`${entity.friendlyName}/${key}`, JSON.stringify(messagePayload[key]), options); + if (typeof messagePayload[key] == 'object') { + Object.keys(messagePayload[key]).forEach((subKey) => { + this.mqtt.publish(`${entity.friendlyName}/${key}-${subKey}`, + JSON.stringify(messagePayload[key][subKey]), options); + }); + } else { + this.mqtt.publish(`${entity.friendlyName}/${key}`, JSON.stringify(messagePayload[key]), options); + } }); } }