From 6427da244f2c58a1a55a27c0a2b0d645f1a46b2f Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 4 Mar 2019 18:13:36 +0100 Subject: [PATCH] Implement attribute output. https://github.com/Koenkk/zigbee2mqtt/issues/493 --- lib/controller.js | 8 +++++++- lib/extension/homeassistant.js | 4 ++++ lib/util/settings.js | 2 ++ test/controller.test.js | 37 ++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/controller.js b/lib/controller.js index 7e7eb9c7a..3568f409e 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -259,7 +259,13 @@ class Controller { messagePayload.device = this.getDeviceInfoForMqtt(entityID); } - this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options); + if (settings.get().experimental.output === 'json') { + 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); + }); + } } getDeviceInfoForMqtt(ieeeAddr) { diff --git a/lib/extension/homeassistant.js b/lib/extension/homeassistant.js index 9baa72327..8423a5264 100644 --- a/lib/extension/homeassistant.js +++ b/lib/extension/homeassistant.js @@ -578,6 +578,10 @@ class HomeAssistant { if (!settings.get().advanced.cache_state) { logger.warn('In order for HomeAssistant integration to work properly set `cache_state: true'); } + + if (settings.get().experimental.output === 'attribute') { + throw new Error('Home Assitant integration is not possible with attribute output!'); + } } onMQTTConnected() { diff --git a/lib/util/settings.js b/lib/util/settings.js index 4827c1d06..ac23c66f7 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -16,6 +16,8 @@ const defaults = { device_options: {}, experimental: { livolo: false, + // json or attribute + output: 'json', }, advanced: { log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'), diff --git a/test/controller.test.js b/test/controller.test.js index c5f2f1273..1de3b9264 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -52,6 +52,9 @@ describe('Controller', () => { advanced: { cache_state: false, }, + experimental: { + output: 'json', + }, }; }); @@ -65,5 +68,39 @@ describe('Controller', () => { `"manufName":"IKEA","modelId":"TRADFRI bulb E27 CWS opal 600lm"}}` ); }); + + it('Should output to json by default', () => { + const payload = {temperature: 1, humidity: 2}; + controller.publishEntityState('0x12345678', payload); + chai.assert.isTrue(mqttPublish.calledOnce); + chai.assert.deepEqual( + JSON.parse(mqttPublish.getCall(0).args[1]), + payload + ); + }); + + it('Should output to attribute', () => { + sandbox.stub(settings, 'get').callsFake(() => { + return { + mqtt: { + include_device_information: false, + }, + advanced: { + cache_state: false, + }, + experimental: { + output: 'attribute', + }, + }; + }); + + const payload = {temperature: 1, humidity: 2}; + controller.publishEntityState('0x12345678', payload); + chai.assert.isTrue(mqttPublish.calledTwice); + chai.assert.deepEqual(mqttPublish.getCall(0).args[0], 'test/temperature'); + chai.assert.deepEqual(mqttPublish.getCall(0).args[1], '1'); + chai.assert.deepEqual(mqttPublish.getCall(1).args[0], 'test/humidity'); + chai.assert.deepEqual(mqttPublish.getCall(1).args[1], '2'); + }); }); });