2018-11-26 09:47:44 -07:00
|
|
|
const DeviceReceive = require('../lib/extension/deviceReceive');
|
|
|
|
const settings = require('../lib/util/settings');
|
|
|
|
const devices = require('zigbee-shepherd-converters').devices;
|
2018-11-28 11:34:37 -07:00
|
|
|
const utils = require('./utils');
|
2018-11-26 09:47:44 -07:00
|
|
|
|
|
|
|
// Devices
|
|
|
|
const WXKG11LM = devices.find((d) => d.model === 'WXKG11LM');
|
2018-11-27 13:50:55 -07:00
|
|
|
const WXKG02LM = devices.find((d) => d.model === 'WXKG02LM');
|
2018-12-15 14:02:23 -07:00
|
|
|
const WSDCGQ11LM = devices.find((d) => d.model === 'WSDCGQ11LM');
|
2019-01-11 14:38:16 -07:00
|
|
|
const RTCGQ11LM = devices.find((d) => d.model === 'RTCGQ11LM');
|
2018-11-26 09:47:44 -07:00
|
|
|
|
|
|
|
const mqtt = {
|
|
|
|
log: () => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('DeviceReceive', () => {
|
|
|
|
let deviceReceive;
|
2019-02-04 10:36:49 -07:00
|
|
|
let publishEntityState;
|
2018-11-26 09:47:44 -07:00
|
|
|
|
2018-11-27 13:50:55 -07:00
|
|
|
beforeEach(() => {
|
2019-03-09 15:34:19 -07:00
|
|
|
utils.stubLogger(jest);
|
|
|
|
jest.spyOn(settings, 'addDevice').mockImplementation(() => {});
|
|
|
|
publishEntityState = jest.fn();
|
2019-02-04 10:36:49 -07:00
|
|
|
deviceReceive = new DeviceReceive(null, mqtt, null, publishEntityState);
|
2018-11-27 13:50:55 -07:00
|
|
|
});
|
|
|
|
|
2018-11-28 11:34:37 -07:00
|
|
|
afterEach(() => {
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.restoreAllMocks();
|
2018-11-28 11:34:37 -07:00
|
|
|
});
|
|
|
|
|
2018-11-26 09:47:44 -07:00
|
|
|
describe('Handling zigbee messages', () => {
|
2019-02-08 16:28:59 -07:00
|
|
|
it('Should handle a zigbee message', () => {
|
2018-11-26 09:47:44 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
2018-11-28 13:30:09 -07:00
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
2018-11-26 09:47:44 -07:00
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({click: 'single'});
|
2018-11-26 09:47:44 -07:00
|
|
|
});
|
2018-11-27 13:50:55 -07:00
|
|
|
|
2019-02-08 16:28:59 -07:00
|
|
|
it('Should handle a zigbee message which uses ep (left)', () => {
|
2018-11-28 13:30:09 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
2018-11-27 13:50:55 -07:00
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({click: 'left'});
|
2018-11-27 13:50:55 -07:00
|
|
|
});
|
|
|
|
|
2019-02-08 16:28:59 -07:00
|
|
|
it('Should handle a zigbee message which uses ep (right)', () => {
|
2018-11-28 13:30:09 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 2);
|
2018-11-27 13:50:55 -07:00
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({click: 'right'});
|
2018-11-27 13:50:55 -07:00
|
|
|
});
|
2018-12-15 14:02:23 -07:00
|
|
|
|
|
|
|
it('Should handle a zigbee message with default precision', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({temperature: -0.85});
|
2018-12-15 14:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with 1 precision', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
|
2018-12-15 14:02:23 -07:00
|
|
|
return {temperature_precision: 1};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({temperature: -0.8});
|
2018-12-15 14:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with 0 precision', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
|
2018-12-15 14:02:23 -07:00
|
|
|
return {temperature_precision: 0};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({temperature: -1});
|
2018-12-15 14:02:23 -07:00
|
|
|
});
|
2018-12-19 09:33:02 -07:00
|
|
|
|
|
|
|
it('Should handle a zigbee message with 1 precision when set via device_options', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'get').mockImplementation(() => {
|
2018-12-19 09:33:02 -07:00
|
|
|
return {
|
|
|
|
device_options: {
|
|
|
|
temperature_precision: 1,
|
|
|
|
},
|
2019-01-18 12:31:55 -07:00
|
|
|
advanced: {
|
|
|
|
last_seen: 'disable',
|
|
|
|
},
|
2018-12-19 09:33:02 -07:00
|
|
|
};
|
|
|
|
});
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
|
2018-12-19 09:33:02 -07:00
|
|
|
return {};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({temperature: -0.8});
|
2019-03-09 14:12:10 -07:00
|
|
|
}
|
|
|
|
);
|
2018-12-19 09:33:02 -07:00
|
|
|
|
|
|
|
it('Should handle a zigbee message with 2 precision when overrides device_options', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'get').mockImplementation(() => {
|
2018-12-19 09:33:02 -07:00
|
|
|
return {
|
|
|
|
device_options: {
|
|
|
|
temperature_precision: 1,
|
|
|
|
},
|
2019-01-18 12:31:55 -07:00
|
|
|
advanced: {
|
|
|
|
last_seen: 'disable',
|
|
|
|
},
|
2018-12-19 09:33:02 -07:00
|
|
|
};
|
|
|
|
});
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'getDevice').mockImplementation(() => {
|
2018-12-19 09:33:02 -07:00
|
|
|
return {
|
|
|
|
temperature_precision: 2,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual({temperature: -0.85});
|
2019-03-09 14:12:10 -07:00
|
|
|
}
|
|
|
|
);
|
2018-12-27 14:51:16 -07:00
|
|
|
|
|
|
|
it('Should handle a zigbee message with voltage 3010', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', {'65281': {'1': 3010}}, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
2018-12-27 14:51:16 -07:00
|
|
|
const expected = {battery: 100, voltage: 3010};
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual(expected);
|
2018-12-27 14:51:16 -07:00
|
|
|
});
|
|
|
|
|
2019-01-06 07:31:08 -07:00
|
|
|
it('Should handle a zigbee message with voltage 2850', () => {
|
2018-12-27 14:51:16 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', {'65281': {'1': 2850}}, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
2019-01-06 07:31:08 -07:00
|
|
|
const expected = {battery: 35, voltage: 2850};
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual(expected);
|
2018-12-27 14:51:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with voltage 2650', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', {'65281': {'1': 2650}}, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
2019-01-06 07:31:08 -07:00
|
|
|
const expected = {battery: 14, voltage: 2650};
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual(expected);
|
2019-01-06 07:31:08 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with voltage 2000', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', {'65281': {'1': 2000}}, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
2019-01-06 07:31:08 -07:00
|
|
|
const expected = {battery: 0, voltage: 2000};
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual(expected);
|
2018-12-27 14:51:16 -07:00
|
|
|
});
|
2019-01-11 14:38:16 -07:00
|
|
|
|
|
|
|
it('Should publish 1 message when converted twice', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const payload = {
|
|
|
|
'65281': {'1': 3045, '3': 19, '4': 17320, '5': 35, '6': [0, 3], '10': 51107, '11': 381, '100': 0},
|
|
|
|
};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', payload, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, RTCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
2019-01-11 14:38:16 -07:00
|
|
|
const expected = {'battery': 100, 'illuminance': 381, 'voltage': 3045};
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState.mock.calls[0][1]).toEqual(expected);
|
2019-01-11 14:38:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should publish no message when converted without result', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const payload = {'9999': {'1': 3045}};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', payload, 1);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, RTCGQ11LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(0);
|
2019-01-11 14:38:16 -07:00
|
|
|
});
|
2019-01-18 12:31:55 -07:00
|
|
|
|
2019-02-08 16:28:59 -07:00
|
|
|
it('Should publish last_seen epoch', () => {
|
2019-01-18 12:31:55 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'get').mockImplementation(() => {
|
2019-01-18 12:31:55 -07:00
|
|
|
return {
|
|
|
|
advanced: {
|
|
|
|
last_seen: 'epoch',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(typeof publishEntityState.mock.calls[0][1].last_seen).toEqual('number');
|
2019-01-18 12:31:55 -07:00
|
|
|
});
|
|
|
|
|
2019-02-08 16:28:59 -07:00
|
|
|
it('Should publish last_seen ISO_8601', () => {
|
2019-01-18 12:31:55 -07:00
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'get').mockImplementation(() => {
|
2019-01-18 12:31:55 -07:00
|
|
|
return {
|
|
|
|
advanced: {
|
|
|
|
last_seen: 'ISO_8601',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(typeof publishEntityState.mock.calls[0][1].last_seen).toEqual('string');
|
2019-01-18 12:31:55 -07:00
|
|
|
});
|
2019-03-02 08:47:36 -07:00
|
|
|
|
|
|
|
it('Should publish last_seen ISO_8601_local', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
2019-03-09 15:34:19 -07:00
|
|
|
jest.spyOn(settings, 'get').mockImplementation(() => {
|
2019-03-02 08:47:36 -07:00
|
|
|
return {
|
|
|
|
advanced: {
|
|
|
|
last_seen: 'ISO_8601_local',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
2019-03-09 15:34:19 -07:00
|
|
|
expect(publishEntityState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(typeof publishEntityState.mock.calls[0][1].last_seen).toEqual('string');
|
2019-03-02 08:47:36 -07:00
|
|
|
});
|
2018-11-26 09:47:44 -07:00
|
|
|
});
|
|
|
|
});
|