Skip to content

Commit cc7f479

Browse files
authored
fix: Remove disappeared endpoints (#1227)
1 parent 50d9b2f commit cc7f479

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/controller/model/device.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,6 @@ class Device extends Entity<ControllerEventMap> {
10741074

10751075
// Make sure that the endpoint are sorted.
10761076
activeEndpoints.endpointList.sort((a, b) => a - b);
1077-
1078-
// TODO: this does not take care of removing endpoint (changing custom devices)?
10791077
for (const endpoint of activeEndpoints.endpointList) {
10801078
// Some devices, e.g. TERNCY return endpoint 0 in the active endpoints request.
10811079
// This is not a valid endpoint number according to the ZCL, requesting a simple descriptor will result
@@ -1085,6 +1083,9 @@ class Device extends Entity<ControllerEventMap> {
10851083
this._endpoints.push(Endpoint.create(endpoint, undefined, undefined, [], [], this.networkAddress, this.ieeeAddr));
10861084
}
10871085
}
1086+
1087+
// Remove disappeared endpoints (can happen with e.g. custom devices).
1088+
this._endpoints = this._endpoints.filter((e) => activeEndpoints.endpointList.includes(e.ID));
10881089
}
10891090

10901091
/**

test/controller.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as Events from '../src/controller/events';
1515
import Request from '../src/controller/helpers/request';
1616
import zclTransactionSequenceNumber from '../src/controller/helpers/zclTransactionSequenceNumber';
1717
import ZclTransactionSequenceNumber from '../src/controller/helpers/zclTransactionSequenceNumber';
18-
import {Device, Group} from '../src/controller/model';
18+
import {Device, Endpoint, Group} from '../src/controller/model';
1919
import * as Models from '../src/models';
2020
import {Wait} from '../src/utils';
2121
import * as Utils from '../src/utils';
@@ -4037,6 +4037,18 @@ describe('Controller', () => {
40374037
expect(mockAdapterSendZdo).toHaveBeenCalledTimes(8); // nodeDesc + activeEp + simpleDesc x1
40384038
});
40394039

4040+
it('Should remove disappeared endpoints on updateActiveEndpoints', async () => {
4041+
await controller.start();
4042+
mockAdapterSendZdo.mockClear();
4043+
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
4044+
4045+
const device = controller.getDeviceByIeeeAddr('0x129')!;
4046+
device.endpoints.push(Endpoint.create(2, undefined, undefined, [], [], device.networkAddress, device.ieeeAddr));
4047+
expect(device.endpoints.map((e) => e.ID)).toStrictEqual([1, 2]);
4048+
await device.updateActiveEndpoints();
4049+
expect(device.endpoints.map((e) => e.ID)).toStrictEqual([1]);
4050+
});
4051+
40404052
it('Receive zclData report from unkown attribute', async () => {
40414053
await controller.start();
40424054
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});

0 commit comments

Comments
 (0)