zigbee2mqtt/lib/extension/deviceReport.js

92 lines
3.3 KiB
JavaScript
Raw Normal View History

const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
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
}
setupReporting(endpoint) {
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));
const attributes = [];
attributeNames.forEach((attribute) => {
attributes.push({
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-01 11:04:49 -07:00
});
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);
if (mappedDevice && mappedDevice.report) {
2019-02-23 07:07:44 -07:00
const endpoint = (typeof(mappedDevice.report)=='number')
? this.zigbee.getEndpoint(device.ieeeAddr, mappedDevice.report)
: this.zigbee.getEndpoint(device.ieeeAddr);
this.setupReporting(endpoint);
}
});
2019-02-01 11:04:49 -07:00
}
onZigbeeMessage(message, device, mappedDevice) {
// Handle messages of type endDeviceAnnce.
// 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
// The mappedDevice.report property is now used as boolean
// the property might contain more information about how to
// configure reporting. For instance the endpointIds to use.
if (device && mappedDevice
&& (message.type=='endDeviceAnnce' || message.type=='devIncoming')
&& mappedDevice.report) {
2019-02-23 07:07:44 -07:00
const endpoint = (typeof(mappedDevice.report)=='number')
? this.zigbee.getEndpoint(device.ieeeAddr, mappedDevice.report)
: this.zigbee.getEndpoint(device.ieeeAddr);
2019-02-01 11:04:49 -07:00
if (endpoint) {
this.setupReporting(endpoint);
}
}
}
}
2019-02-13 12:55:14 -07:00
module.exports = DeviceReport;