2018-11-16 12:23:11 -07:00
|
|
|
const settings = require('../util/settings');
|
2020-07-08 12:46:00 -07:00
|
|
|
const utils = require('../util/utils');
|
2018-11-16 12:23:11 -07:00
|
|
|
const logger = require('../util/logger');
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2020-09-24 09:06:43 -07:00
|
|
|
const stringify = require('json-stable-stringify-without-jsonify');
|
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();
|
2020-08-04 10:11:13 -07:00
|
|
|
this.onReportingDisabled = this.onReportingDisabled.bind(this);
|
2019-02-02 12:09:20 -07:00
|
|
|
this.attempts = {};
|
2020-04-11 11:58:22 -07:00
|
|
|
|
2020-07-08 12:46:00 -07:00
|
|
|
this.topic = `${settings.get().mqtt.base_topic}/bridge/request/device/configure`;
|
2020-04-11 11:58:22 -07:00
|
|
|
this.legacyTopic = `${settings.get().mqtt.base_topic}/bridge/configure`;
|
2020-08-04 10:11:13 -07:00
|
|
|
this.eventBus.on(`reportingDisabled`, this.onReportingDisabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
onReportingDisabled(data) {
|
|
|
|
// Disabling reporting unbinds some cluster which could be bound by configure, re-setup.
|
|
|
|
const device = data.device;
|
|
|
|
|
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(device);
|
|
|
|
if (resolvedEntity.device.meta && resolvedEntity.device.meta.hasOwnProperty('configured')) {
|
|
|
|
delete device.meta.configured;
|
|
|
|
device.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.shouldConfigure(resolvedEntity)) {
|
|
|
|
this.configure(resolvedEntity);
|
|
|
|
}
|
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-07-22 04:44:56 -07:00
|
|
|
if (resolvedEntity.device.interviewing === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-07-08 12:46:00 -07:00
|
|
|
async onMQTTMessage(topic, message) {
|
|
|
|
if (topic === this.legacyTopic) {
|
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);
|
2020-07-08 12:46:00 -07:00
|
|
|
} else if (topic === this.topic) {
|
|
|
|
message = utils.parseJSON(message, message);
|
2020-07-15 13:16:18 -07:00
|
|
|
const ID = typeof message === 'object' && message.hasOwnProperty('id') ? message.id : message;
|
2020-07-08 12:46:00 -07:00
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(ID);
|
|
|
|
if (!resolvedEntity || resolvedEntity.type !== 'device') {
|
|
|
|
error = `Device '${ID}' does not exist`;
|
|
|
|
} else if (!resolvedEntity.definition || !resolvedEntity.definition.configure) {
|
|
|
|
error = `Device '${resolvedEntity.name}' cannot be configured`;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await this.configure(resolvedEntity, true, true);
|
|
|
|
} catch (e) {
|
|
|
|
error = `Failed to configure (${e.message})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 13:16:18 -07:00
|
|
|
const response = utils.getResponse(message, {id: ID}, error);
|
2020-08-13 11:00:35 -07:00
|
|
|
await this.mqtt.publish(`bridge/response/device/configure`, stringify(response));
|
2020-04-11 11:58:22 -07:00
|
|
|
}
|
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-07-08 12:46:00 -07:00
|
|
|
async configure(resolvedEntity, force=false, thowError=false) {
|
2020-04-05 06:41:24 -07:00
|
|
|
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-12-28 13:48:07 -07:00
|
|
|
await resolvedEntity.definition.configure(device, this.coordinatorEndpoint, logger);
|
2020-04-05 06:41:24 -07:00
|
|
|
logger.info(`Successfully configured '${resolvedEntity.name}'`);
|
|
|
|
device.meta.configured = resolvedEntity.definition.meta.configureKey;
|
2019-10-01 11:22:47 -07:00
|
|
|
device.save();
|
2020-12-29 02:43:06 -07:00
|
|
|
this.eventBus.emit(`devicesChanged`);
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
this.attempts[device.ieeeAddr]++;
|
2020-07-08 12:46:00 -07:00
|
|
|
const attempt = this.attempts[device.ieeeAddr];
|
|
|
|
const msg = `Failed to configure '${resolvedEntity.name}', attempt ${attempt} (${error.stack})`;
|
|
|
|
logger.error(msg);
|
|
|
|
|
|
|
|
if (thowError) {
|
|
|
|
throw error;
|
|
|
|
}
|
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;
|