Skip to content

Commit

Permalink
Add more properties for coordinator /devices/get. #2020
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Sep 23, 2019
1 parent f60b4dd commit 3001e2d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/extension/bridgeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,19 @@ class BridgeConfig extends BaseExtension {
}

async devices(topic, message) {
const coordinator = await this.zigbee.getCoordinatorVersion();
const devices = this.zigbee.getDevices().map((device) => {
const payload = {
ieeeAddr: device.ieeeAddr,
type: device.type,
networkAddress: device.networkAddress,
};

if (device.type !== 'Coordinator') {
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
const friendlyDevice = settings.getDevice(device.ieeeAddr);
payload.model = mappedDevice ? mappedDevice.model : device.modelID;
payload.friendly_name = friendlyDevice ? friendlyDevice.friendly_name : device.ieeeAddr;
payload.networkAddress = device.networkAddress;
payload.manufacturerID = device.manufacturerID;
payload.manufacturerName = device.manufacturerName;
payload.powerSource = device.powerSource;
Expand All @@ -150,6 +151,11 @@ class BridgeConfig extends BaseExtension {
payload.softwareBuildID = device.softwareBuildID;
payload.dateCode = device.dateCode;
payload.lastSeen = device.lastSeen;
} else {
payload.friendly_name = 'Coordinator';
payload.softwareBuildID = coordinator.type;
payload.dateCode = coordinator.meta.revision.toString();
payload.lastSeen = Date.now();
}

return payload;
Expand Down
5 changes: 4 additions & 1 deletion test/bridgeConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,17 @@ describe('Bridge config', () => {
});

it('Should allow to get devices', async () => {
const now = Date.now;
Date.now = () => 100;
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/config/devices/get', '');
await flushPromises();
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/config/devices');
const payload = JSON.parse(MQTT.publish.mock.calls[0][1]);
expect(payload.length).toStrictEqual(Object.values(zigbeeHerdsman.devices).length);
expect(payload[0]).toStrictEqual({"ieeeAddr": "0x00124b00120144ae", "type": "Coordinator"});
expect(payload[0]).toStrictEqual({"ieeeAddr": "0x00124b00120144ae", "type": "Coordinator", "dateCode": "20190425", "friendly_name": "Coordinator", networkAddress: 0, softwareBuildID: "z-Stack", lastSeen: 100});
expect(payload[1]).toStrictEqual({"friendly_name": "bulb", "ieeeAddr": "0x000b57fffec6a5b2", "lastSeen": 1000, "manufacturerID": 4476, "model": "LED1545G12", "modelID": "TRADFRI bulb E27 WS opal 980lm", "networkAddress": 40369, "powerSource": "Mains (single phase)", "type": "Router"});
Date.now = now;
});

it('Should allow to get groups', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/stub/zigbeeHerdsman.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const groups = {
const mock = {
start: jest.fn(),
permitJoin: jest.fn(),
getCoordinatorVersion: jest.fn().mockReturnValue({type: 'z-Stack', meta: {version: 1}}),
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) => {
events[type] = handler;
Expand Down

0 comments on commit 3001e2d

Please sign in to comment.