mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-10-23 14:22:38 +00:00
Don’t allow duplicate friendly_name. #1876
This commit is contained in:
1 parent
bf36f1e8a3
commit
67f6ccc8a8
2 files changed
+45
-3
No files matched your search
+16
-2
@@ -262,6 +262,16 @@ function write() {
|
||||
function validate() {
|
||||
const validate = ajv.compile(schema);
|
||||
const valid = validate(_settings);
|
||||
|
||||
// Verify that all friendly names are unique
|
||||
const names = [];
|
||||
const check = (name) => {
|
||||
if (names.includes(name)) throw new Error(`Duplicate friendly_name '${name}' found`);
|
||||
names.push(name);
|
||||
};
|
||||
Object.values(_settingsWithDefaults.devices).forEach((d) => check(d.friendly_name));
|
||||
Object.values(_settingsWithDefaults.groups).forEach((g) => check(g.friendly_name));
|
||||
|
||||
return !valid ? validate.errors.map((v) => `${v.dataPath.substring(1)} ${v.message}`) : null;
|
||||
}
|
||||
|
||||
@@ -444,8 +454,8 @@ function removeDevice(IDorName) {
|
||||
}
|
||||
|
||||
function addGroup(name) {
|
||||
if (getGroup(name)) {
|
||||
throw new Error(`Group '${name}' already exists`);
|
||||
if (getGroup(name) || getDevice(name)) {
|
||||
throw new Error(`friendly_name '${name}' is already in use`);
|
||||
}
|
||||
|
||||
const settings = get();
|
||||
@@ -524,6 +534,10 @@ function changeDeviceOptions(IDorName, newOptions) {
|
||||
}
|
||||
|
||||
function changeFriendlyName(IDorName, newName) {
|
||||
if (getGroup(newName) || getDevice(newName)) {
|
||||
throw new Error(`friendly_name '${newName}' is already in use`);
|
||||
}
|
||||
|
||||
const device = getDeviceThrowIfNotExists(IDorName);
|
||||
const settings = get();
|
||||
settings.devices[device.ID].friendly_name = newName;
|
||||
|
||||
Reference in new issue
Block a user