From c2c09b1284dcdbefeef5a16c273fb89de327c091 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 30 Mar 2022 20:19:26 +0200 Subject: [PATCH] Add switch as x to entity settings (#12161) Co-authored-by: Zack --- .../entities/entity-registry-settings.ts | 88 ++++++++++++++++++- src/translations/en.json | 1 + 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index a5c215bbd6..48ebb5df49 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 = "switch"; + @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,28 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { ` : ""} + ${domain === "switch" + ? html` + + ${domainToName(this.hass.localize, "switch")} + ${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 +648,46 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { } finally { this._submitting = false; } + + if (this._switchAs !== "switch") { + if ( + !(await showConfirmationDialog(this, { + text: this.hass!.localize( + "ui.dialogs.entity_registry.editor.switch_as_x_confirm", + "domain", + this._switchAs + ), + })) + ) { + 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 { diff --git a/src/translations/en.json b/src/translations/en.json index ce6aa85021..86bc60cbfc 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 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",