Refactor remove device & remove from home assistant

This commit is contained in:
Koenkk
2018-06-09 12:33:40 +02:00
parent 7c53e9bb15
commit 884e3aa475
4 changed files with 66 additions and 33 deletions
+29 -18
View File
@@ -162,7 +162,7 @@ class Controller {
getDeviceStartupLogMessage(device) {
let friendlyName = 'unknown';
let type = 'unknown';
let friendlyDevice = { model: 'unkown', description: 'unknown' };
let friendlyDevice = {model: 'unkown', description: 'unknown'};
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
if (mappedModel) {
friendlyDevice = mappedModel;
@@ -310,20 +310,33 @@ class Controller {
this.mqtt.log('devices', devices);
} else if (option === 'remove') {
if (message.toString() !== null && message.toString() !== '' && message.toString().length > 0) {
const deviceID = Object.keys(settings.get().devices).find((id) =>
settings.getDevice(id).friendly_name === message.toString()
);
if (!deviceID) {
logger.error(`Cannot handle '${topic}' because deviceID of '${message.toString()}' cannot be found`);
return;
}
settings.removeDevice(deviceID);
this.zigbee.removedevice(deviceID);
} else {
logger.error(`Cannot handle MQTT config option '${option}' with Payload is null - we need the friendly_name to proceed`);
message = message.toString();
const deviceID = settings.getIDByFriendlyName(message);
const device = this.zigbee.getDevice(deviceID);
if (!deviceID) {
logger.error(`Cannot handle '${topic}' because device with friendly_name '${message}' cannot be found`);
return;
}
// Remove from zigbee network and settings.
this.zigbee.removeDevice(deviceID, (error) => {
if (!error) {
// Clear Home Assistant MQTT discovery message
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
if (settings.get().homeassistant && mappedModel) {
homeassistant.clear(deviceID, mappedModel.model, this.mqtt);
}
// Remove from configuration.yaml
settings.removeDevice(deviceID);
logger.info(`Successfully removed ${deviceID}`);
this.mqtt.log('removed_device', message);
} else {
logger.error(`Failed to remove ${deviceID}`);
}
});
} else {
logger.warn(`Cannot handle MQTT config option '${option}' with message '${message}'`);
}
@@ -334,9 +347,7 @@ class Controller {
const topicPrefix = withPrefix ? topic.split('/')[2] : '';
// Map friendlyName to deviceID.
const deviceID = Object.keys(settings.get().devices).find((id) =>
settings.getDevice(id).friendly_name === friendlyName
);
const deviceID = settings.getIDByFriendlyName(friendlyName);
if (!deviceID) {
logger.error(`Cannot handle '${topic}' because deviceID of '${friendlyName}' cannot be found`);
@@ -349,7 +360,7 @@ class Controller {
json = JSON.parse(message);
} catch (e) {
// Cannot be parsed to JSON, assume state message.
json = { state: message.toString() };
json = {state: message.toString()};
}
// Find ep for this device
@@ -383,7 +394,7 @@ class Controller {
this.zigbee.publish(deviceID, message.cid, message.cmd, message.zclData, ep, callback);
published.push({ message: message, converter: converter });
published.push({message: message, converter: converter});
});
/**