diff --git a/src/panels/config/devices/ha-config-device-page.ts b/src/panels/config/devices/ha-config-device-page.ts index 9d625e4f09..18813f5141 100644 --- a/src/panels/config/devices/ha-config-device-page.ts +++ b/src/panels/config/devices/ha-config-device-page.ts @@ -1224,6 +1224,7 @@ export class HaConfigDevicePage extends LitElement { ), text: err.message, }); + return; } if ( @@ -1251,16 +1252,32 @@ export class HaConfigDevicePage extends LitElement { const updateProms = entities.map((entity) => { const name = entity.name || entity.stateName; - let newEntityId: string | null = null; - let newName: string | null = null; + let newEntityId: string | undefined; + let newName: string | null | undefined; - if (name && name.includes(oldDeviceName)) { + let shouldUpdateName: boolean; + let shouldUpdateEntityId = false; + + if (entity.has_entity_name && !entity.name) { + shouldUpdateName = false; + } else if ( + entity.has_entity_name && + (entity.name === oldDeviceName || entity.name === newDeviceName) + ) { + shouldUpdateName = true; + // clear name if it matches the device name and it uses the device name (entity naming) + newName = null; + } else if (name && name.includes(oldDeviceName)) { + shouldUpdateName = true; newName = name.replace(oldDeviceName, newDeviceName); + } else { + shouldUpdateName = false; } if (renameEntityid) { const oldSearch = slugify(oldDeviceName); if (entity.entity_id.includes(oldSearch)) { + shouldUpdateEntityId = true; newEntityId = entity.entity_id.replace( oldSearch, slugify(newDeviceName) @@ -1268,13 +1285,13 @@ export class HaConfigDevicePage extends LitElement { } } - if (!newName && !newEntityId) { + if (newName === undefined && newEntityId === undefined) { return undefined; } return updateEntityRegistryEntry(this.hass!, entity.entity_id, { - name: newName || name, - new_entity_id: newEntityId || entity.entity_id, + name: shouldUpdateName ? newName : undefined, + new_entity_id: shouldUpdateEntityId ? newEntityId : undefined, }); }); await Promise.all(updateProms);