Skip to content

Commit

Permalink
Touchlink identify. #3281
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Sep 9, 2020
1 parent 7f29c02 commit c29cc78
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/extension/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Bridge extends Extension {
'config/elapsed': this.configElapsed.bind(this),
'config/log_level': this.configLogLevel.bind(this),
'touchlink/factory_reset': this.touchlinkFactoryReset.bind(this),
'touchlink/identify': this.touchlinkIdentify.bind(this),
'touchlink/scan': this.touchlinkScan.bind(this),
'health_check': this.healthCheck.bind(this),
};
Expand Down Expand Up @@ -218,6 +219,17 @@ class Bridge extends Extension {
return utils.getResponse(message, {value}, null);
}

async touchlinkIdentify(message) {
if (typeof message !== 'object' || !message.hasOwnProperty('ieee_address') ||
!message.hasOwnProperty('channel')) {
throw new Error('Invalid payload');
}

logger.info(`Start Touchlink identify of '${message.ieee_address}' on channel ${message.channel}`);
await this.zigbee.touchlinkIdentify(message.ieee_address, message.channel);
return utils.getResponse(message, {ieee_address: message.ieee_address, channel: message.channel}, null);
}

async touchlinkFactoryReset(message) {
let result = false;
const payload = {};
Expand Down
4 changes: 4 additions & 0 deletions lib/zigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ class Zigbee extends events.EventEmitter {
return this.herdsman.touchlinkFactoryReset(ieeeAddr, channel);
}

async touchlinkIdentify(ieeeAddr, channel) {
await this.herdsman.touchlinkIdentify(ieeeAddr, channel);
}

async touchlinkScan() {
return this.herdsman.touchlinkScan();
}
Expand Down
27 changes: 27 additions & 0 deletions test/bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,33 @@ describe('Bridge', () => {
);
});

it('Should allow to touchlink identify specific device', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.touchlinkIdentify.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/touchlink/identify', stringify({ieee_address: '0x1239', channel: 12}));
await flushPromises();
expect(zigbeeHerdsman.touchlinkIdentify).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.touchlinkIdentify).toHaveBeenCalledWith('0x1239', 12);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/touchlink/identify',
stringify({"data":{"ieee_address":'0x1239',"channel":12},"status":"ok"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Touchlink identify fails when payload is invalid', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.touchlinkIdentify.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/touchlink/identify', stringify({ieee_address: '0x1239'}));
await flushPromises();
expect(zigbeeHerdsman.touchlinkIdentify).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/touchlink/identify',
stringify({"data":{},"status":"error","error":"Invalid payload"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should allow to touchlink factory reset (fails)', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.touchlinkFactoryResetFirst.mockClear();
Expand Down
1 change: 1 addition & 0 deletions test/stub/zigbeeHerdsman.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const mock = {
touchlinkFactoryReset: jest.fn(),
touchlinkFactoryResetFirst: jest.fn(),
touchlinkScan: jest.fn(),
touchlinkIdentify: jest.fn(),
start: jest.fn(),
permitJoin: jest.fn(),
getCoordinatorVersion: jest.fn().mockReturnValue({type: 'z-Stack', meta: {version: 1, revision: 20190425}}),
Expand Down

0 comments on commit c29cc78

Please sign in to comment.