2018-11-16 12:23:11 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2019-09-09 10:48:09 -07:00
|
|
|
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
|
2019-09-17 09:32:16 -07:00
|
|
|
const BaseExtension = require('./baseExtension');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-09-17 09:32:16 -07:00
|
|
|
class DeviceConfigure extends BaseExtension {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2019-09-17 09:32:16 -07:00
|
|
|
super(zigbee, mqtt, state, publishEntityState);
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.configuring = new Set();
|
2019-02-02 12:09:20 -07:00
|
|
|
this.attempts = {};
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
shouldConfigure(device, mappedDevice) {
|
|
|
|
if (!device) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (device.meta.hasOwnProperty('configured') && device.meta.configured === mappedDevice.meta.configureKey) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (!mappedDevice || !mappedDevice.configure) {
|
|
|
|
return false;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-10-17 09:36:55 -07:00
|
|
|
if (device.interviewing === true) {
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
2019-02-02 12:09:20 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onZigbeeStarted() {
|
2019-09-23 13:21:27 -07:00
|
|
|
this.coordinatorEndpoint = this.zigbee.getDevicesByType('Coordinator')[0].endpoints[0];
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-23 13:21:27 -07:00
|
|
|
for (const device of this.zigbee.getClients()) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
|
|
|
|
const settingsDevice = settings.getDevice(device.ieeeAddr);
|
|
|
|
if (this.shouldConfigure(device, mappedDevice)) {
|
|
|
|
await this.configure(device, mappedDevice, settingsDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeEvent(type, data, mappedDevice, settingsDevice) {
|
|
|
|
const device = data.device;
|
|
|
|
if (this.shouldConfigure(device, mappedDevice)) {
|
|
|
|
this.configure(device, mappedDevice, settingsDevice);
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async configure(device, mappedDevice, settingsDevice) {
|
|
|
|
if (this.configuring.has(device.ieeeAddr) || this.attempts[device.ieeeAddr] >= 3) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.configuring.add(device.ieeeAddr);
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (!this.attempts.hasOwnProperty(device.ieeeAddr)) {
|
|
|
|
this.attempts[device.ieeeAddr] = 0;
|
|
|
|
}
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.info(`Configuring '${settingsDevice.friendlyName}'`);
|
|
|
|
try {
|
|
|
|
await mappedDevice.configure(device, this.coordinatorEndpoint);
|
|
|
|
logger.info(`Succesfully configured '${settingsDevice.friendlyName}'`);
|
|
|
|
// eslint-disable-next-line
|
|
|
|
device.meta.configured = mappedDevice.meta.configureKey;
|
2019-10-01 11:22:47 -07:00
|
|
|
device.save();
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to configure '${settingsDevice.friendlyName}', ` +
|
2019-10-26 09:25:51 -07:00
|
|
|
`attempt ${this.attempts[device.ieeeAddr] + 1} (${error.stack})`,
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
|
|
|
this.attempts[device.ieeeAddr]++;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
this.configuring.delete(device.ieeeAddr);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DeviceConfigure;
|