mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-28 23:00:55 +00:00
Refactor publishDeviceState to publishEntityState (for future group support).
This commit is contained in:
+32
-30
@@ -5,6 +5,7 @@ const logger = require('./util/logger');
|
||||
const settings = require('./util/settings');
|
||||
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
|
||||
const objectAssignDeep = require('object-assign-deep');
|
||||
const utils = require('./util/utils');
|
||||
|
||||
// Extensions
|
||||
const ExtensionNetworkMap = require('./extension/networkMap');
|
||||
@@ -33,40 +34,40 @@ class Controller {
|
||||
this.onMQTTConnected = this.onMQTTConnected.bind(this);
|
||||
this.onZigbeeMessage = this.onZigbeeMessage.bind(this);
|
||||
this.onMQTTMessage = this.onMQTTMessage.bind(this);
|
||||
this.publishDeviceState = this.publishDeviceState.bind(this);
|
||||
this.publishEntityState = this.publishEntityState.bind(this);
|
||||
|
||||
// Initialize extensions.
|
||||
this.extensions = [
|
||||
new ExtensionDeviceReceive(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionDeviceConfigure(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionDevicePublish(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionNetworkMap(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionRouterPollXiaomi(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionMarkOnlineXiaomi(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionBridgeConfig(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionGroups(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionBind(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionCoordinatorGroup(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionReporting(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
new ExtensionDeviceReceive(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionDeviceConfigure(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionDevicePublish(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionNetworkMap(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionRouterPollXiaomi(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionMarkOnlineXiaomi(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionBridgeConfig(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionGroups(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionBind(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionCoordinatorGroup(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
new ExtensionReporting(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
// https://github.com/Koenkk/zigbee2mqtt/issues/592
|
||||
// new ExtensionPollLivoloSwitch(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
|
||||
// new ExtensionPollLivoloSwitch(this.zigbee, this.mqtt, this.state, this.publishEntityState),
|
||||
];
|
||||
|
||||
if (settings.get().homeassistant) {
|
||||
this.extensions.push(new ExtensionHomeAssistant(
|
||||
this.zigbee, this.mqtt, this.state, this.publishDeviceState
|
||||
this.zigbee, this.mqtt, this.state, this.publishEntityState
|
||||
));
|
||||
}
|
||||
|
||||
if (settings.get().advanced.soft_reset_timeout !== 0) {
|
||||
this.extensions.push(new ExtensionSoftReset(
|
||||
this.zigbee, this.mqtt, this.state, this.publishDeviceState
|
||||
this.zigbee, this.mqtt, this.state, this.publishEntityState
|
||||
));
|
||||
}
|
||||
|
||||
if (settings.get().advanced.availability_timeout) {
|
||||
this.extensions.push(new ExtensionAvailability(
|
||||
this.zigbee, this.mqtt, this.state, this.publishDeviceState
|
||||
this.zigbee, this.mqtt, this.state, this.publishEntityState
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -215,44 +216,45 @@ class Controller {
|
||||
sendAllCachedStates() {
|
||||
this.zigbee.getAllClients().forEach((device) => {
|
||||
if (this.state.exists(device.ieeeAddr)) {
|
||||
this.publishDeviceState(device, this.state.get(device.ieeeAddr), false);
|
||||
this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
publishDeviceState(device, payload, cache) {
|
||||
const deviceID = device.ieeeAddr;
|
||||
publishEntityState(entityID, payload, cache) {
|
||||
const entity = utils.resolveEntity(entityID);
|
||||
const appSettings = settings.get();
|
||||
let messagePayload = {...payload};
|
||||
|
||||
if (appSettings.advanced.cache_state) {
|
||||
// Add cached state to payload
|
||||
if (this.state.exists(deviceID)) {
|
||||
messagePayload = objectAssignDeep.noMutate(this.state.get(deviceID), payload);
|
||||
if (this.state.exists(entityID)) {
|
||||
messagePayload = objectAssignDeep.noMutate(this.state.get(entityID), payload);
|
||||
}
|
||||
|
||||
// Update state cache with new state.
|
||||
if (cache) {
|
||||
this.state.set(deviceID, messagePayload);
|
||||
this.state.set(entityID, messagePayload);
|
||||
}
|
||||
}
|
||||
|
||||
const deviceSettings = settings.getDevice(deviceID);
|
||||
const friendlyName = deviceSettings ? deviceSettings.friendly_name : deviceID;
|
||||
const entitySettings = entity.type === 'device' ? settings.getDevice(entityID) : settings.getGroup(entityID);
|
||||
const friendlyName = entitySettings ? entitySettings.friendly_name : entityID;
|
||||
const options = {
|
||||
retain: deviceSettings ? deviceSettings.retain : false,
|
||||
qos: deviceSettings && deviceSettings.qos ? deviceSettings.qos : 0,
|
||||
retain: entitySettings ? entitySettings.retain : false,
|
||||
qos: entitySettings && entitySettings.qos ? entitySettings.qos : 0,
|
||||
};
|
||||
|
||||
if (appSettings.mqtt.include_device_information) {
|
||||
messagePayload.device = this.getDeviceInfoForMqtt(device);
|
||||
if (entity.type === 'device' && appSettings.mqtt.include_device_information) {
|
||||
messagePayload.device = this.getDeviceInfoForMqtt(entityID);
|
||||
}
|
||||
|
||||
this.mqtt.publish(friendlyName, JSON.stringify(messagePayload), options);
|
||||
}
|
||||
|
||||
getDeviceInfoForMqtt(device) {
|
||||
const {type, ieeeAddr, nwkAddr, manufId, manufName, powerSource, modelId, status} = device;
|
||||
getDeviceInfoForMqtt(ieeeAddr) {
|
||||
const device = this.zigbee.getDevice(ieeeAddr);
|
||||
const {type, nwkAddr, manufId, manufName, powerSource, modelId, status} = device;
|
||||
const deviceSettings = settings.getDevice(device.ieeeAddr);
|
||||
|
||||
return {
|
||||
|
||||
@@ -15,7 +15,7 @@ const toZigbeeCandidates = ['state'];
|
||||
* This extensions pings devices to check if they are online.
|
||||
*/
|
||||
class Availability {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.availability_timeout = settings.get().advanced.availability_timeout;
|
||||
|
||||
@@ -13,11 +13,11 @@ const allowedClusters = [
|
||||
];
|
||||
|
||||
class Bind {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
|
||||
// Setup queue
|
||||
this.queue = new Queue();
|
||||
|
||||
@@ -6,11 +6,11 @@ const configRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/
|
||||
const allowedLogLevels = ['error', 'warn', 'info', 'debug'];
|
||||
|
||||
class BridgeConfig {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
|
||||
// Bind functions
|
||||
this.permitJoin = this.permitJoin.bind(this);
|
||||
|
||||
@@ -11,11 +11,11 @@ const devices = [E1524];
|
||||
* This extensions adds the coordinator to a group which is required for some devices to work properly.
|
||||
*/
|
||||
class CoordinatorGroup {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
}
|
||||
|
||||
onZigbeeStarted() {
|
||||
|
||||
@@ -7,7 +7,7 @@ const Queue = require('queue');
|
||||
* This extensions handles configuration of devices.
|
||||
*/
|
||||
class DeviceConfigure {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.configured = [];
|
||||
this.attempts = {};
|
||||
|
||||
@@ -17,11 +17,11 @@ const groupConverters = [
|
||||
];
|
||||
|
||||
class DevicePublish {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
}
|
||||
|
||||
onMQTTConnected() {
|
||||
@@ -151,7 +151,7 @@ class DevicePublish {
|
||||
const msg = {};
|
||||
const _key = topic.postfix ? `state_${topic.postfix}` : 'state';
|
||||
msg[_key] = key.startsWith('brightness') ? 'ON' : json['state'];
|
||||
this.publishDeviceState(device, msg, true);
|
||||
this.publishEntityState(device.ieeeAddr, msg, true);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -7,11 +7,11 @@ const dontCacheProperties = ['action', 'button', 'button_left', 'button_right',
|
||||
* This extensions handles messages received from devices.
|
||||
*/
|
||||
class DeviceReceive {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
this.coordinator = null;
|
||||
this.elapsed = {};
|
||||
}
|
||||
@@ -132,7 +132,7 @@ class DeviceReceive {
|
||||
this.elapsed[device.ieeeAddr] = now;
|
||||
}
|
||||
|
||||
this.publishDeviceState(device, payload, cache);
|
||||
this.publishEntityState(device.ieeeAddr, payload, cache);
|
||||
};
|
||||
|
||||
let payload = {};
|
||||
|
||||
@@ -9,13 +9,13 @@ class ExtensionTemplate {
|
||||
* @param {Zigbee} zigbee Zigbee controller
|
||||
* @param {MQTT} mqtt MQTT controller
|
||||
* @param {State} state State controller
|
||||
* @param {Function} publishDeviceState Method to publish device state to MQTT.
|
||||
* @param {Function} publishEntityState Method to publish device state to MQTT.
|
||||
*/
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,11 +4,11 @@ const logger = require('../util/logger');
|
||||
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/group/.+/(remove|add|remove_all)$`);
|
||||
|
||||
class Groups {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
}
|
||||
|
||||
onMQTTConnected() {
|
||||
|
||||
@@ -487,11 +487,11 @@ const mapping = {
|
||||
* This extensions handles integration with HomeAssistant
|
||||
*/
|
||||
class HomeAssistant {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
this.zigbee2mqttVersion = zigbee2mqttVersion;
|
||||
|
||||
// A map of all discoverd devices
|
||||
@@ -593,7 +593,7 @@ class HomeAssistant {
|
||||
// Publish all device states.
|
||||
this.zigbee.getAllClients().forEach((device) => {
|
||||
if (this.state.exists(device.ieeeAddr)) {
|
||||
this.publishDeviceState(device, this.state.get(device.ieeeAddr), false);
|
||||
this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr), false);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ const utils = require('../util/utils');
|
||||
* This extensions marks Xiaomi devices as online.
|
||||
*/
|
||||
class MarkOnlineXiaomi {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ const settings = require('../util/settings');
|
||||
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
|
||||
|
||||
class NetworkMap {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
|
||||
@@ -8,7 +8,7 @@ const foundationCfg = {manufSpec: 0, disDefaultRsp: 0};
|
||||
* Extension required for Livolo device support.
|
||||
*/
|
||||
class PollLivoloSwitch {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.timer = null;
|
||||
this.configured = {};
|
||||
|
||||
@@ -15,11 +15,11 @@ const reportInterval = {
|
||||
const reportableChange = 0;
|
||||
|
||||
class Reporting {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.mqtt = mqtt;
|
||||
this.state = state;
|
||||
this.publishDeviceState = publishDeviceState;
|
||||
this.publishEntityState = publishEntityState;
|
||||
}
|
||||
|
||||
shouldReport(ieeeAddr) {
|
||||
|
||||
@@ -5,7 +5,7 @@ const interval = utils.secondsToMilliseconds(60);
|
||||
* This extensions polls Xiaomi Zigbee routers to keep them awake.
|
||||
*/
|
||||
class RouterPollXiaomi {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ const utils = require('../util/utils');
|
||||
* This extensions soft resets the ZNP after a certain timeout.
|
||||
*/
|
||||
class SoftReset {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
constructor(zigbee, mqtt, state, publishEntityState) {
|
||||
this.zigbee = zigbee;
|
||||
this.timer = null;
|
||||
this.timeout = utils.secondsToMilliseconds(settings.get().advanced.soft_reset_timeout);
|
||||
|
||||
@@ -125,6 +125,7 @@ module.exports = {
|
||||
write: () => write(),
|
||||
|
||||
getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null,
|
||||
getGroup: (ID) => settings.groups ? settings.groups[ID]: null,
|
||||
getDevices: () => settings.devices ? settings.devices : [],
|
||||
addDevice: (ieeeAddr) => addDevice(ieeeAddr),
|
||||
removeDevice: (ieeeAddr) => removeDevice(ieeeAddr),
|
||||
|
||||
@@ -17,6 +17,14 @@ describe('Controller', () => {
|
||||
});
|
||||
mqttPublish = sandbox.stub(mqtt.prototype, 'publish').callsFake(() => {});
|
||||
controller = new Controller();
|
||||
controller.zigbee = {
|
||||
getDevice: () => {
|
||||
return {
|
||||
modelId: 'TRADFRI bulb E27 CWS opal 600lm',
|
||||
manufName: 'IKEA',
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -54,7 +62,7 @@ describe('Controller', () => {
|
||||
chai.assert.strictEqual(
|
||||
mqttPublish.getCall(0).args[1],
|
||||
`{"state":"ON","device":{"ieeeAddr":"0x12345678","friendlyName":"test",` +
|
||||
`"modelId":"TRADFRI bulb E27 CWS opal 600lm"}}`
|
||||
`"manufName":"IKEA","modelId":"TRADFRI bulb E27 CWS opal 600lm"}}`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
+34
-34
@@ -18,13 +18,13 @@ const mqtt = {
|
||||
|
||||
describe('DeviceReceive', () => {
|
||||
let deviceReceive;
|
||||
let publishDeviceState;
|
||||
let publishEntityState;
|
||||
|
||||
beforeEach(() => {
|
||||
utils.stubLogger(sandbox);
|
||||
sandbox.stub(settings, 'addDevice').callsFake(() => {});
|
||||
publishDeviceState = sinon.spy();
|
||||
deviceReceive = new DeviceReceive(null, mqtt, null, publishDeviceState);
|
||||
publishEntityState = sinon.spy();
|
||||
deviceReceive = new DeviceReceive(null, mqtt, null, publishEntityState);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -36,24 +36,24 @@ describe('DeviceReceive', () => {
|
||||
const device = {ieeeAddr: '0x12345678'};
|
||||
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {click: 'single'});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {click: 'single'});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message which uses ep (left)', () => {
|
||||
const device = {ieeeAddr: '0x12345678'};
|
||||
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 1);
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {click: 'left'});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {click: 'left'});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message which uses ep (right)', () => {
|
||||
const device = {ieeeAddr: '0x12345678'};
|
||||
const message = utils.zigbeeMessage(device, 'genOnOff', 'attReport', {onOff: 1}, 2);
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {click: 'right'});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {click: 'right'});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with default precision', () => {
|
||||
@@ -62,8 +62,8 @@ describe('DeviceReceive', () => {
|
||||
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
||||
);
|
||||
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {temperature: -0.85});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {temperature: -0.85});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with 1 precision', () => {
|
||||
@@ -75,8 +75,8 @@ describe('DeviceReceive', () => {
|
||||
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
||||
);
|
||||
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {temperature: -0.8});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {temperature: -0.8});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with 0 precision', () => {
|
||||
@@ -88,8 +88,8 @@ describe('DeviceReceive', () => {
|
||||
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
||||
);
|
||||
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {temperature: -1});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {temperature: -1});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with 1 precision when set via device_options', () => {
|
||||
@@ -111,8 +111,8 @@ describe('DeviceReceive', () => {
|
||||
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
||||
);
|
||||
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {temperature: -0.8});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {temperature: -0.8});
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with 2 precision when overrides device_options', () => {
|
||||
@@ -136,44 +136,44 @@ describe('DeviceReceive', () => {
|
||||
device, 'msTemperatureMeasurement', 'attReport', {measuredValue: -85}, 1
|
||||
);
|
||||
deviceReceive.onZigbeeMessage(message, device, WSDCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], {temperature: -0.85});
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {temperature: -0.85});
|
||||
});
|
||||
|
||||
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);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
const expected = {battery: 100, voltage: 3010};
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], expected);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], expected);
|
||||
});
|
||||
|
||||
it('Should handle a zigbee message with voltage 2850', () => {
|
||||
const device = {ieeeAddr: '0x12345678'};
|
||||
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', {'65281': {'1': 2850}}, 1);
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
const expected = {battery: 35, voltage: 2850};
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], expected);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], expected);
|
||||
});
|
||||
|
||||
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);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
const expected = {battery: 14, voltage: 2650};
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], expected);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], expected);
|
||||
});
|
||||
|
||||
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);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
const expected = {battery: 0, voltage: 2000};
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], expected);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], expected);
|
||||
});
|
||||
|
||||
it('Should publish 1 message when converted twice', () => {
|
||||
@@ -183,9 +183,9 @@ describe('DeviceReceive', () => {
|
||||
};
|
||||
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', payload, 1);
|
||||
deviceReceive.onZigbeeMessage(message, device, RTCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
const expected = {'battery': 100, 'illuminance': 381, 'voltage': 3045};
|
||||
chai.assert.deepEqual(publishDeviceState.getCall(0).args[1], expected);
|
||||
chai.assert.deepEqual(publishEntityState.getCall(0).args[1], expected);
|
||||
});
|
||||
|
||||
it('Should publish no message when converted without result', () => {
|
||||
@@ -193,7 +193,7 @@ describe('DeviceReceive', () => {
|
||||
const payload = {'9999': {'1': 3045}};
|
||||
const message = utils.zigbeeMessage(device, 'genBasic', 'attReport', payload, 1);
|
||||
deviceReceive.onZigbeeMessage(message, device, RTCGQ11LM);
|
||||
chai.assert.isTrue(publishDeviceState.notCalled);
|
||||
chai.assert.isTrue(publishEntityState.notCalled);
|
||||
});
|
||||
|
||||
it('Should publish last_seen epoch', () => {
|
||||
@@ -207,8 +207,8 @@ describe('DeviceReceive', () => {
|
||||
};
|
||||
});
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.equal(typeof publishDeviceState.getCall(0).args[1].last_seen, 'number');
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.equal(typeof publishEntityState.getCall(0).args[1].last_seen, 'number');
|
||||
});
|
||||
|
||||
it('Should publish last_seen ISO_8601', () => {
|
||||
@@ -222,8 +222,8 @@ describe('DeviceReceive', () => {
|
||||
};
|
||||
});
|
||||
deviceReceive.onZigbeeMessage(message, device, WXKG02LM);
|
||||
chai.assert.isTrue(publishDeviceState.calledOnce);
|
||||
chai.assert.equal(typeof publishDeviceState.getCall(0).args[1].last_seen, 'string');
|
||||
chai.assert.isTrue(publishEntityState.calledOnce);
|
||||
chai.assert.equal(typeof publishEntityState.getCall(0).args[1].last_seen, 'string');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user