From 13a7902fd4e1ab0d6712312eb5d2d85b2a499ce8 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 18:29:41 +0200 Subject: [PATCH 1/7] Add switch as x to entity settings --- .../entities/entity-registry-settings.ts | 80 ++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index a5c215bbd6ab..cfb079f43d5e 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -28,6 +28,11 @@ import { deleteConfigEntry, getConfigEntries, } from "../../../data/config_entries"; +import { + createConfigFlow, + handleConfigFlowStep, +} from "../../../data/config_flow"; +import { DataEntryFlowStepCreateEntry } from "../../../data/data_entry_flow"; import { DeviceRegistryEntry, subscribeDeviceRegistry, @@ -36,9 +41,11 @@ import { import { EntityRegistryEntryUpdateParams, ExtEntityRegistryEntry, + fetchEntityRegistry, removeEntityRegistryEntry, updateEntityRegistryEntry, } from "../../../data/entity_registry"; +import { domainToName } from "../../../data/integration"; import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow"; import { showAlertDialog, @@ -48,6 +55,7 @@ import { SubscribeMixin } from "../../../mixins/subscribe-mixin"; import { haStyle } from "../../../resources/styles"; import type { HomeAssistant } from "../../../types"; import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail"; +import { showEntityEditorDialog } from "./show-dialog-entity-editor"; const OVERRIDE_DEVICE_CLASSES = { cover: [ @@ -88,6 +96,8 @@ const OVERRIDE_SENSOR_UNITS = { pressure: ["hPa", "Pa", "kPa", "bar", "cbar", "mbar", "mmHg", "inHg", "psi"], }; +const SWITCH_AS_DOMAINS = ["light", "lock", "cover", "fan", "siren"]; + @customElement("entity-registry-settings") export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @property({ attribute: false }) public hass!: HomeAssistant; @@ -102,6 +112,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @state() private _deviceClass?: string; + @state() private _switchAs?: string; + @state() private _areaId?: string | null; @state() private _disabledBy!: string | null; @@ -263,7 +275,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { > ${this._deviceClassOptions[0].map( (deviceClass: string) => html` - + ${this.hass.localize( `ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}` )} @@ -273,7 +285,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
  • ${this._deviceClassOptions[1].map( (deviceClass: string) => html` - + ${this.hass.localize( `ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}` )} @@ -307,6 +319,26 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { ` : ""} + ${domain === "switch" + ? html` + + ${SWITCH_AS_DOMAINS.map( + (as_domain) => html` + + ${domainToName(this.hass.localize, as_domain)} + + ` + )} + ` + : ""} { this._submitting = true; + + const parent = (this.getRootNode() as ShadowRoot).host as HTMLElement; + const params: Partial = { name: this._name.trim() || null, icon: this._icon.trim() || null, @@ -604,6 +643,43 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { } finally { this._submitting = false; } + + if (this._switchAs) { + if ( + !(await showConfirmationDialog(this, { + text: `This entity will be replaced by an entity of the domain '${this._switchAs}', the original entity will be hidden. You can manually change your configuration to use the new entity. If you remove the new entity, the old entity will be restored.`, + })) + ) { + this._submitting = false; + return; + } + const configFlow = await createConfigFlow(this.hass, "switch_as_x"); + const result = (await handleConfigFlowStep( + this.hass, + configFlow.flow_id, + { + entity_id: this._entityId.trim(), + target_domain: this._switchAs, + } + )) as DataEntryFlowStepCreateEntry; + if (!result.result?.entry_id) { + return; + } + const unsub = await this.hass.connection.subscribeEvents(() => { + unsub(); + fetchEntityRegistry(this.hass.connection).then((entityRegistry) => { + const entity = entityRegistry.find( + (reg) => reg.config_entry_id === result.result!.entry_id + ); + if (!entity) { + return; + } + showEntityEditorDialog(parent, { + entity_id: entity.entity_id, + }); + }); + }, "entity_registry_updated"); + } } private async _confirmDeleteEntry(): Promise { From f25cfb73ab2ca1388aec48a3a4abed0d2812e942 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 18:30:55 +0200 Subject: [PATCH 2/7] Update entity-registry-settings.ts --- src/panels/config/entities/entity-registry-settings.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index cfb079f43d5e..1e5b7e51a4e3 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -650,7 +650,6 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { text: `This entity will be replaced by an entity of the domain '${this._switchAs}', the original entity will be hidden. You can manually change your configuration to use the new entity. If you remove the new entity, the old entity will be restored.`, })) ) { - this._submitting = false; return; } const configFlow = await createConfigFlow(this.hass, "switch_as_x"); From 52c918523f57ddc53c6ad4ceef577f9b65b70e3d Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 30 Mar 2022 11:50:12 -0500 Subject: [PATCH 3/7] Update confirmation text --- src/panels/config/entities/entity-registry-settings.ts | 6 +++++- src/translations/en.json | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index 1e5b7e51a4e3..5f901c0cdf7f 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -647,7 +647,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { if (this._switchAs) { if ( !(await showConfirmationDialog(this, { - text: `This entity will be replaced by an entity of the domain '${this._switchAs}', the original entity will be hidden. You can manually change your configuration to use the new entity. If you remove the new entity, the old entity will be restored.`, + text: this.hass!.localize( + "ui.dialogs.entity_registry.editor.switch_as_x_confirm", + "domain", + this._switchAs + ), })) ) { return; diff --git a/src/translations/en.json b/src/translations/en.json index ce6aa8502194..7475278a5b13 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -842,6 +842,7 @@ "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", "open_device_settings": "Open device settings", + "switch_as_x_confirm": "This switch will be replaced by a {domain} and hidden from the UI. Current configuration using the switch will continue to work. Removing the {domain} will un-hide the switch. Are you sure you want to change it?", "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", From 366d55306d51a2f68441701d2f179c2550a59b12 Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 30 Mar 2022 12:06:06 -0500 Subject: [PATCH 4/7] Updates based on review --- src/panels/config/entities/entity-registry-settings.ts | 4 +++- src/translations/en.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index 5f901c0cdf7f..c8237d28c819 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -329,7 +329,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @selected=${this._switchAsChanged} @closed=${stopPropagation} > - + + ${domainToName(this.hass.localize, "switch")} ${SWITCH_AS_DOMAINS.map( (as_domain) => html` diff --git a/src/translations/en.json b/src/translations/en.json index 7475278a5b13..2b4b7728f948 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -842,7 +842,7 @@ "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", "open_device_settings": "Open device settings", - "switch_as_x_confirm": "This switch will be replaced by a {domain} and hidden from the UI. Current configuration using the switch will continue to work. Removing the {domain} will un-hide the switch. Are you sure you want to change it?", + "switch_as_x_confirm": "This switch will be replaced by a fan. Existing configurations using the switch will continue to 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", From f3feb24435effb940e6061e31f0d2523044301d1 Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 30 Mar 2022 12:46:25 -0500 Subject: [PATCH 5/7] un hard cde --- src/translations/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/translations/en.json b/src/translations/en.json index 2b4b7728f948..cda86d14fac7 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -842,7 +842,7 @@ "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", "open_device_settings": "Open device settings", - "switch_as_x_confirm": "This switch will be replaced by a fan. Existing configurations using the switch will continue to work.", + "switch_as_x_confirm": "This switch will be replaced by a {domain}. Existing configurations using the switch will continue to 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", From 90b7abc37022463626bdbb02331d800287a8cb94 Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 30 Mar 2022 12:53:25 -0500 Subject: [PATCH 6/7] switch default --- src/panels/config/entities/entity-registry-settings.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index c8237d28c819..48ebb5df49cf 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -112,7 +112,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @state() private _deviceClass?: string; - @state() private _switchAs?: string; + @state() private _switchAs = "switch"; @state() private _areaId?: string | null; @@ -547,6 +547,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { } private _switchAsChanged(ev): void { + if (ev.target.value === "") { + return; + } this._switchAs = ev.target.value; } @@ -646,7 +649,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { this._submitting = false; } - if (this._switchAs) { + if (this._switchAs !== "switch") { if ( !(await showConfirmationDialog(this, { text: this.hass!.localize( From 63a316cc68d191eafed9585afba7d85f69c17907 Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Wed, 30 Mar 2022 13:01:30 -0500 Subject: [PATCH 7/7] Update src/translations/en.json --- src/translations/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/translations/en.json b/src/translations/en.json index cda86d14fac7..86bc60cbfc02 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -842,7 +842,7 @@ "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", "open_device_settings": "Open device settings", - "switch_as_x_confirm": "This switch will be replaced by a {domain}. 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.", "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",