mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-28 13:34:24 +00:00
Implement attribute output. https://github.com/Koenkk/zigbee2mqtt/issues/493
This commit is contained in:
+7
-1
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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%'),
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user