zigbee2mqtt/test/controller.test.js
Gergely Markics 5d3461c4c6 Add the timestamp of last received zigbee message visible in HASS (#739)
* Add timestamp on receiving message from Zigbee

Add last_message to the payload

* Discovery on HASS restart and last_message attribute added

- On restarting Home Assistant, resending device discovery information
- Add timestamp on receiving message from Zigbee

* Add option: add_timestamp in settings

* Add option: add_timestamp in settings

* Add option: add_timestamp in settings

* typo

* Update homeassistant.js

* Update homeassistant.js

* Update homeassistant.js

* Update deviceReceive.js

* Update deviceReceive.js

* Update homeassistant.js

* Update settings.js

* Update deviceReceive.js

* Update deviceReceive.js

* Update deviceReceive.js

* Update deviceReceive.js

* Fix tests

* Fix lint

* Fix lint
2018-12-26 17:33:39 +01:00

62 lines
2.3 KiB
JavaScript

const chai = require('chai');
const sinon = require('sinon');
const Controller = require('../lib/controller');
const settings = require('../lib/util/settings');
const mqtt = require('../lib/mqtt');
const utils = require('./utils');
const sandbox = sinon.createSandbox();
describe('Controller', () => {
let controller;
let mqttPublish;
beforeEach(() => {
utils.stubLogger(sandbox);
sandbox.stub(settings, 'getDevice').callsFake((ieeeAddr) => {
return {friendly_name: 'test'};
});
mqttPublish = sandbox.stub(mqtt.prototype, 'publish').callsFake(() => {});
controller = new Controller();
});
afterEach(() => {
sandbox.restore();
});
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);
controller.onZigbeeMessage(message);
chai.assert.isTrue(mqttPublish.calledOnce);
chai.assert.strictEqual(
utils.withoutLastSeen(mqttPublish.getCall(0).args[1]),
JSON.stringify({state: 'ON'})
);
});
it('Should handle a zigbee message when include_device_information is set', () => {
sandbox.stub(settings, 'get').callsFake(() => {
return {
mqtt: {
include_device_information: true,
},
advanced: {
cache_state: false,
},
};
});
const device = {ieeeAddr: '0x12345678', modelId: 'TRADFRI bulb E27 CWS opal 600lm'};
const message = utils.zigbeeMessage(device, 'genOnOff', 'devChange', {onOff: 1}, 1);
controller.onZigbeeMessage(message);
chai.assert.isTrue(mqttPublish.calledOnce);
chai.assert.strictEqual(
utils.withoutLastSeen(mqttPublish.getCall(0).args[1]),
`{"state":"ON","device":{"ieeeAddr":"0x12345678","friendlyName":"test",` +
`"modelId":"TRADFRI bulb E27 CWS opal 600lm"}}`
);
});
});
});