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?
This commit is contained in:
Stoinov authored and Koen Kanters committed 2019-03-18 18:17:29 +01:00
1 parent bee722c9c4
commit 6d24a49062
1 file changed
+8 -1
+8 -1
View File
@@ -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);
}
});
}
}