2018-11-16 12:23:11 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2019-03-02 08:47:36 -07:00
|
|
|
const utils = require('../util/utils');
|
2019-03-16 15:41:46 -07:00
|
|
|
const debounce = require('debounce');
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-04-15 13:34:59 -07:00
|
|
|
class Receive extends Extension {
|
2020-01-09 13:47:19 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
2019-01-22 12:02:34 -07:00
|
|
|
this.elapsed = {};
|
2019-03-16 15:41:46 -07:00
|
|
|
this.debouncers = {};
|
2020-05-29 10:24:59 -07:00
|
|
|
this.eventBus.on('publishEntityState', (data) => this.onPublishEntityState(data));
|
2019-01-18 14:23:47 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onZigbeeStarted() {
|
2019-09-23 13:21:27 -07:00
|
|
|
this.coordinator = this.zigbee.getDevicesByType('Coordinator')[0];
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2020-05-29 10:24:59 -07:00
|
|
|
async onPublishEntityState(data) {
|
|
|
|
/**
|
|
|
|
* Prevent that outdated properties are being published.
|
|
|
|
* In case that e.g. the state is currently held back by a debounce and a new state is published
|
|
|
|
* remove it from the to be send debounced message.
|
|
|
|
*/
|
|
|
|
if (data.entity.type === 'device' && this.debouncers[data.entity.device.ieeeAddr] &&
|
|
|
|
data.stateChangeReason !== 'publishDebounce') {
|
|
|
|
for (const key of Object.keys(data.payload)) {
|
|
|
|
delete this.debouncers[data.entity.device.ieeeAddr].payload[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:21:23 -07:00
|
|
|
publishDebounce(ieeeAddr, payload, time, debounceIgnore) {
|
2019-03-16 15:41:46 -07:00
|
|
|
if (!this.debouncers[ieeeAddr]) {
|
|
|
|
this.debouncers[ieeeAddr] = {
|
|
|
|
payload: {},
|
|
|
|
publish: debounce(() => {
|
2020-05-29 10:24:59 -07:00
|
|
|
this.publishEntityState(ieeeAddr, this.debouncers[ieeeAddr].payload, 'publishDebounce');
|
2019-03-16 15:41:46 -07:00
|
|
|
this.debouncers[ieeeAddr].payload = {};
|
|
|
|
}, time * 1000),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:21:23 -07:00
|
|
|
if (this.isPayloadConflicted(payload, this.debouncers[ieeeAddr].payload, debounceIgnore)) {
|
|
|
|
// publish previous payload immediately
|
|
|
|
this.debouncers[ieeeAddr].publish.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// extend debounced payload with current
|
2019-03-16 15:41:46 -07:00
|
|
|
this.debouncers[ieeeAddr].payload = {...this.debouncers[ieeeAddr].payload, ...payload};
|
|
|
|
this.debouncers[ieeeAddr].publish();
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:21:23 -07:00
|
|
|
// if debounce_ignore are specified (Array of strings)
|
|
|
|
// then all newPayload values with key present in debounce_ignore
|
|
|
|
// should equal or be undefined in oldPayload
|
|
|
|
// otherwise payload is conflicted
|
|
|
|
isPayloadConflicted(newPayload, oldPayload, debounceIgnore) {
|
|
|
|
let result = false;
|
|
|
|
Object.keys(oldPayload)
|
|
|
|
.filter((key) => (debounceIgnore || []).includes(key))
|
|
|
|
.forEach((key) => {
|
|
|
|
if (typeof newPayload[key] !== 'undefined' && newPayload[key] !== oldPayload[key]) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:34:59 -07:00
|
|
|
shouldProcess(type, data, resolvedEntity) {
|
2020-04-05 06:41:24 -07:00
|
|
|
if (type !== 'message' || !resolvedEntity) {
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (data.device.ieeeAddr === this.coordinator.ieeeAddr) {
|
2019-01-18 14:23:47 -07:00
|
|
|
logger.debug('Ignoring message from coordinator');
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-18 09:33:13 -07:00
|
|
|
/**
|
|
|
|
* Don't handle re-transmitted Xiaomi messages.
|
|
|
|
* https://github.com/Koenkk/zigbee2mqtt/issues/1238
|
|
|
|
*
|
|
|
|
* Some Xiaomi router devices re-transmit messages from Xiaomi end devices.
|
|
|
|
* The source address of these message is set to the one of the Xiaomi router.
|
|
|
|
* Therefore it looks like if the message came from the Xiaomi router, while in
|
|
|
|
* fact it came from the end device.
|
|
|
|
* Handling these message would result in false state updates.
|
|
|
|
* The group ID attribute of these message defines the source address of the end device.
|
|
|
|
* As the same message is also received directly from the end device, it makes no sense
|
|
|
|
* to handle these messages.
|
|
|
|
*/
|
2020-05-22 09:25:32 -07:00
|
|
|
const hasGroupID = data.hasOwnProperty('groupID') && !!data.groupID;
|
2019-09-09 10:48:09 -07:00
|
|
|
if (utils.isXiaomiDevice(data.device) && utils.isRouter(data.device) && hasGroupID) {
|
2019-03-18 09:33:13 -07:00
|
|
|
logger.debug('Skipping re-transmitted Xiaomi message');
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
2019-03-18 09:33:13 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:44:47 -07:00
|
|
|
if (!data.device.modelID && data.device.interviewing) {
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.debug(`Skipping message, modelID is undefined and still interviewing`);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
if (!resolvedEntity.definition) {
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.warn(`Received message from unsupported device with Zigbee model '${data.device.modelID}'`);
|
|
|
|
logger.warn(`Please see: https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html.`);
|
2018-11-16 12:23:11 -07:00
|
|
|
return false;
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
onZigbeeEvent(type, data, resolvedEntity) {
|
2020-04-15 13:34:59 -07:00
|
|
|
if (!this.shouldProcess(type, data, resolvedEntity)) {
|
2019-09-09 10:48:09 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
const converters = resolvedEntity.definition.fromZigbee.filter((c) => {
|
2019-09-09 10:48:09 -07:00
|
|
|
const type = Array.isArray(c.type) ? c.type.includes(data.type) : c.type === data.type;
|
|
|
|
return c.cluster === data.cluster && type;
|
2018-11-16 12:23:11 -07:00
|
|
|
});
|
|
|
|
|
2020-04-13 08:18:45 -07:00
|
|
|
// Check if there is an available converter, genOta messages are not interesting.
|
2020-05-07 09:01:09 -07:00
|
|
|
if (!converters.length && data.cluster !== 'genOta' && data.cluster !== 'genTime') {
|
2019-12-17 11:19:57 -07:00
|
|
|
logger.debug(
|
2020-04-05 06:41:24 -07:00
|
|
|
`No converter available for '${resolvedEntity.definition.model}' with cluster '${data.cluster}' ` +
|
2019-10-26 09:25:51 -07:00
|
|
|
`and type '${data.type}' and data '${JSON.stringify(data.data)}'`,
|
2019-10-26 09:03:44 -07:00
|
|
|
);
|
2018-11-16 12:23:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
const debounce = resolvedEntity.settings.debounce || settings.get().device_options.debounce;
|
|
|
|
const debounceIgnore = resolvedEntity.settings.debounce_ignore || settings.get().device_options.debounce_ignore;
|
2020-02-27 13:06:27 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Convert this Zigbee message to a MQTT message.
|
|
|
|
// Get payload for the message.
|
|
|
|
// - If a payload is returned publish it to the MQTT broker
|
|
|
|
// - If NO payload is returned do nothing. This is for non-standard behaviour
|
|
|
|
// for e.g. click switches where we need to count number of clicks and detect long presses.
|
2019-01-11 14:38:16 -07:00
|
|
|
const publish = (payload) => {
|
|
|
|
// Add device linkquality.
|
2019-09-09 10:48:09 -07:00
|
|
|
if (data.hasOwnProperty('linkquality')) {
|
|
|
|
payload.linkquality = data.linkquality;
|
2019-01-11 14:38:16 -07:00
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-01-22 12:02:34 -07:00
|
|
|
if (settings.get().advanced.elapsed) {
|
2019-09-12 13:48:23 -07:00
|
|
|
const now = Date.now();
|
2019-09-09 10:48:09 -07:00
|
|
|
if (this.elapsed[data.device.ieeeAddr]) {
|
|
|
|
payload.elapsed = now - this.elapsed[data.device.ieeeAddr];
|
2019-01-22 12:02:34 -07:00
|
|
|
}
|
2019-01-22 12:22:16 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.elapsed[data.device.ieeeAddr] = now;
|
2019-01-22 12:02:34 -07:00
|
|
|
}
|
|
|
|
|
2019-03-16 15:41:46 -07:00
|
|
|
// Check if we have to debounce
|
2020-02-27 13:06:27 -07:00
|
|
|
if (debounce) {
|
|
|
|
this.publishDebounce(data.device.ieeeAddr, payload, debounce, debounceIgnore);
|
2019-03-16 15:41:46 -07:00
|
|
|
} else {
|
2019-09-09 10:48:09 -07:00
|
|
|
this.publishEntityState(data.device.ieeeAddr, payload);
|
2019-03-16 15:41:46 -07:00
|
|
|
}
|
2019-01-11 14:38:16 -07:00
|
|
|
};
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-06-13 03:55:01 -07:00
|
|
|
const meta = {device: data.device, logger};
|
2019-01-11 14:38:16 -07:00
|
|
|
let payload = {};
|
|
|
|
converters.forEach((converter) => {
|
2019-09-09 10:48:09 -07:00
|
|
|
const options = {...settings.get().device_options, ...settings.getDevice(data.device.ieeeAddr)};
|
2020-04-05 06:41:24 -07:00
|
|
|
const converted = converter.convert(resolvedEntity.definition, data, publish, options, meta);
|
2019-01-11 14:38:16 -07:00
|
|
|
if (converted) {
|
|
|
|
payload = {...payload, ...converted};
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
});
|
2019-01-11 14:38:16 -07:00
|
|
|
|
|
|
|
if (Object.keys(payload).length) {
|
|
|
|
publish(payload);
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:34:59 -07:00
|
|
|
module.exports = Receive;
|