Skip to content

Commit

Permalink
Add detail to the device+entity_id rename dialog (#21952)
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts authored Sep 25, 2024
1 parent c6e2e07 commit 4bd27e5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
89 changes: 70 additions & 19 deletions src/panels/config/devices/ha-config-device-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,19 +1330,71 @@ export class HaConfigDevicePage extends LitElement {
}
const entities = this._entities(this.deviceId, this._entityReg);

const renameEntityid =
this.showAdvanced &&
(await showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_ids"
),
text: this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_ids_warning"
),
confirmText: this.hass.localize("ui.common.rename"),
dismissText: this.hass.localize("ui.common.no"),
warning: true,
}));
let renameEntityid = false;
let entityIdRenames: { oldId: string; newId?: string }[] = [];

if (this.showAdvanced) {
const oldDeviceSlug = slugify(oldDeviceName);
const newDeviceSlug = slugify(newDeviceName);
entityIdRenames = entities.map((entity) => {
const oldId = entity.entity_id;
if (oldId.includes(oldDeviceSlug)) {
const newId = oldId.replace(oldDeviceSlug, newDeviceSlug);
return { oldId, newId };
}
return { oldId };
});

const dialogRenames = entityIdRenames
.filter((entity) => entity.newId)
.map(
(entity) =>
html`<li style="white-space: nowrap;">
${entity.oldId} -> ${entity.newId}
</li>`
);
const dialogNoRenames = entityIdRenames
.filter((entity) => !entity.newId)
.map(
(entity) =>
html`<li style="white-space: nowrap;">${entity.oldId}</li>`
);

if (dialogRenames.length) {
renameEntityid = await showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_ids"
),
text: html`${this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_ids_warning"
)} <br /><br />${this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_will_rename"
)}:
${dialogRenames}
${dialogNoRenames.length
? html`<br /><br />${this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_wont_rename",
{ deviceSlug: oldDeviceSlug }
)}:
${dialogNoRenames}`
: nothing}`,
confirmText: this.hass.localize("ui.common.rename"),
dismissText: this.hass.localize("ui.common.no"),
warning: true,
});
} else if (dialogNoRenames.length) {
await showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_no_renamable_entity_ids"
),
text: html`${this.hass.localize(
"ui.panel.config.devices.confirm_rename_entity_wont_rename",
{ deviceSlug: oldDeviceSlug }
)}:
${dialogNoRenames}`,
});
}
}

const updateProms = entities.map((entity) => {
const name = entity.name || entity.stateName;
Expand All @@ -1369,13 +1421,12 @@ export class HaConfigDevicePage extends LitElement {
}

if (renameEntityid) {
const oldSearch = slugify(oldDeviceName);
if (entity.entity_id.includes(oldSearch)) {
const entityRename = entityIdRenames?.find(
(item) => item.oldId === entity.entity_id
);
if (entityRename?.newId) {
shouldUpdateEntityId = true;
newEntityId = entity.entity_id.replace(
oldSearch,
slugify(newDeviceName)
);
newEntityId = entityRename.newId;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4080,6 +4080,9 @@
},
"confirm_rename_entity_ids": "Do you also want to rename the entity IDs of your entities?",
"confirm_rename_entity_ids_warning": "This will not change any configuration (like automations, scripts, scenes, dashboards) that is currently using these entities! You will have to update them yourself to use the new entity IDs!",
"confirm_rename_entity_will_rename": "The following entity IDs will be renamed",
"confirm_rename_entity_wont_rename": "The following entity IDs will not be renamed as they do not contain the current device name ({deviceSlug})",
"confirm_rename_entity_no_renamable_entity_ids": "No renamable entity IDs",
"confirm_disable_config_entry": "There are no more devices for the config entry {entry_name}, do you want to instead disable the config entry?",
"update_device_error": "Updating the device failed",
"disabled": "Disabled",
Expand Down

0 comments on commit 4bd27e5

Please sign in to comment.