From a7e02a7be847418df463095b68951c1a1459ca83 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Thu, 8 Jun 2023 09:04:14 +0200 Subject: [PATCH] fix(ignore): Better fix for #17891 (#17951) * fix(ignore): Better fix for #17891 * remove onlythis --- lib/mqtt.ts | 7 +++---- test/controller.test.js | 7 ++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/mqtt.ts b/lib/mqtt.ts index 4ca606bb4..73a7811af 100644 --- a/lib/mqtt.ts +++ b/lib/mqtt.ts @@ -101,10 +101,9 @@ export default class MQTT { }, utils.seconds(10)); logger.info('Connected to MQTT server'); + await this.publishStateOnline(); - if (this.initialConnect) { - await this.publishStateOnline(); - } else { + if (!this.initialConnect) { this.republishRetainedTimer = setTimeout(() => { // Republish retained messages in case MQTT broker does not persist them. // https://github.com/Koenkk/zigbee2mqtt/issues/9629 @@ -141,7 +140,7 @@ export default class MQTT { this.eventBus.emitMQTTMessage({topic, message: message + ''}); } - if (this.republishRetainedTimer && topic == `${settings.get().mqtt.base_topic}/bridge/state`) { + if (this.republishRetainedTimer && topic === `${settings.get().mqtt.base_topic}/bridge/info`) { clearTimeout(this.republishRetainedTimer); this.republishRetainedTimer = null; } diff --git a/test/controller.test.js b/test/controller.test.js index d616eff2c..43a72a868 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -651,7 +651,7 @@ describe('Controller', () => { MQTT.events['connect'](); await flushPromises(); jest.runOnlyPendingTimers(); - expect(MQTT.publish).toHaveBeenCalledTimes(12); + expect(MQTT.publish).toHaveBeenCalledTimes(13); expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/info', expect.any(String), { retain: true, qos: 0 }, expect.any(Function)); }); @@ -660,9 +660,10 @@ describe('Controller', () => { MQTT.publish.mockClear(); MQTT.events['connect'](); await flushPromises(); - await MQTT.events.message('zigbee2mqtt/bridge/state', 'online'); + await MQTT.events.message('zigbee2mqtt/bridge/info', 'dummy'); jest.runOnlyPendingTimers(); - expect(MQTT.publish).toHaveBeenCalledTimes(0); + expect(MQTT.publish).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/state', expect.any(String), { retain: true, qos: 0 }, expect.any(Function)); }); it('Should prevent any message being published with retain flag when force_disable_retain is set', async () => {