From c28731957ae109d88e28b2fcafa49d5cac4079f5 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sat, 2 Oct 2021 10:49:47 +0200 Subject: [PATCH] Fix last seen not published when changed. https://github.com/Koenkk/zigbee2mqtt/issues/7423 --- lib/controller.ts | 4 ++++ test/controller.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/controller.ts b/lib/controller.ts index b931742ad..2dbf197d9 100644 --- a/lib/controller.ts +++ b/lib/controller.ts @@ -150,6 +150,10 @@ class Controller { // Call extensions await this.callExtensions('start', this.extensions); + + if (settings.get().advanced.last_seen && settings.get().advanced.last_seen !== 'disable') { + this.eventBus.onLastSeenChanged(this, (data) => this.publishEntityState(data.device, {})); + } } @bind async enableDisableExtension(enable: boolean, name: string): Promise { diff --git a/test/controller.test.js b/test/controller.test.js index 4685f1e14..0af5975ec 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -632,4 +632,16 @@ describe('Controller', () => { expect(settings.get().advanced.homeassistant_legacy_entity_attributes).toBeFalsy(); expect(settings.get().advanced.legacy_api).toBeFalsy(); }); + + it('Should publish last seen changes', async () => { + settings.set(['advanced', 'last_seen'], 'epoch'); + await controller.start(); + await flushPromises(); + MQTT.publish.mockClear(); + const device = zigbeeHerdsman.devices.remote; + await zigbeeHerdsman.events.lastSeenChanged({device}); + expect(MQTT.publish).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/remote', stringify({"brightness":255,"last_seen":1000}), { qos: 0, retain: true }, expect.any(Function)); + }); });