2019-09-09 10:48:09 -07:00
|
|
|
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
|
2019-02-26 12:21:35 -07:00
|
|
|
const logger = require('../util/logger');
|
2019-09-09 10:48:09 -07:00
|
|
|
const CC2530Router = zigbeeHerdsmanConverters.devices.find((d) => d.model === 'CC2530.ROUTER');
|
2019-04-16 08:56:28 -07:00
|
|
|
const utils = require('../util/utils');
|
2019-02-01 11:04:49 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const defaultConfiguration = {
|
|
|
|
minimumReportInterval: 3, maximumReportInterval: 300, reportableChange: 0,
|
2019-02-01 11:04:49 -07:00
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const reportKey = 1;
|
|
|
|
|
|
|
|
const clusters = {
|
|
|
|
'genOnOff': [
|
|
|
|
{attribute: 'onOff', ...defaultConfiguration, minimumReportInterval: 0, reportableChange: 0},
|
|
|
|
],
|
|
|
|
'genLevelCtrl': [
|
|
|
|
{attribute: 'currentLevel', ...defaultConfiguration},
|
|
|
|
],
|
|
|
|
'lightingColorCtrl': [
|
|
|
|
{attribute: 'colorTemperature', ...defaultConfiguration},
|
|
|
|
{attribute: 'currentX', ...defaultConfiguration},
|
|
|
|
{attribute: 'currentY', ...defaultConfiguration},
|
|
|
|
],
|
|
|
|
'closuresWindowCovering': [
|
|
|
|
{attribute: 'currentPositionLiftPercentage', ...defaultConfiguration},
|
|
|
|
{attribute: 'currentPositionTiltPercentage', ...defaultConfiguration},
|
|
|
|
],
|
2019-02-01 11:04:49 -07:00
|
|
|
};
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
class DeviceReport {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2019-02-01 11:04:49 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.mqtt = mqtt;
|
|
|
|
this.state = state;
|
2019-02-04 10:36:49 -07:00
|
|
|
this.publishEntityState = publishEntityState;
|
2019-09-09 10:48:09 -07:00
|
|
|
this.configuring = new Set();
|
2019-02-01 11:04:49 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async setupReporting(device) {
|
|
|
|
if (this.configuring.has(device.ieeeAddr)) return;
|
|
|
|
this.configuring.add(device.ieeeAddr);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (const endpoint of device.endpoints) {
|
|
|
|
for (const [cluster, configuration] of Object.entries(clusters)) {
|
|
|
|
if (endpoint.supportsInputCluster(cluster)) {
|
|
|
|
logger.debug(`Setup reporting for '${device.ieeeAddr}' - ${endpoint.ID} - ${cluster}`);
|
|
|
|
await endpoint.bind(cluster, this.coordinatorEndpoint);
|
|
|
|
await endpoint.configureReporting(cluster, configuration);
|
|
|
|
logger.info(
|
|
|
|
`Succesfully setup reporting for '${device.ieeeAddr}' - ${endpoint.ID} - ${cluster}`
|
|
|
|
);
|
|
|
|
}
|
2019-02-26 12:21:35 -07:00
|
|
|
}
|
2019-02-01 11:04:49 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// eslint-disable-next-line
|
|
|
|
device.meta.reporting = reportKey;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to setup reporting for '${device.ieeeAddr}' - ${error}`
|
|
|
|
);
|
|
|
|
}
|
2019-02-22 13:06:24 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
await device.save();
|
|
|
|
this.configuring.delete(device.ieeeAddr);
|
2019-02-01 11:04:49 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
shouldSetupReporting(mappedDevice, device, messageType) {
|
|
|
|
if (!device) return false;
|
|
|
|
|
2019-02-26 12:21:35 -07:00
|
|
|
// Handle messages of type endDeviceAnnce and devIncoming.
|
2019-02-01 11:04:49 -07:00
|
|
|
// This message is typically send when a device comes online after being powered off
|
|
|
|
// Ikea TRADFRI tend to forget their reporting after powered off.
|
|
|
|
// Re-setup reporting.
|
|
|
|
// https://github.com/Koenkk/zigbee2mqtt/issues/966
|
2019-09-09 10:48:09 -07:00
|
|
|
if (messageType === 'deviceAnnounce' && utils.isIkeaTradfriDevice(device)) return true;
|
|
|
|
|
|
|
|
if (device.meta.hasOwnProperty('reporting') && device.meta.reporting === reportKey) return false;
|
|
|
|
if (!utils.isRouter(device) || utils.isBatteryPowered(device)) return false;
|
|
|
|
if (mappedDevice === CC2530Router) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onZigbeeStarted() {
|
|
|
|
this.coordinatorEndpoint = (await this.zigbee.getDevice({type: 'Coordinator'})).endpoints[0];
|
|
|
|
|
|
|
|
for (const device of await this.zigbee.getClients()) {
|
|
|
|
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
|
|
|
|
if (this.shouldSetupReporting(mappedDevice, device, null)) {
|
|
|
|
this.setupReporting(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeEvent(type, data, mappedDevice, settingsDevice) {
|
|
|
|
if (this.shouldSetupReporting(mappedDevice, data.device, type)) {
|
|
|
|
this.setupReporting(data.device);
|
2019-02-01 11:04:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
module.exports = DeviceReport;
|