Stop pinging device (availability feature) when it is removed. https://github.com/Koenkk/zigbee2mqtt/issues/9490

This commit is contained in:
Koen Kanters 2021-11-04 20:52:10 +01:00
parent 178da2db1d
commit 20b6fd42d3
2 changed files with 18 additions and 4 deletions

View File

@ -97,10 +97,10 @@ export default class Availability extends Extension {
override async start(): Promise<void> {
logger.warn('Using experimental new availability feature');
this.eventBus.onDeviceRenamed(this, (data: eventdata.DeviceRenamed) =>
this.publishAvailability(data.device, false, true));
this.eventBus.onDeviceLeave(this, (data: eventdata.DeviceLeave) => clearTimeout(this.timers[data.ieeeAddr]));
this.eventBus.onDeviceAnnounce(this, (data: eventdata.DeviceAnnounce) => this.retrieveState(data.device));
this.eventBus.onDeviceRenamed(this, (data) => this.publishAvailability(data.device, false, true));
this.eventBus.onDeviceRemoved(this, (data) => clearTimeout(this.timers[data.ieeeAddr]));
this.eventBus.onDeviceLeave(this, (data) => clearTimeout(this.timers[data.ieeeAddr]));
this.eventBus.onDeviceAnnounce(this, (data) => this.retrieveState(data.device));
this.eventBus.onLastSeenChanged(this, this.onLastSeenChanged);
for (const device of this.zigbee.devices(false)) {

View File

@ -205,6 +205,20 @@ describe('Availability', () => {
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
});
it('Should stop pinging device when it is removed', async () => {
await resetExtension();
MQTT.publish.mockClear();
await advancedTime(utils.minutes(9));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
MQTT.events.message('zigbee2mqtt/bridge/request/device/remove', stringify({id: "bulb_color"}));
await flushPromises();
await advancedTime(utils.minutes(3));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
});
it('Should allow to be disabled', async () => {
settings.set(['availability'], false);
await resetExtension();