zigbee2mqtt/test/controller.test.js

106 lines
3.8 KiB
JavaScript
Raw Normal View History

const assert = require('chai').assert;
2018-11-28 11:34:37 -07:00
const sinon = require('sinon');
const Controller = require('../lib/controller');
const settings = require('../lib/util/settings');
const mqtt = require('../lib/mqtt');
const utils = require('./utils');
describe('Controller', () => {
let controller;
let mqttPublish;
beforeEach(() => {
utils.stubLogger(sinon);
sinon.stub(settings, 'getDevice').callsFake((ieeeAddr) => {
2018-11-28 11:34:37 -07:00
return {friendly_name: 'test'};
});
mqttPublish = sinon.stub(mqtt.prototype, 'publish').callsFake(() => {});
2018-11-28 11:34:37 -07:00
controller = new Controller();
controller.zigbee = {
getDevice: () => {
return {
modelId: 'TRADFRI bulb E27 CWS opal 600lm',
manufName: 'IKEA',
};
},
};
2018-11-28 11:34:37 -07:00
});
afterEach(() => {
sinon.restore();
2018-11-28 11:34:37 -07:00
});
describe('Handling zigbee messages', () => {
it('Should handle a zigbee message', () => {
const device = {ieeeAddr: '0x12345678', modelId: 'TRADFRI bulb E27 CWS opal 600lm'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'devChange', {onOff: 1}, 1);
2018-11-28 11:34:37 -07:00
controller.onZigbeeMessage(message);
assert.isTrue(mqttPublish.calledOnce);
assert.strictEqual(
mqttPublish.getCall(0).args[1],
JSON.stringify({state: 'ON'})
);
2018-11-28 11:34:37 -07:00
});
it('Should handle a zigbee message when include_device_information is set', () => {
sinon.stub(settings, 'get').callsFake(() => {
2018-11-28 11:34:37 -07:00
return {
mqtt: {
include_device_information: true,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'json',
},
2018-11-28 11:34:37 -07:00
};
});
const device = {ieeeAddr: '0x12345678', modelId: 'TRADFRI bulb E27 CWS opal 600lm'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'devChange', {onOff: 1}, 1);
2018-11-28 11:34:37 -07:00
controller.onZigbeeMessage(message);
assert.isTrue(mqttPublish.calledOnce);
assert.strictEqual(
mqttPublish.getCall(0).args[1],
2018-11-28 11:34:37 -07:00
`{"state":"ON","device":{"ieeeAddr":"0x12345678","friendlyName":"test",` +
`"manufName":"IKEA","modelId":"TRADFRI bulb E27 CWS opal 600lm"}}`
2018-11-28 11:34:37 -07:00
);
});
it('Should output to json by default', () => {
const payload = {temperature: 1, humidity: 2};
controller.publishEntityState('0x12345678', payload);
assert.isTrue(mqttPublish.calledOnce);
assert.deepEqual(
JSON.parse(mqttPublish.getCall(0).args[1]),
payload
);
});
it('Should output to attribute', () => {
sinon.stub(settings, 'get').callsFake(() => {
return {
mqtt: {
include_device_information: false,
},
advanced: {
cache_state: false,
},
experimental: {
output: 'attribute',
},
};
});
const payload = {temperature: 1, humidity: 2};
controller.publishEntityState('0x12345678', payload);
assert.isTrue(mqttPublish.calledTwice);
assert.deepEqual(mqttPublish.getCall(0).args[0], 'test/temperature');
assert.deepEqual(mqttPublish.getCall(0).args[1], '1');
assert.deepEqual(mqttPublish.getCall(1).args[0], 'test/humidity');
assert.deepEqual(mqttPublish.getCall(1).args[1], '2');
});
2018-11-28 11:34:37 -07:00
});
});