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');
|
2020-09-24 09:06:43 -07:00
|
|
|
const stringify = require('json-stable-stringify-without-jsonify');
|
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
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
if (!resolvedEntity.definition) {
|
2020-07-22 05:00:27 -07:00
|
|
|
if (data.device.interviewing) {
|
|
|
|
logger.debug(`Skipping message, definition is undefined and still interviewing`);
|
|
|
|
} else {
|
2020-11-11 10:27:20 -07:00
|
|
|
logger.warn(
|
|
|
|
`Received message from unsupported device with Zigbee model '${data.device.modelID}' ` +
|
|
|
|
`and manufacturer name '${data.device.manufacturerName}'`);
|
2020-07-22 05:00:27 -07:00
|
|
|
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-08-23 13:05:48 -07:00
|
|
|
/**
|
|
|
|
* Handling of re-transmitted Xiaomi messages.
|
|
|
|
* https://github.com/Koenkk/zigbee2mqtt/issues/1238
|
|
|
|
* https://github.com/Koenkk/zigbee2mqtt/issues/3592
|
|
|
|
*
|
|
|
|
* Some Xiaomi router devices re-transmit messages from Xiaomi end devices.
|
|
|
|
* The network 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 network address of the end device.
|
|
|
|
*/
|
|
|
|
if (type === 'message' && utils.isXiaomiDevice(data.device) && data.device.type === 'Router' && data.groupID) {
|
|
|
|
logger.debug('Handling re-transmitted Xiaomi message');
|
|
|
|
data.device = this.zigbee.getDeviceByNetworkAddress(data.groupID);
|
|
|
|
resolvedEntity = this.zigbee.resolveEntity(data.device);
|
|
|
|
}
|
|
|
|
|
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.
|
2021-02-12 12:46:32 -07:00
|
|
|
if (!converters.length && !['genOta', 'genTime', 'genBasic'].includes(data.cluster)) {
|
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}' ` +
|
2020-08-13 11:00:35 -07:00
|
|
|
`and type '${data.type}' and data '${stringify(data.data)}'`,
|
2019-10-26 09:03:44 -07:00
|
|
|
);
|
2018-11-16 12:23:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) => {
|
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-08-17 11:57:12 -07:00
|
|
|
if (resolvedEntity.settings.debounce) {
|
|
|
|
this.publishDebounce(
|
|
|
|
data.device.ieeeAddr, payload, resolvedEntity.settings.debounce,
|
|
|
|
resolvedEntity.settings.debounce_ignore,
|
|
|
|
);
|
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-08-20 12:54:59 -07:00
|
|
|
const meta = {device: data.device, logger, state: this.state.get(data.device.ieeeAddr)};
|
2019-01-11 14:38:16 -07:00
|
|
|
let payload = {};
|
|
|
|
converters.forEach((converter) => {
|
2020-08-17 11:57:12 -07:00
|
|
|
const converted = converter.convert(
|
|
|
|
resolvedEntity.definition, data, publish, resolvedEntity.settings, 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;
|