Add install code support (#13780)

* Add install code support

* Update bridge.ts

Co-authored-by: John Doe <nurikk@users.noreply.github.com>
This commit is contained in:
Koen Kanters 2022-09-06 09:30:03 +02:00 committed by GitHub
parent 0b645a4e6a
commit 9519ffdac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 0 deletions

View File

@ -44,6 +44,7 @@ export default class Bridge extends Extension {
'backup': this.backup,
'touchlink/factory_reset': this.touchlinkFactoryReset,
'touchlink/identify': this.touchlinkIdentify,
'install_code/add': this.installCodeAdd,
'touchlink/scan': this.touchlinkScan,
'health_check': this.healthCheck,
'options': this.bridgeOptions,
@ -248,6 +249,17 @@ export default class Bridge extends Extension {
return utils.getResponse(message, {zip: base64Zip}, null);
}
@bind async installCodeAdd(message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message === 'object' && !message.hasOwnProperty('value')) {
throw new Error('Invalid payload');
}
const value = typeof message === 'object' ? message.value : message;
await this.zigbee.addInstallCode(value);
logger.info('Successfully added new install code');
return utils.getResponse(message, {value}, null);
}
@bind async permitJoin(message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message === 'object' && !message.hasOwnProperty('value')) {
throw new Error('Invalid payload');

View File

@ -310,6 +310,10 @@ export default class Zigbee {
return this.herdsman.touchlinkFactoryReset(ieeeAddr, channel);
}
async addInstallCode(installCode: string): Promise<void> {
await this.herdsman.addInstallCode(installCode);
}
async touchlinkIdentify(ieeeAddr: string, channel: number): Promise<void> {
await this.herdsman.touchlinkIdentify(ieeeAddr, channel);
}

View File

@ -913,6 +913,47 @@ describe('Bridge', () => {
);
});
it('Add install code', async () => {
MQTT.publish.mockClear();
// By object
zigbeeHerdsman.addInstallCode.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/install_code/add', stringify({value: 'my-code'}));
await flushPromises();
expect(zigbeeHerdsman.addInstallCode).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.addInstallCode).toHaveBeenCalledWith('my-code');
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/install_code/add',
stringify({"data":{"value":'my-code'},"status":"ok"}),
{retain: false, qos: 0}, expect.any(Function)
);
// By string
zigbeeHerdsman.addInstallCode.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/install_code/add', 'my-string-code');
await flushPromises();
expect(zigbeeHerdsman.addInstallCode).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.addInstallCode).toHaveBeenCalledWith('my-string-code');
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/install_code/add',
stringify({"data":{"value":'my-code'},"status":"ok"}),
{retain: false, qos: 0}, expect.any(Function)
);
});
it('Add install code error', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.addInstallCode.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/install_code/add', stringify({wrong: 'my-code'}));
await flushPromises();
expect(zigbeeHerdsman.addInstallCode).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/install_code/add',
stringify({"data":{},"status":"error","error":"Invalid payload"}),
{retain: false, qos: 0}, expect.any(Function)
);
});
it('Should allow to touchlink identify specific device', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.touchlinkIdentify.mockClear();

View File

@ -214,6 +214,7 @@ const mock = {
backup: jest.fn(),
isStopping: jest.fn(),
permitJoin: jest.fn(),
addInstallCode: jest.fn(),
getCoordinatorVersion: jest.fn().mockReturnValue({type: 'z-Stack', meta: {version: 1, revision: 20190425}}),
getNetworkParameters: jest.fn().mockReturnValue({panID: 0x162a, extendedPanID: [0, 11, 22], channel: 15}),
on: (type, handler) => {