This commit is contained in:
Koen Kanters
2019-03-04 18:13:36 +01:00
parent 339816c160
commit 6427da244f
4 changed files with 50 additions and 1 deletions
+7 -1
View File
@@ -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) {
+4
View File
@@ -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() {
+2
View File
@@ -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%'),
+37
View File
@@ -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');
});
});
});