2018-11-16 12:23:11 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
|
|
|
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This extensions handles configuration of devices.
|
|
|
|
*/
|
|
|
|
class DeviceConfigure {
|
|
|
|
constructor(zigbee, mqtt, state, publishDeviceState) {
|
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.configured = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeStarted() {
|
|
|
|
this.zigbee.getAllClients().forEach((device) => {
|
|
|
|
const mappedDevice = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
|
|
|
|
|
|
|
|
if (mappedDevice) {
|
|
|
|
this.configure(device, mappedDevice);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeMessage(message, device, mappedDevice) {
|
|
|
|
if (device && mappedDevice) {
|
2019-01-31 13:32:18 -07:00
|
|
|
// endDeviceAnnce is typically send when a device comes online after being
|
|
|
|
// powered off. In some cases this requires a re-configure of the device.
|
|
|
|
// Mark this device as not configured.
|
|
|
|
// https://github.com/Koenkk/zigbee2mqtt/issues/966
|
|
|
|
if (message.type === 'endDeviceAnnce') {
|
|
|
|
this.mark(device.ieeeAddr, false);
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
this.configure(device, mappedDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:32:18 -07:00
|
|
|
mark(ieeeAddr, configured) {
|
|
|
|
if (configured) {
|
|
|
|
if (!this.configured.includes(ieeeAddr)) {
|
|
|
|
this.configured.push(ieeeAddr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const index = this.configured.indexOf(ieeeAddr);
|
|
|
|
if (index > -1) {
|
|
|
|
this.configured.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
configure(device, mappedDevice) {
|
|
|
|
const ieeeAddr = device.ieeeAddr;
|
|
|
|
|
|
|
|
if (!this.configured.includes(ieeeAddr) && mappedDevice.configure) {
|
|
|
|
const friendlyName = settings.getDevice(ieeeAddr) ? settings.getDevice(ieeeAddr).friendly_name : 'unknown';
|
2019-01-07 10:18:14 -07:00
|
|
|
logger.debug(`Configuring ${friendlyName} (${ieeeAddr}) ...`);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
// Call configure function of this device.
|
|
|
|
mappedDevice.configure(ieeeAddr, this.zigbee.shepherd, this.zigbee.getCoordinator(), (ok, msg) => {
|
|
|
|
if (ok) {
|
2019-01-07 10:18:14 -07:00
|
|
|
logger.info(`Succesfully configured ${friendlyName} (${ieeeAddr})`);
|
2018-11-16 12:23:11 -07:00
|
|
|
} else {
|
2019-01-07 10:18:14 -07:00
|
|
|
logger.error(`Failed to configure ${friendlyName} (${ieeeAddr}) ('${msg}')`);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark as configured
|
2019-01-31 13:32:18 -07:00
|
|
|
this.mark(ieeeAddr, true);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DeviceConfigure;
|