Skip to content

Commit

Permalink
fix: On NO_ENTRY error during unbind cleanup database
Browse files Browse the repository at this point in the history
* initial implementation
* rework for reviewer feedback
  • Loading branch information
sjorge committed Sep 28, 2024
1 parent 5d02efe commit b2f2f03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/controller/model/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,18 @@ class Endpoint extends Entity {
this.save();
} catch (error) {
const err = error as Error;
err.message = `${log} failed (${err.message})`;
logger.debug(err.stack!, NS);
throw error;
if ((error as Zdo.StatusError).code == Zdo.Status.NO_ENTRY) {
/* Device returned NO_ENTRY error, meaning the bind is not present
* Do not threat this as an error and remove the entry from the database instead.
*/
logger.debug(`${log} failed (${err.message}), removing entry from database.`, NS);
this._binds.splice(index, 1);
this.save();
} else {
err.message = `${log} failed (${err.message})`;
logger.debug(err.stack!, NS);
throw error;
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7995,6 +7995,34 @@ describe('Controller', () => {
expect(error).toStrictEqual(new Error(`Unbind 0x129/1 genOnOff invalid target '1' (no group with this ID exists).`));
});

it('Unbind against unbound cluster', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
await mockAdapterEvents['deviceJoined']({networkAddress: 170, ieeeAddr: '0x170'});
const endpoint = controller.getDeviceByIeeeAddr('0x129')!.getEndpoint(1)!;
const target = controller.getDeviceByIeeeAddr('0x170')!.getEndpoint(1)!;
await endpoint.bind('genOnOff', target);
mockAdapterSendZdo.mockClear();

sendZdoResponseStatus = Zdo.Status.NO_ENTRY;

await endpoint.unbind('genOnOff', target);

const zdoPayload = Zdo.Buffalo.buildRequest(
false,
Zdo.ClusterId.UNBIND_REQUEST,
'0x129',
1,
Zcl.Clusters.genOnOff.ID,
Zdo.UNICAST_BINDING,
'0x170',
0,
1,
);
expect(mockAdapterSendZdo).toHaveBeenCalledWith('0x129', 129, Zdo.ClusterId.UNBIND_REQUEST, zdoPayload, false);
expect(endpoint.binds).toStrictEqual([]);
});

it('Unbind error', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
Expand Down

0 comments on commit b2f2f03

Please sign in to comment.