From c2b632fd9e413968f03ce1a2f46d5ab3f247b596 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 7 Feb 2023 15:06:02 +0100 Subject: [PATCH] Allow changing `switch_as_x` back to `switch` --- .../entities/entity-registry-settings.ts | 123 ++++++++++++------ src/translations/en.json | 1 + 2 files changed, 85 insertions(+), 39 deletions(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index d1afbb6b50..d977b0f58c 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -204,6 +204,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { this._helperConfigEntry = entries.find( (ent) => ent.entry_id === this.entry.config_entry_id ); + if (this._helperConfigEntry?.domain === "switch_as_x") { + this._switchAs = computeDomain(this.entry.entity_id); + } }); } } @@ -552,7 +555,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { ` : ""} - ${domain === "switch" + ${domain === "switch" || + this._helperConfigEntry?.domain === "switch_as_x" ? html` - - ${this.hass.localize( - "ui.dialogs.entity_registry.editor.device_classes.switch.switch" - )} - - - ${this.hass.localize( - "ui.dialogs.entity_registry.editor.device_classes.switch.outlet" - )} - -
  • - ${this._switchAsDomainsSorted( - SWITCH_AS_DOMAINS, - this.hass.localize - ).map( - (entry) => html` - - ${entry.label} - - ` - )} + ${domain === "switch" + ? html` + ${this.hass.localize( + "ui.dialogs.entity_registry.editor.device_classes.switch.switch" + )} + + + ${this.hass.localize( + "ui.dialogs.entity_registry.editor.device_classes.switch.outlet" + )} + +
  • + ${this._switchAsDomainsSorted( + SWITCH_AS_DOMAINS, + this.hass.localize + ).map( + (entry) => html` + + ${entry.label} + + ` + )}` + : html`${domainToName( + this.hass.localize, + "switch" + )}${domainToName( + this.hass.localize, + domain + )}`}
    ` : ""} - ${this._helperConfigEntry + ${this._helperConfigEntry && this._helperConfigEntry.supports_options ? html`
    ${this.hass.localize("ui.dialogs.entity_registry.editor.delete")} @@ -923,7 +948,10 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { const switchAs = ev.target.value === "outlet" ? "switch" : ev.target.value; this._switchAs = switchAs; - if (ev.target.value === "outlet" || ev.target.value === "switch") { + if ( + computeDomain(this.entry.entity_id) === "switch" && + (ev.target.value === "outlet" || ev.target.value === "switch") + ) { this._deviceClass = ev.target.value; } } @@ -1136,24 +1164,41 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { this._submitting = false; } - if (this._switchAs !== "switch") { + if (this._switchAs !== computeDomain(this._entityId)) { if ( !(await showConfirmationDialog(this, { text: this.hass!.localize( - "ui.dialogs.entity_registry.editor.switch_as_x_confirm", + `ui.dialogs.entity_registry.editor.${ + this._helperConfigEntry?.domain === "switch_as_x" + ? "switch_as_x_remove_confirm" + : "switch_as_x_confirm" + }`, "domain", - this._switchAs + domainToName( + this.hass.localize, + this._helperConfigEntry?.domain === "switch_as_x" + ? domain + : this._switchAs + ).toLowerCase() ), })) ) { return; } + const entityId = this._entityId.trim(); + if (this._helperConfigEntry?.domain === "switch_as_x") { + // remove current helper + await deleteConfigEntry(this.hass, this._helperConfigEntry.entry_id); + if (this._switchAs === "switch") { + return; + } + } const configFlow = await createConfigFlow(this.hass, "switch_as_x"); const result = (await handleConfigFlowStep( this.hass, configFlow.flow_id, { - entity_id: this._entityId.trim(), + entity_id: entityId, target_domain: this._switchAs, } )) as DataEntryFlowStepCreateEntry; @@ -1210,9 +1255,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { private _switchAsDomainsSorted = memoizeOne( (domains: string[], localize: LocalizeFunc) => domains - .map((entry) => ({ - domain: entry, - label: domainToName(localize, entry), + .map((domain) => ({ + domain, + label: domainToName(localize, domain), })) .sort((a, b) => stringCompare(a.label, b.label, this.hass.locale.language) diff --git a/src/translations/en.json b/src/translations/en.json index 79f5a7ebaa..44cbc1968f 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -972,6 +972,7 @@ "enable_entity": "Enable", "open_device_settings": "Open device settings", "switch_as_x_confirm": "This switch will be hidden and a new {domain} will be added. Your existing configurations using the switch will continue to work.", + "switch_as_x_remove_confirm": "This {domain} will be removed and the original switch will be visible again. Your existing configurations using the {domain} will no longer work!", "enabled_description": "Disabled entities will not be added to Home Assistant.", "enabled_delay_confirm": "The enabled entities will be added to Home Assistant in {delay} seconds", "enabled_restart_confirm": "Restart Home Assistant to finish enabling the entities",