2018-11-16 12:23:11 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
/**
|
|
|
|
* This extension calls the zigbee-herdsman-converters definition configure() method
|
|
|
|
*/
|
|
|
|
class Configure extends Extension {
|
2020-01-09 13:47:19 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
2019-09-17 09:32:16 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.configuring = new Set();
|
2019-02-02 12:09:20 -07:00
|
|
|
this.attempts = {};
|
2020-04-11 11:58:22 -07:00
|
|
|
|
|
|
|
this.legacyApi = settings.get().advanced.legacy_api;
|
|
|
|
this.legacyTopic = `${settings.get().mqtt.base_topic}/bridge/configure`;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
shouldConfigure(resolvedEntity) {
|
|
|
|
if (!resolvedEntity || !resolvedEntity.definition || !resolvedEntity.definition.configure) {
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
if (resolvedEntity.device.meta &&
|
|
|
|
resolvedEntity.device.meta.hasOwnProperty('configured') &&
|
|
|
|
resolvedEntity.device.meta.configured === resolvedEntity.definition.meta.configureKey) {
|
2019-09-09 10:48:09 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
if (resolvedEntity.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-11-29 01:43:59 -07:00
|
|
|
onMQTTConnected() {
|
2020-04-11 11:58:22 -07:00
|
|
|
this.mqtt.subscribe(this.legacyTopic);
|
2019-11-29 01:43:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async onMQTTMessage(topic, message) {
|
2020-04-11 11:58:22 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (this.legacyApi) {
|
|
|
|
if (topic !== this.legacyTopic) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-29 01:43:59 -07:00
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(message);
|
|
|
|
if (!resolvedEntity || resolvedEntity.type !== 'device') {
|
|
|
|
logger.error(`Device '${message}' does not exist`);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-29 01:43:59 -07:00
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
if (!resolvedEntity.definition || !resolvedEntity.definition.configure) {
|
|
|
|
logger.warn(`Skipping configure of '${resolvedEntity.name}', device does not require this.`);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-23 12:48:37 -07:00
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
this.configure(resolvedEntity, true);
|
|
|
|
}
|
2019-11-29 01:43:59 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onZigbeeStarted() {
|
2019-11-18 09:54:28 -07:00
|
|
|
this.coordinatorEndpoint = this.zigbee.getDevicesByType('Coordinator')[0].getEndpoint(1);
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2019-09-23 13:21:27 -07:00
|
|
|
for (const device of this.zigbee.getClients()) {
|
2020-04-05 06:41:24 -07:00
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(device);
|
|
|
|
if (this.shouldConfigure(resolvedEntity)) {
|
|
|
|
await this.configure(resolvedEntity);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
onZigbeeEvent(type, data, resolvedEntity) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const device = data.device;
|
2019-11-20 11:06:04 -07:00
|
|
|
|
|
|
|
if (type === 'deviceJoined' && device.meta.hasOwnProperty('configured')) {
|
|
|
|
delete device.meta.configured;
|
|
|
|
device.save();
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
if (this.shouldConfigure(resolvedEntity)) {
|
|
|
|
this.configure(resolvedEntity);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-02 12:09:20 -07:00
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
async configure(resolvedEntity, force=false) {
|
|
|
|
const device = resolvedEntity.device;
|
2019-11-29 01:43:59 -07:00
|
|
|
if (this.configuring.has(device.ieeeAddr) || (this.attempts[device.ieeeAddr] >= 3 && !force)) {
|
2019-09-09 10:48:09 -07:00
|
|
|
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
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
logger.info(`Configuring '${resolvedEntity.name}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
2020-04-05 06:41:24 -07:00
|
|
|
await resolvedEntity.definition.configure(device, this.coordinatorEndpoint);
|
|
|
|
logger.info(`Successfully configured '${resolvedEntity.name}'`);
|
|
|
|
device.meta.configured = resolvedEntity.definition.meta.configureKey;
|
2019-10-01 11:22:47 -07:00
|
|
|
device.save();
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
2020-04-05 06:41:24 -07:00
|
|
|
`Failed to configure '${resolvedEntity.name}', ` +
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
module.exports = Configure;
|