Skip to content

Commit

Permalink
Retrieve default ep from mapping. Koenkk#1662
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 25, 2019
1 parent e5ea50a commit 3453724
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function getEndpointByEntityID(zigbee, entityID, epName) {
logger.error(`Device ${mappedDevice.model} doesn't have ep named '${epName}'`);
return;
}
} else if (mappedDevice.hasOwnProperty('ep')) {
const eps = mappedDevice.ep(device);
epID = eps[''] || null;
}

const endpoint = zigbee.getEndpoint(entityID, epID);
Expand Down
19 changes: 19 additions & 0 deletions test/deviceBind.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const devices = {
return {inClusterList: [5, 6]};
},
},
occupancy_sensor: {
getSimpleDesc: () => {
return {outClusterList: [5, 6]};
},
},
};

const zigbee = {
Expand All @@ -46,12 +51,15 @@ const zigbee = {
return devices.switch_ep2;
} else if (ep == 3 && ID === 'switch_ep3') {
return devices.switch_ep3;
} else if (ep == 2 && ID == 'occupancy_sensor') {
return devices.occupancy_sensor;
}

throw new Error(`No mock for ${ID} and ep ${ep}`);
},
getDevice: (ID) => {
const lookup = {
'occupancy_sensor': 'SML002',
'switch_ep2': 'lumi.sensor_86sw2.es1',
'switch_ep3': 'DNCKAT_S003',
'bulb': 'TRADFRI bulb E27 WS opal 980lm',
Expand Down Expand Up @@ -154,6 +162,17 @@ describe('DeviceBind', () => {
expect.any(Function)
);
});

it('Bind default ep when mapped', async () => {
deviceBind.onMQTTMessage('zigbee2mqtt/bridge/bind/occupancy_sensor', 'bulb');
expect(zigbee.bind).toHaveBeenCalledTimes(1);
expect(zigbee.bind).toHaveBeenNthCalledWith(1,
devices.occupancy_sensor,
6,
devices.bulb,
expect.any(Function)
);
});
});

describe('Unbind devices', () => {
Expand Down
41 changes: 41 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,45 @@ describe('Utils', () => {
expect('EndDevice').toBe(utils.correctDeviceType(device));
});
});

describe('Get endpoint by id', () => {
it('Pick default ep', () => {
const zigbee = {
getDevice: (entityID) => {
return {modelId: 'TRADFRI on/off switch'};
},
getEndpoint: (entityID, epId) => {
return {epId: epId == null ? 1 : 0};
},
};
const endpoint = utils.getEndpointByEntityID(zigbee, '0x12345678', null);
expect(endpoint.epId).toBe(1);
});

it('Pick default ep from mapping when default defined', () => {
const zigbee = {
getDevice: (entityID) => {
return {modelId: 'SML002'};
},
getEndpoint: (entityID, epId) => {
return {epId};
},
};
const endpoint = utils.getEndpointByEntityID(zigbee, '0x12345678', null);
expect(endpoint.epId).toBe(2);
});

it('Pick default ep from mapping when not defined', () => {
const zigbee = {
getDevice: (entityID) => {
return {modelId: 'lumi.sensor_86sw2.es1'};
},
getEndpoint: (entityID, epId) => {
return {epId: epId == null ? 1 : 0};
},
};
const endpoint = utils.getEndpointByEntityID(zigbee, '0x12345678', null);
expect(endpoint.epId).toBe(1);
});
});
});

0 comments on commit 3453724

Please sign in to comment.