mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2024-11-17 10:58:31 -07:00
e5b2f71fcd
* fix potential null exception * Added support for Stelpro Ki * Stelpro Ki: Added support for action_mode (cherry picked from commit 0f740f1aedbedb28d64245531f10a9bfd985e49b) * Stelpro Ki: fix "lock" status update (cherry picked from commit 6ac94411183d92d1f658a7d17a724e50587b8571) * Fixed lint issues * Stelpro: Fixed local_temperature sensor * Added support for Stelpro Maestro * Fix lint * HASS: Fixed support for action_topic/action_template * Prevent a non supported device from causing a null-ref * Update naming. Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
117 lines
3.7 KiB
JavaScript
117 lines
3.7 KiB
JavaScript
const settings = require('../util/settings');
|
|
const logger = require('../util/logger');
|
|
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
|
|
const BaseExtension = require('./baseExtension');
|
|
|
|
class DeviceConfigure extends BaseExtension {
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
|
super(zigbee, mqtt, state, publishEntityState);
|
|
|
|
this.configuring = new Set();
|
|
this.attempts = {};
|
|
this.topic = `${settings.get().mqtt.base_topic}/bridge/configure`;
|
|
}
|
|
|
|
shouldConfigure(device, mappedDevice) {
|
|
if (!device || !mappedDevice) {
|
|
return false;
|
|
}
|
|
|
|
if (device.meta &&
|
|
device.meta.hasOwnProperty('configured') &&
|
|
device.meta.configured === mappedDevice.meta.configureKey) {
|
|
return false;
|
|
}
|
|
|
|
if (!mappedDevice || !mappedDevice.configure) {
|
|
return false;
|
|
}
|
|
|
|
if (device.interviewing === true) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
onMQTTConnected() {
|
|
this.mqtt.subscribe(this.topic);
|
|
}
|
|
|
|
async onMQTTMessage(topic, message) {
|
|
if (topic !== this.topic) {
|
|
return;
|
|
}
|
|
|
|
const entity = this.zigbee.resolveEntity(message);
|
|
if (!entity || !entity.type === 'device') {
|
|
logger.error(`Device '${message}' does not exist`);
|
|
return;
|
|
}
|
|
|
|
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(entity.device.modelID);
|
|
if (!mappedDevice.configure) {
|
|
logger.warn(`Skipping configure of '${entity.name}', device does not require this.`);
|
|
return;
|
|
}
|
|
|
|
this.configure(entity.device, mappedDevice, entity.settings, true);
|
|
}
|
|
|
|
async onZigbeeStarted() {
|
|
this.coordinatorEndpoint = this.zigbee.getDevicesByType('Coordinator')[0].getEndpoint(1);
|
|
|
|
for (const device of this.zigbee.getClients()) {
|
|
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 (type === 'deviceJoined' && device.meta.hasOwnProperty('configured')) {
|
|
delete device.meta.configured;
|
|
device.save();
|
|
}
|
|
|
|
if (this.shouldConfigure(device, mappedDevice)) {
|
|
this.configure(device, mappedDevice, settingsDevice);
|
|
}
|
|
}
|
|
|
|
async configure(device, mappedDevice, settingsDevice, force=false) {
|
|
if (this.configuring.has(device.ieeeAddr) || (this.attempts[device.ieeeAddr] >= 3 && !force)) {
|
|
return false;
|
|
}
|
|
|
|
this.configuring.add(device.ieeeAddr);
|
|
|
|
if (!this.attempts.hasOwnProperty(device.ieeeAddr)) {
|
|
this.attempts[device.ieeeAddr] = 0;
|
|
}
|
|
|
|
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;
|
|
device.save();
|
|
} catch (error) {
|
|
logger.error(
|
|
`Failed to configure '${settingsDevice.friendlyName}', ` +
|
|
`attempt ${this.attempts[device.ieeeAddr] + 1} (${error.stack})`,
|
|
);
|
|
this.attempts[device.ieeeAddr]++;
|
|
}
|
|
|
|
this.configuring.delete(device.ieeeAddr);
|
|
}
|
|
}
|
|
|
|
module.exports = DeviceConfigure;
|