Skip to content

Commit

Permalink
move recording of bindings to addBinding (Koenkk#256)
Browse files Browse the repository at this point in the history
* move recording of bindings to addBinding

* add addBinding tests
  • Loading branch information
sjorge authored Nov 22, 2020
1 parent 895b2f3 commit a114eec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/controller/model/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,26 @@ class Endpoint extends Entity {
}
}

public addBinding(clusterKey: number | string, target: Endpoint | Group | number): void {
const cluster = Zcl.Utils.getCluster(clusterKey);
if (typeof target === 'number') {
target = Group.byGroupID(target) || Group.create(target);
}

if (!this.binds.find((b) => b.cluster.ID === cluster.ID && b.target === target)) {
if (target instanceof Group) {
this._binds.push({cluster: cluster.ID, groupID: target.groupID, type: 'group'});
} else {
this._binds.push({
cluster: cluster.ID, type: 'endpoint', deviceIeeeAddress: target.deviceIeeeAddress,
endpointID: target.ID
});
}

this.save();
}
}

public async bind(clusterKey: number | string, target: Endpoint | Group | number): Promise<void> {
const cluster = Zcl.Utils.getCluster(clusterKey);
const type = target instanceof Endpoint ? 'endpoint' : 'group';
Expand All @@ -363,18 +383,7 @@ class Endpoint extends Entity {
target instanceof Endpoint ? target.ID : null,
);

if (!this.binds.find((b) => b.cluster.ID === cluster.ID && b.target === target)) {
if (target instanceof Group) {
this._binds.push({cluster: cluster.ID, groupID: target.groupID, type: 'group'});
} else {
this._binds.push({
cluster: cluster.ID, type: 'endpoint', deviceIeeeAddress: target.deviceIeeeAddress,
endpointID: target.ID
});
}

this.save();
}
this.addBinding(clusterKey, target);
} catch (error) {
error.message = `${log} failed (${error.message})`;
debug.error(error.message);
Expand Down Expand Up @@ -635,4 +644,4 @@ class Endpoint extends Entity {
}
}

export default Endpoint;
export default Endpoint;
37 changes: 37 additions & 0 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,22 @@ describe('Controller', () => {
expect(mockAdapterBind).toBeCalledWith(129, "0x129", 1, 0, "0x170", "endpoint", 1);
});

it('Endpoint addBinding', async () => {
await controller.start();
skipWait = true;
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
await mockAdapterEvents['deviceJoined']({networkAddress: 170, ieeeAddr: '0x170'});
const device = controller.getDeviceByIeeeAddr('0x129');
const target = controller.getDeviceByIeeeAddr('0x170').getEndpoint(1);
const endpoint = device.getEndpoint(1);
endpoint.addBinding('genPowerCfg', target);
expect(deepClone(endpoint.binds)).toStrictEqual(deepClone([{cluster: Zcl.Utils.getCluster(1), target}]));

// Should bind another time but not add it to the binds
endpoint.addBinding('genPowerCfg', target);
expect(deepClone(endpoint.binds)).toStrictEqual(deepClone([{cluster: Zcl.Utils.getCluster(1), target}]));
});

it('Endpoint get binds non-existing device', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
Expand All @@ -2074,6 +2090,16 @@ describe('Controller', () => {
expect(mockAdapterBind).toBeCalledWith(129, "0x129", 1, 1, 4, "group", null);
});

it('Group addBinding', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
const group = await controller.createGroup(4);
const device = controller.getDeviceByIeeeAddr('0x129');
const endpoint = device.getEndpoint(1);
endpoint.addBinding('genBasic', group);
expect(deepClone(endpoint.binds)).toStrictEqual(deepClone([{cluster: Zcl.Utils.getCluster(0), target: group}]));
});

it('Group bind by number (should create group)', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
Expand All @@ -2086,6 +2112,17 @@ describe('Controller', () => {
expect(mockAdapterBind).toBeCalledWith(129, "0x129", 1, 1, 11, "group", null);
});

it('Group addBinding by number (should create group)', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
expect(Group.byGroupID(11)).toBeUndefined();
const device = controller.getDeviceByIeeeAddr('0x129');
const endpoint = device.getEndpoint(1);
endpoint.addBinding('genBasic', 11);
const group = Group.byGroupID(11);
expect(deepClone(endpoint.binds)).toStrictEqual(deepClone([{cluster: Zcl.Utils.getCluster(0), target: group}]));
});

it('Endpoint unbind', async () => {
await controller.start();
skipWait = true;
Expand Down

0 comments on commit a114eec

Please sign in to comment.