From d8d78af4ecbf135d671ee5be09a164037aa4f897 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 6 May 2019 20:29:17 +0200 Subject: [PATCH] Query device state on endDeviceAnnce when using device availability. https://github.com/Koenkk/zigbee2mqtt/issues/1383 --- lib/extension/deviceAvailability.js | 32 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/extension/deviceAvailability.js b/lib/extension/deviceAvailability.js index 7f9d0fd1e..c6ae512b3 100644 --- a/lib/extension/deviceAvailability.js +++ b/lib/extension/deviceAvailability.js @@ -147,18 +147,34 @@ class DeviceAvailability { } onZigbeeMessage(message, device, mappedDevice) { - // When a zigbee message from a device is received we know the device is still alive. - // => reset the timer. if (device && this.isPingable(this.zigbee.getDevice(device.ieeeAddr))) { - // When a message is received and the device is marked as offline, mark it online. - if (this.state.hasOwnProperty(device.ieeeAddr) && !this.state[device.ieeeAddr]) { - this.publishAvailability(device.ieeeAddr, true); - } else if (!this.state.hasOwnProperty(device.ieeeAddr)) { + // When a zigbee message from a device is received we know the device is still alive. + // => reset the timer. + this.setTimer(device); + + const online = this.state.hasOwnProperty(device.ieeeAddr) && this.state[device.ieeeAddr]; + const offline = this.state.hasOwnProperty(device.ieeeAddr) && !this.state[device.ieeeAddr]; + + if (!online && !offline) { // A new device has been connected this.publishAvailability(device.ieeeAddr, true); + } else if (offline) { + // When a message is received and the device is marked as offline, mark it online. + this.publishAvailability(device.ieeeAddr, true); + } else if (online) { + /** + * In case the device is powered off AND on within the availability timeout, + * zigbee2qmtt does not detect the device as offline (device is still marked online). + * When a device is turned on again the state could be out of sync. + * https://github.com/Koenkk/zigbee2mqtt/issues/1383#issuecomment-489412168 + * endDeviceAnnce is typically send when a device comes online. + * + * This isn't needed for TRADFRI devices as they already send the state themself. + */ + if (message.type === 'endDeviceAnnce' && !utils.isIkeaTradfriDevice(device)) { + this.onReconnect(device.ieeeAddr); + } } - - this.setTimer(device); } } }