From a845d35b8ef9a4363930bb1ce825dd03bea21523 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Thu, 16 Jul 2020 23:16:59 +0200 Subject: [PATCH] Update Birth & Will Topic for Home Assistant #3918 --- lib/extension/homeassistant.js | 5 ++++- test/homeassistant.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/extension/homeassistant.js b/lib/extension/homeassistant.js index b4605bcb8..9fb545dc2 100644 --- a/lib/extension/homeassistant.js +++ b/lib/extension/homeassistant.js @@ -1784,6 +1784,8 @@ const mapping = { 'TYZS1L': [cfg.light_brightness_colortemp_colorxy], }; +const defaultStatusTopic = 'homeassistant/status'; + /** * This extensions handles integration with HomeAssistant */ @@ -1940,6 +1942,7 @@ class HomeAssistant extends Extension { async onMQTTConnected() { this.mqtt.subscribe(this.statusTopic); + this.mqtt.subscribe(defaultStatusTopic); // MQTT discovery of all paired devices on startup. for (const device of this.zigbee.getClients()) { @@ -2132,7 +2135,7 @@ class HomeAssistant extends Extension { } onMQTTMessage(topic, message) { - if (topic !== this.statusTopic) { + if (topic !== this.statusTopic && topic !== defaultStatusTopic) { return false; } diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 95174b4d0..722f66206 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -573,12 +573,39 @@ describe('HomeAssistant extension', () => { expect(MQTT.publish).toHaveBeenCalledTimes(1); }); + it('Should send all status when home assistant comes online (default topic)', async () => { + jest.useFakeTimers(); + data.writeDefaultState(); + controller = new Controller(); + await controller.start(); + expect(MQTT.subscribe).toHaveBeenCalledWith('homeassistant/status'); + await flushPromises(); + MQTT.publish.mockClear(); + await MQTT.events.message('homeassistant/status', 'online'); + await flushPromises(); + jest.runOnlyPendingTimers(); + await flushPromises(); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bulb', + '{"state":"ON","brightness":50,"color_temp":370,"linkquality":99,"update_available":false}', + { retain: true, qos: 0 }, + expect.any(Function) + ); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/remote', + '{"brightness":255,"update_available":false}', + { retain: true, qos: 0 }, + expect.any(Function) + ); + }); + it('Should send all status when home assistant comes online', async () => { jest.useFakeTimers(); data.writeDefaultState(); controller = new Controller(); await controller.start(); await flushPromises(); + expect(MQTT.subscribe).toHaveBeenCalledWith('hass/status'); MQTT.publish.mockClear(); await MQTT.events.message('hass/status', 'online'); await flushPromises();