From 87faaa1817ce7a9c640bbc8c99b381aceac5d06c Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Thu, 13 Feb 2020 21:10:44 +0100 Subject: [PATCH] Don't crash when devices doesn't respond to read modelID after update. https://github.com/Koenkk/zigbee2mqtt/issues/2921 --- lib/extension/otaUpdate.js | 22 +++++++++++++--------- test/otaUpdate.test.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/extension/otaUpdate.js b/lib/extension/otaUpdate.js index e77a03d7..c633b824 100644 --- a/lib/extension/otaUpdate.js +++ b/lib/extension/otaUpdate.js @@ -16,16 +16,20 @@ class OTAUpdate extends BaseExtension { } async readSoftwareBuildIDAndDateCode(device, update) { - const endpoint = device.endpoints.find((e) => e.supportsInputCluster('genBasic')); - const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId']); + try { + const endpoint = device.endpoints.find((e) => e.supportsInputCluster('genBasic')); + const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId']); - if (update) { - device.softwareBuildID = result.swBuildId; - device.dateCode = result.dateCode; - device.save(); + if (update) { + device.softwareBuildID = result.swBuildId; + device.dateCode = result.dateCode; + device.save(); + } + + return {softwareBuildID: result.swBuildId, dateCode: result.dateCode}; + } catch (e) { + return null; } - - return {softwareBuildID: result.swBuildId, dateCode: result.dateCode}; } async onMQTTMessage(topic, message) { @@ -74,7 +78,7 @@ class OTAUpdate extends BaseExtension { await device.mapped.ota.updateToLatest(device.device, logger, onProgress); const to = await this.readSoftwareBuildIDAndDateCode(device.device, true); const [fromS, toS] = [JSON.stringify(from_), JSON.stringify(to)]; - logger.info(`Finished update of '${device.name}', from '${fromS}' to '${toS}'`); + logger.info(`Finished update of '${device.name}'` + (to ? `, from '${fromS}' to '${toS}'` : ``)); } catch (error) { logger.error(`Update of '${device.name}' failed (${error.message})`); } diff --git a/test/otaUpdate.test.js b/test/otaUpdate.test.js index 7bb4787f..b184a72b 100644 --- a/test/otaUpdate.test.js +++ b/test/otaUpdate.test.js @@ -143,4 +143,22 @@ describe('OTA update', () => { jest.runAllTimers(); await flushPromises(); }); + + it('Shouldnt crash when read modelID after OTA update fails', async () => { + const device = zigbeeHerdsman.devices.bulb; + const endpoint = device.endpoints[0]; + let count = 0; + endpoint.read.mockImplementation(() => { + if (count === 1) throw new Error('Failed!') + count++; + return {swBuildId: 1, dateCode: '2019010'} + }); + + const mapped = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID) + mockClear(mapped); + logger.info.mockClear(); + MQTT.events.message('zigbee2mqtt/bridge/ota_update/update', 'bulb'); + await flushPromises(); + expect(logger.info).toHaveBeenCalledWith(`Finished update of 'bulb'`); + }); });