Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add api for device re-interview #22788

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/extension/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Bridge extends Extension {
'device/options': this.deviceOptions,
'device/configure_reporting': this.deviceConfigureReporting,
'device/remove': this.deviceRemove,
'device/interview': this.deviceInterview,
'device/generate_external_definition': this.deviceGenerateExternalDefinition,
'device/rename': this.deviceRename,
'group/add': this.groupAdd,
Expand Down Expand Up @@ -500,6 +501,25 @@ export default class Bridge extends Extension {
}, null);
}

@bind async deviceInterview(message: string | KeyValue): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('id')) {
throw new Error(`Invalid payload`);
}

const device = this.getEntity('device', message.id) as Device;

try {
await device.zh.interview();
} catch (error) {
throw new Error(`interview of '${device.name}' (${device.ieeeAddr}) failed: ${error}`, {cause: error});
}

justfalter marked this conversation as resolved.
Show resolved Hide resolved
// publish devices so that the front-end has up-to-date info.
await this.publishDevices();

return utils.getResponse(message, {id: message.id}, null);
}

@bind async deviceGenerateExternalDefinition(message: string | KeyValue): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('id')) {
throw new Error(`Invalid payload`);
Expand Down
98 changes: 98 additions & 0 deletions test/bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('Bridge', () => {
logger.setTransportsEnabled(false);
MQTT.publish.mockClear();
const device = zigbeeHerdsman.devices.bulb;
device.interview.mockClear();
device.removeFromDatabase.mockClear();
device.removeFromNetwork.mockClear();
extension.lastJoinedDeviceIeeeAddr = null;
Expand Down Expand Up @@ -727,6 +728,103 @@ describe('Bridge', () => {
);
});

it('Should allow interviewing a device by friendly name', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.devices.bulb.interview.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: 'bulb'}));
await flushPromises();
expect(zigbeeHerdsman.devices.bulb.interview).toHaveBeenCalled();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{"id":"bulb"},"status":"ok"}),
{retain: false, qos: 0}, expect.any(Function)
);

// The following indicates that devices have published.
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/devices',
expect.any(String),
{retain: true, qos: 0}, expect.any(Function)
);
});

it('Should allow interviewing a device by ieeeAddr', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.devices.bulb.interview.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: "0x000b57fffec6a5b2"}));
await flushPromises();
expect(zigbeeHerdsman.devices.bulb.interview).toHaveBeenCalled();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{"id":"0x000b57fffec6a5b2"},"status":"ok"}),
{retain: false, qos: 0}, expect.any(Function)
);

// The following indicates that devices have published.
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/devices',
expect.any(String),
{retain: true, qos: 0}, expect.any(Function)
);
});

it('Should throw error on invalid device interview payload', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({foo: 'bulb'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{},"status":"error","error":"Invalid payload"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should throw error on non-existing device interview', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: 'bulb_not_existing'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{},"status":"error","error":"Device 'bulb_not_existing' does not exist"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should throw error on id is device endpoint', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: 'bulb/1'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{},"status":"error","error":"Device 'bulb/1' does not exist"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should throw error on id is a group', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: 'group_1'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{},"status":"error","error":"Device 'group_1' does not exist"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should throw error on when interview fails', async () => {
MQTT.publish.mockClear();
zigbeeHerdsman.devices.bulb.interview.mockClear();
zigbeeHerdsman.devices.bulb.interview.mockImplementation(() => Promise.reject(new Error('something went wrong')))
MQTT.events.message('zigbee2mqtt/bridge/request/device/interview', stringify({id: 'bulb'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/interview',
stringify({"data":{},"status":"error","error":"interview of 'bulb' (0x000b57fffec6a5b2) failed: Error: something went wrong"}),
{retain: false, qos: 0}, expect.any(Function)
);
});

it('Should error when generate_external_definition is invalid', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/generate_external_definition', stringify({wrong: ZNCZ02LM.ieeeAddr}));
Expand Down
1 change: 1 addition & 0 deletions test/stub/zigbeeHerdsman.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Device {
this.softwareBuildID = softwareBuildID;
this.interviewCompleted = interviewCompleted;
this.modelID = modelID;
this.interview = jest.fn();
this.interviewing = interviewing;
this.meta = {};
this.ping = jest.fn();
Expand Down