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

View File

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