zigbee2mqtt/lib/extension/deviceConfigure.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

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) {
this.configure(device, mappedDevice);
}
}
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
this.mark(ieeeAddr, true);
2018-11-16 12:23:11 -07:00
}
}
}
module.exports = DeviceConfigure;