From d0ceaba6f2c261401610ae6f4428a2a7a10c7fd9 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 16 Jan 2022 17:25:53 +0000 Subject: [PATCH] Fix OTA (#10833) * dump * updates * Updates --- lib/extension/otaUpdate.ts | 15 +++++++-------- test/otaUpdate.test.js | 13 ++++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/extension/otaUpdate.ts b/lib/extension/otaUpdate.ts index 188d50e4c..7a8846819 100644 --- a/lib/extension/otaUpdate.ts +++ b/lib/extension/otaUpdate.ts @@ -121,11 +121,11 @@ export default class OTAUpdate extends Extension { } } - private async readSoftwareBuildIDAndDateCode(device: Device, sendWhenActive: boolean): + private async readSoftwareBuildIDAndDateCode(device: Device, sendWhen: 'active' | 'immediate'): Promise<{softwareBuildID: string, dateCode: string}> { try { const endpoint = device.zh.endpoints.find((e) => e.supportsInputCluster('genBasic')); - const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId'], {sendWhenActive}); + const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId'], {sendWhen}); return {softwareBuildID: result.swBuildId, dateCode: result.dateCode}; } catch (e) { return null; @@ -252,17 +252,16 @@ export default class OTAUpdate extends Extension { } }; - const from_ = await this.readSoftwareBuildIDAndDateCode(device, false); + const from_ = await this.readSoftwareBuildIDAndDateCode(device, 'immediate'); await device.definition.ota.updateToLatest(device.zh, logger, onProgress); - const to = await this.readSoftwareBuildIDAndDateCode(device, device.zh.type === 'EndDevice'); - const [fromS, toS] = [stringify(from_), stringify(to)]; + logger.info(`Finished update of '${device.name}'`); this.eventBus.emitReconfigure({device}); - const msg = `Finished update of '${device.name}'` + - (to ? `, from '${fromS}' to '${toS}'` : ``); - logger.info(msg); this.removeProgressAndRemainingFromState(device); const payload = this.getEntityPublishPayload('idle'); this.publishEntityState(device, payload); + const to = await this.readSoftwareBuildIDAndDateCode(device, 'active'); + const [fromS, toS] = [stringify(from_), stringify(to)]; + logger.info(`Device '${device.name}' was updated from '${fromS}' to '${toS}'`); responseData.from = from_ ? utils.toSnakeCase(from_) : null; responseData.to = to ? utils.toSnakeCase(to) : null; this.eventBus.emitDevicesChanged(); diff --git a/test/otaUpdate.test.js b/test/otaUpdate.test.js index e8991cc67..6dfa104d5 100644 --- a/test/otaUpdate.test.js +++ b/test/otaUpdate.test.js @@ -78,9 +78,11 @@ describe('OTA update', () => { expect(mapped.ota.updateToLatest).toHaveBeenCalledWith(device, logger, expect.any(Function)); expect(logger.info).toHaveBeenCalledWith(`Update of 'bulb' at 0.00%`); expect(logger.info).toHaveBeenCalledWith(`Update of 'bulb' at 10.00%, ≈ 60 minutes remaining`); - expect(logger.info).toHaveBeenCalledWith(`Finished update of 'bulb', from '{"dateCode":"20190101","softwareBuildID":1}' to '{"dateCode":"20190102","softwareBuildID":2}'`); + expect(logger.info).toHaveBeenCalledWith(`Finished update of 'bulb'`); + expect(logger.info).toHaveBeenCalledWith(`Device 'bulb' was updated from '{"dateCode":"20190101","softwareBuildID":1}' to '{"dateCode":"20190103","softwareBuildID":3}'`); expect(device.save).toHaveBeenCalledTimes(2); - expect(endpoint.read).toHaveBeenCalledWith('genBasic', ['dateCode', 'swBuildId'], {'sendWhenActive': false}); + expect(endpoint.read).toHaveBeenCalledWith('genBasic', ['dateCode', 'swBuildId'], {'sendWhen': 'immediate'}); + expect(endpoint.read).toHaveBeenCalledWith('genBasic', ['dateCode', 'swBuildId'], {'sendWhen': 'active'}); expect(MQTT.publish).toHaveBeenCalledWith( 'zigbee2mqtt/bulb', stringify({"update_available":false,"update":{"state":"updating","progress":0}}), @@ -98,7 +100,7 @@ describe('OTA update', () => { ); expect(MQTT.publish).toHaveBeenCalledWith( 'zigbee2mqtt/bridge/response/device/ota_update/update', - stringify({"data":{"id": "bulb","from":{"software_build_id":1,"date_code":"20190101"},"to":{"software_build_id":2,"date_code":"20190102"}},"status":"ok"}), + stringify({"data":{"from":{"date_code":"20190101","software_build_id":1},"id":"bulb","to":{"date_code":"20190103","software_build_id":3}},"status":"ok"}), {retain: false, qos: 0}, expect.any(Function) ); expect(MQTT.publish).toHaveBeenCalledWith( @@ -377,10 +379,11 @@ describe('OTA update', () => { expect(mapped.ota.updateToLatest).toHaveBeenCalledWith(device, logger, expect.any(Function)); expect(logger.info).toHaveBeenCalledWith(`Update of 'bulb' at 0.00%`); expect(logger.info).toHaveBeenCalledWith(`Update of 'bulb' at 10.00%, ≈ 60 minutes remaining`); - expect(logger.info).toHaveBeenCalledWith(`Finished update of 'bulb', from '{"dateCode":"20190101","softwareBuildID":1}' to '{"dateCode":"20190102","softwareBuildID":2}'`); + expect(logger.info).toHaveBeenCalledWith(`Finished update of 'bulb'`); + expect(logger.info).toHaveBeenCalledWith(`Device 'bulb' was updated from '{"dateCode":"20190101","softwareBuildID":1}' to '{"dateCode":"20190103","softwareBuildID":3}'`); expect(logger.error).toHaveBeenCalledTimes(0); expect(device.save).toHaveBeenCalledTimes(2); - expect(endpoint.read).toHaveBeenCalledWith('genBasic', ['dateCode', 'swBuildId'], {'sendWhenActive': false}); + expect(endpoint.read).toHaveBeenCalledWith('genBasic', ['dateCode', 'swBuildId'], {'sendWhen': 'immediate'}); }); it('Legacy api: Should handle when OTA update fails', async () => {