zigbee2mqtt/lib/extension/deviceReport.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
2019-02-26 12:21:35 -07:00
const logger = require('../util/logger');
2019-02-01 11:04:49 -07:00
const candidates = {
'genOnOff': {
attrs: ['onOff'],
reportIntervalMin: 3,
reportIntervalMax: 300,
reportableChange: 0,
},
'genLevelCtrl': {
attrs: ['currentLevel'],
},
'lightingColorCtrl': {
attrs: ['colorTemperature', 'currentX', 'currentY'],
},
2019-02-01 11:04:49 -07:00
};
const reportInterval = {
min: 3,
2019-02-01 11:04:49 -07:00
max: 3600,
};
const reportableChange = 0;
2019-02-13 12:55:14 -07:00
class DeviceReport {
constructor(zigbee, mqtt, state, publishEntityState) {
2019-02-01 11:04:49 -07:00
this.zigbee = zigbee;
this.mqtt = mqtt;
this.state = state;
this.publishEntityState = publishEntityState;
2019-02-01 11:04:49 -07:00
}
2019-02-26 12:21:35 -07:00
setupReporting(mappedDevice, device) {
let epId = null;
// Check if this device uses a different epId.
if (mappedDevice.hasOwnProperty('ep')) {
const eps = mappedDevice.ep(device);
epId = eps[''] || null;
}
const endpoint = this.zigbee.getEndpoint(device.ieeeAddr, epId);
if (!endpoint) {
logger.error(`Failed to setup reporting for ${device.ieeeAddr}, endpoint not found`);
return;
}
logger.debug(`Setting up reporting for ${device.ieeeAddr}`);
2019-02-01 11:04:49 -07:00
Object.values(endpoint.clusters).filter((c) => c).forEach((c) => {
const cluster = c.attrs.cid;
if (candidates[cluster]) {
const candidate = candidates[cluster];
const attributeNames = candidate.attrs.filter((a) => c.attrs.hasOwnProperty(a));
2019-02-26 12:21:35 -07:00
const attributes = attributeNames.map((attribute) => {
return {
attr: attribute,
min: candidate.hasOwnProperty('reportIntervalMin')?
candidate.reportIntervalMin:reportInterval.min,
max: candidate.hasOwnProperty('reportIntervalMax')?
candidate.reportIntervalMax:reportInterval.max,
change: candidate.hasOwnProperty('reportableChange')?
candidate.reportableChange:reportableChange,
2019-02-26 12:21:35 -07:00
};
2019-02-01 11:04:49 -07:00
});
2019-02-26 12:21:35 -07:00
if (attributes.length > 0) {
this.zigbee.report(endpoint, cluster, attributes);
}
2019-02-01 11:04:49 -07:00
}
});
}
onZigbeeStarted() {
this.zigbee.getAllClients().forEach((device) => {
const mappedDevice = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
2019-02-26 12:21:35 -07:00
if (mappedDevice) {
this.setupReporting(mappedDevice, device);
}
});
2019-02-01 11:04:49 -07:00
}
onZigbeeMessage(message, device, mappedDevice) {
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-02-26 12:21:35 -07:00
if (device && mappedDevice && ['endDeviceAnnce', 'devIncoming'].includes(message.type)) {
this.setupReporting(mappedDevice, device);
2019-02-01 11:04:49 -07:00
}
}
}
2019-02-13 12:55:14 -07:00
module.exports = DeviceReport;