Home Assistant: publish counter value for click and action payloads. #959

This commit is contained in:
Koen Kanters
2019-04-07 18:11:16 +02:00
parent 2554cf8e8b
commit 24780e6eac
2 changed files with 25 additions and 0 deletions
+15
View File
@@ -183,6 +183,21 @@ class DeviceReceive {
this.publishDebounce(device.ieeeAddr, payload, settingsDevice.debounce);
} else {
this.publishEntityState(device.ieeeAddr, payload);
if (settings.get().homeassistant) {
/**
* Publish an empty value for click and action payload, in this way Home Assistant
* can use Home Assistant entities in automations.
* https://github.com/Koenkk/zigbee2mqtt/issues/959#issuecomment-480341347
*/
Object.keys(payload).forEach((key) => {
if (['action', 'click'].includes(key)) {
const counterPayload = {};
counterPayload[key] = '';
this.publishEntityState(device.ieeeAddr, counterPayload);
}
});
}
}
};
+10
View File
@@ -40,6 +40,16 @@ describe('DeviceReceive', () => {
expect(publishEntityState.mock.calls[0][1]).toStrictEqual({click: 'single'});
});
it('Should handle a zigbee message and counter it when Home Assistant integration is enabled', () => {
jest.spyOn(settings, 'get').mockReturnValue({homeassistant: true, advanced: {last_seen: 'disabled'}});
const device = {ieeeAddr: '0x12345678'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
deviceReceive.onZigbeeMessage(message, device, WXKG11LM);
expect(publishEntityState).toHaveBeenCalledTimes(2);
expect(publishEntityState.mock.calls[0][1]).toStrictEqual({click: 'single'});
expect(publishEntityState.mock.calls[1][1]).toStrictEqual({click: ''});
});
it('Should handle a zigbee message which uses ep (left)', () => {
const device = {ieeeAddr: '0x12345678'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);