2018-11-26 09:47:44 -07:00
|
|
|
const chai = require('chai');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
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');
|
|
|
|
const sandbox = sinon.createSandbox();
|
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');
|
2018-11-26 09:47:44 -07:00
|
|
|
|
|
|
|
const mqtt = {
|
|
|
|
log: () => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('DeviceReceive', () => {
|
|
|
|
let deviceReceive;
|
|
|
|
let publishDeviceState;
|
|
|
|
|
2018-11-27 13:50:55 -07:00
|
|
|
beforeEach(() => {
|
2018-11-28 11:34:37 -07:00
|
|
|
utils.stubLogger(sandbox);
|
|
|
|
sandbox.stub(settings, 'addDevice').callsFake(() => {});
|
2018-11-27 13:50:55 -07:00
|
|
|
publishDeviceState = sinon.spy();
|
|
|
|
deviceReceive = new DeviceReceive(null, mqtt, null, publishDeviceState);
|
|
|
|
});
|
|
|
|
|
2018-11-28 11:34:37 -07:00
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2018-11-26 09:47:44 -07:00
|
|
|
describe('Handling zigbee messages', () => {
|
|
|
|
it('Should handle a zigbee message', () => {
|
|
|
|
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);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-26 09:33:39 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {click: 'single'});
|
2018-11-26 09:47:44 -07:00
|
|
|
});
|
2018-11-27 13:50:55 -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);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-26 09:33:39 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {click: 'left'});
|
2018-11-27 13:50:55 -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);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-26 09:33:39 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {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);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-27 10:21:48 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {temperature: -0.85});
|
2018-12-15 14:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with 1 precision', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
sandbox.stub(settings, 'getDevice').callsFake(() => {
|
|
|
|
return {temperature_precision: 1};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-27 10:21:48 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {temperature: -0.8});
|
2018-12-15 14:02:23 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should handle a zigbee message with 0 precision', () => {
|
|
|
|
const device = {ieeeAddr: '0x12345678'};
|
|
|
|
sandbox.stub(settings, 'getDevice').callsFake(() => {
|
|
|
|
return {temperature_precision: 0};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-27 10:21:48 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {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'};
|
|
|
|
sandbox.stub(settings, 'get').callsFake(() => {
|
|
|
|
return {
|
|
|
|
device_options: {
|
|
|
|
temperature_precision: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
sandbox.stub(settings, 'getDevice').callsFake(() => {
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-27 10:21:48 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {temperature: -0.8});
|
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'};
|
|
|
|
sandbox.stub(settings, 'get').callsFake(() => {
|
|
|
|
return {
|
|
|
|
device_options: {
|
|
|
|
temperature_precision: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
sandbox.stub(settings, 'getDevice').callsFake(() => {
|
|
|
|
return {
|
|
|
|
temperature_precision: 2,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const message = utils.zigbeeMessage(
|
|
|
|
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
|
|
|
);
|
|
|
|
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
|
|
|
chai.assert.isTrue(publishDeviceState.calledOnce);
|
2018-12-27 10:21:48 -07:00
|
|
|
chai.assert.deepEqual(utils.withoutLastSeen(publishDeviceState.getCall(0).args[1]), {temperature: -0.85});
|
2018-12-19 09:33:02 -07:00
|
|
|
});
|
2018-11-26 09:47:44 -07:00
|
|
|
});
|
|
|
|
});
|