Allow changing switch_as_x back to switch

This commit is contained in:
Bram Kragten 2023-02-07 15:06:02 +01:00
parent c19cba85ec
commit c2b632fd9e
No known key found for this signature in database
GPG Key ID: FBE2DFDB363EF55B
2 changed files with 85 additions and 39 deletions

View File

@ -204,6 +204,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._helperConfigEntry = entries.find( this._helperConfigEntry = entries.find(
(ent) => ent.entry_id === this.entry.config_entry_id (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) {
</ha-select> </ha-select>
` `
: ""} : ""}
${domain === "switch" ${domain === "switch" ||
this._helperConfigEntry?.domain === "switch_as_x"
? html`<ha-select ? html`<ha-select
.label=${this.hass.localize( .label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.device_class" "ui.dialogs.entity_registry.editor.device_class"
@ -562,37 +566,56 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
@selected=${this._switchAsChanged} @selected=${this._switchAsChanged}
@closed=${stopPropagation} @closed=${stopPropagation}
> >
<mwc-list-item ${domain === "switch"
value="switch" ? html`<mwc-list-item
.selected=${!this._deviceClass || value="switch"
this._deviceClass === "switch"} .selected=${!this._deviceClass ||
> this._deviceClass === "switch"}
${this.hass.localize( >
"ui.dialogs.entity_registry.editor.device_classes.switch.switch" ${this.hass.localize(
)} "ui.dialogs.entity_registry.editor.device_classes.switch.switch"
</mwc-list-item> )}
<mwc-list-item </mwc-list-item>
value="outlet" <mwc-list-item
.selected=${this._deviceClass === "outlet"} value="outlet"
> .selected=${this._deviceClass === "outlet"}
${this.hass.localize( >
"ui.dialogs.entity_registry.editor.device_classes.switch.outlet" ${this.hass.localize(
)} "ui.dialogs.entity_registry.editor.device_classes.switch.outlet"
</mwc-list-item> )}
<li divider role="separator"></li> </mwc-list-item>
${this._switchAsDomainsSorted( <li divider role="separator"></li>
SWITCH_AS_DOMAINS, ${this._switchAsDomainsSorted(
this.hass.localize SWITCH_AS_DOMAINS,
).map( this.hass.localize
(entry) => html` ).map(
<mwc-list-item .value=${entry.domain}> (entry) => html`
${entry.label} <mwc-list-item
</mwc-list-item> .value=${entry.domain}
` .selected=${this._switchAs === entry.domain}
)} >
${entry.label}
</mwc-list-item>
`
)}`
: html`<mwc-list-item
value="switch"
.selected=${this._switchAs === "switch"}
>${domainToName(
this.hass.localize,
"switch"
)}</mwc-list-item
><mwc-list-item
value="switch"
.selected=${this._switchAs === domain}
>${domainToName(
this.hass.localize,
domain
)}</mwc-list-item
>`}
</ha-select>` </ha-select>`
: ""} : ""}
${this._helperConfigEntry ${this._helperConfigEntry && this._helperConfigEntry.supports_options
? html` ? html`
<div class="row"> <div class="row">
<mwc-button <mwc-button
@ -849,7 +872,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
class="warning" class="warning"
@click=${this._confirmDeleteEntry} @click=${this._confirmDeleteEntry}
.disabled=${this._submitting || .disabled=${this._submitting ||
(!this._helperConfigEntry && !stateObj?.attributes.restored)} ((!this._helperConfigEntry ||
this._helperConfigEntry.domain === "switch_as_x") &&
!stateObj?.attributes.restored)}
> >
${this.hass.localize("ui.dialogs.entity_registry.editor.delete")} ${this.hass.localize("ui.dialogs.entity_registry.editor.delete")}
</mwc-button> </mwc-button>
@ -923,7 +948,10 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
const switchAs = ev.target.value === "outlet" ? "switch" : ev.target.value; const switchAs = ev.target.value === "outlet" ? "switch" : ev.target.value;
this._switchAs = switchAs; 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; this._deviceClass = ev.target.value;
} }
} }
@ -1136,24 +1164,41 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._submitting = false; this._submitting = false;
} }
if (this._switchAs !== "switch") { if (this._switchAs !== computeDomain(this._entityId)) {
if ( if (
!(await showConfirmationDialog(this, { !(await showConfirmationDialog(this, {
text: this.hass!.localize( 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", "domain",
this._switchAs domainToName(
this.hass.localize,
this._helperConfigEntry?.domain === "switch_as_x"
? domain
: this._switchAs
).toLowerCase()
), ),
})) }))
) { ) {
return; 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 configFlow = await createConfigFlow(this.hass, "switch_as_x");
const result = (await handleConfigFlowStep( const result = (await handleConfigFlowStep(
this.hass, this.hass,
configFlow.flow_id, configFlow.flow_id,
{ {
entity_id: this._entityId.trim(), entity_id: entityId,
target_domain: this._switchAs, target_domain: this._switchAs,
} }
)) as DataEntryFlowStepCreateEntry; )) as DataEntryFlowStepCreateEntry;
@ -1210,9 +1255,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
private _switchAsDomainsSorted = memoizeOne( private _switchAsDomainsSorted = memoizeOne(
(domains: string[], localize: LocalizeFunc) => (domains: string[], localize: LocalizeFunc) =>
domains domains
.map((entry) => ({ .map((domain) => ({
domain: entry, domain,
label: domainToName(localize, entry), label: domainToName(localize, domain),
})) }))
.sort((a, b) => .sort((a, b) =>
stringCompare(a.label, b.label, this.hass.locale.language) stringCompare(a.label, b.label, this.hass.locale.language)

View File

@ -972,6 +972,7 @@
"enable_entity": "Enable", "enable_entity": "Enable",
"open_device_settings": "Open device settings", "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_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_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_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", "enabled_restart_confirm": "Restart Home Assistant to finish enabling the entities",