Best effort remove device

This commit is contained in:
Koenkk 2018-06-10 17:35:14 +02:00
parent 884e3aa475
commit db0d8c1cdd
2 changed files with 28 additions and 22 deletions

View File

@ -311,32 +311,38 @@ class Controller {
this.mqtt.log('devices', devices);
} else if (option === 'remove') {
message = message.toString();
const deviceID = settings.getIDByFriendlyName(message);
const IDByFriendlyName = settings.getIDByFriendlyName(message);
const deviceID = IDByFriendlyName ? IDByFriendlyName : 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 cleanup = () => {
// Clear Home Assistant MQTT discovery message
if (settings.get().homeassistant && device) {
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
if (settings.get().homeassistant && mappedModel) {
if (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}`);
}
});
// Remove from configuration.yaml
settings.removeDevice(deviceID);
logger.info(`Successfully removed ${deviceID}`);
this.mqtt.log('removed_device', message);
};
// Remove from zigbee network.
if (device) {
this.zigbee.removeDevice(deviceID, (error) => {
if (!error) {
cleanup();
} else {
logger.error(`Failed to remove ${deviceID}`);
}
});
} else {
cleanup();
}
} else {
logger.warn(`Cannot handle MQTT config option '${option}' with message '${message}'`);
}

View File

@ -28,7 +28,7 @@ function addDevice(id) {
}
function removeDevice(id) {
if (settings.devices) {
if (settings.devices && settings.devices[id]) {
delete settings.devices[id];
writeRead();
}
@ -47,7 +47,7 @@ function getIDByFriendlyName(friendlyName) {
module.exports = {
get: () => settings,
write: () => write(),
getDevice: (id) => settings.devices ? settings.devices[id] : false,
getDevice: (id) => settings.devices ? settings.devices[id] : null,
addDevice: (id) => addDevice(id),
removeDevice: (id) => removeDevice(id),
getIDByFriendlyName: (friendlyName) => getIDByFriendlyName(friendlyName),