Merge pull request #206 from tb-killa/rename-devices

Rename devices (friendly_name) ->  (#177)
This commit is contained in:
Koen Kanters
2018-07-24 18:27:04 +02:00
committed by GitHub
2 changed files with 47 additions and 0 deletions
+34
View File
@@ -404,6 +404,40 @@ class Controller {
} else {
cleanup();
}
} else if (option === 'rename') {
const invalid = `Invalid rename message format expected {old: 'friendly_name', new: 'new_name} ` +
`got ${message.toString()}`;
let json = null;
try {
json = JSON.parse(message.toString());
} catch (e) {
logger.error(invalid);
return;
}
// Validate message
if (!json.new || !json.old) {
logger.error(invalid);
return;
}
if (settings.changeFriendlyName(json.old, json.new)) {
logger.info(`Successfully renamed - ${json.old} to ${json.new} `);
} else {
logger.error(`Failed to renamed - ${json.old} to ${json.new}`);
return;
}
// Homeassistant rediscover
if (settings.get().homeassistant) {
const ID = settings.getIDByFriendlyName(json.new);
const device = this.zigbee.getDevice(ID);
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
if (mappedModel) {
homeassistant.discover(device.ieeeAddr, mappedModel.model, this.mqtt, true);
}
}
} else {
logger.warn(`Cannot handle MQTT config option '${option}' with message '${message}'`);
}
+13
View File
@@ -44,6 +44,18 @@ function getIDByFriendlyName(friendlyName) {
);
}
function changeFriendlyName(old, new_) {
const ID = getIDByFriendlyName(old);
if (!ID) {
return false;
}
settings.devices[ID].friendly_name = new_;
writeRead();
return true;
}
module.exports = {
get: () => settings,
write: () => write(),
@@ -51,4 +63,5 @@ module.exports = {
addDevice: (id) => addDevice(id),
removeDevice: (id) => removeDevice(id),
getIDByFriendlyName: (friendlyName) => getIDByFriendlyName(friendlyName),
changeFriendlyName: (old, new_) => changeFriendlyName(old, new_),
};