From 24780e6eac540c77deb6f8768fa1fbba4945ba01 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 7 Apr 2019 18:11:16 +0200 Subject: [PATCH] Home Assistant: publish counter value for click and action payloads. #959 --- lib/extension/deviceReceive.js | 15 +++++++++++++++ test/deviceReceive.test.js | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/extension/deviceReceive.js b/lib/extension/deviceReceive.js index 0a2a669c..20ceeb17 100644 --- a/lib/extension/deviceReceive.js +++ b/lib/extension/deviceReceive.js @@ -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); + } + }); + } } }; diff --git a/test/deviceReceive.test.js b/test/deviceReceive.test.js index 93a8af93..f8cb810d 100644 --- a/test/deviceReceive.test.js +++ b/test/deviceReceive.test.js @@ -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);