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-09-09 10:48:09 -07:00
|
|
|
if (device.interviewing || !device.interviewCompleted) {
|
|
|
|
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() {
|
|
|
|
this.coordinatorEndpoint = (await this.zigbee.getDevice({type: 'Coordinator'})).endpoints[0];
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const devices = await this.zigbee.getClients();
|
|
|
|
for (const device of devices) {
|
|
|
|
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;
|
|
|
|
await device.save();
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to configure '${settingsDevice.friendlyName}', ` +
|
|
|
|
`attempt ${this.attempts[device.ieeeAddr] + 1} (${error})`
|
|
|
|
);
|
|
|
|
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;
|