diff --git a/lib/extension/bridge.js b/lib/extension/bridge.js index 5ee5e40c..c90bc3f9 100644 --- a/lib/extension/bridge.js +++ b/lib/extension/bridge.js @@ -24,6 +24,7 @@ class Bridge extends Extension { 'config/lastseen': this.configLastSeen.bind(this), 'config/elapsed': this.configElapsed.bind(this), 'config/loglevel': this.configLogLevel.bind(this), + 'touchlink/factoryreset': this.touchlinkFactoryReset.bind(this), }; } @@ -169,6 +170,18 @@ class Bridge extends Extension { return utils.getResponse(message, {value}, null); } + async touchlinkFactoryReset(message) { + logger.info('Start touchlink factory reset'); + const result = await this.zigbee.touchlinkFactoryReset(); + if (result) { + logger.info('Successfully factory reset device through Touchlink'); + return utils.getResponse(message, {}, null); + } else { + logger.error('Failed to factory reset device through Touchlink'); + throw new Error('Failed to factory reset device through Touchlink'); + } + } + /** * Utils */ diff --git a/test/bridge.test.js b/test/bridge.test.js index ecd40e7a..af33d4be 100644 --- a/test/bridge.test.js +++ b/test/bridge.test.js @@ -548,4 +548,32 @@ describe('Bridge', () => { {retain: false, qos: 0}, expect.any(Function) ); }); + + it('Should allow to touchlink factory reset (succeeds)', async () => { + MQTT.publish.mockClear(); + zigbeeHerdsman.touchlinkFactoryReset.mockClear(); + zigbeeHerdsman.touchlinkFactoryReset.mockReturnValueOnce(true); + MQTT.events.message('zigbee2mqtt/bridge/request/touchlink/factoryReset', ''); + await flushPromises(); + expect(zigbeeHerdsman.touchlinkFactoryReset).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/response/touchlink/factoryReset', + JSON.stringify({"data":{},"status":"ok"}), + {retain: false, qos: 0}, expect.any(Function) + ); + }); + + it('Should allow to touchlink factory reset (fails)', async () => { + MQTT.publish.mockClear(); + zigbeeHerdsman.touchlinkFactoryReset.mockClear(); + zigbeeHerdsman.touchlinkFactoryReset.mockReturnValueOnce(false); + MQTT.events.message('zigbee2mqtt/bridge/request/touchlink/factoryReset', ''); + await flushPromises(); + expect(zigbeeHerdsman.touchlinkFactoryReset).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/response/touchlink/factoryReset', + JSON.stringify({"data":{},"status":"error","error":"Failed to factory reset device through Touchlink"}), + {retain: false, qos: 0}, expect.any(Function) + ); + }); });