Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions src/panels/config/entities/entity-registry-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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: [
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -263,7 +275,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
>
${this._deviceClassOptions[0].map(
(deviceClass: string) => html`
<mwc-list-item .value=${deviceClass} test=${deviceClass}>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅

<mwc-list-item .value=${deviceClass}>
${this.hass.localize(
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
)}
Expand All @@ -273,7 +285,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
<li divider role="separator"></li>
${this._deviceClassOptions[1].map(
(deviceClass: string) => html`
<mwc-list-item .value=${deviceClass} test=${deviceClass}>
<mwc-list-item .value=${deviceClass}>
${this.hass.localize(
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
)}
Expand Down Expand Up @@ -307,6 +319,28 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
</ha-select>
`
: ""}
${domain === "switch"
? html`<ha-select
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.device_class"
)}
naturalMenuWidth
fixedMenuPosition
@selected=${this._switchAsChanged}
@closed=${stopPropagation}
>
<mwc-list-item value="switch" selected>
${domainToName(this.hass.localize, "switch")}</mwc-list-item
>
${SWITCH_AS_DOMAINS.map(
(as_domain) => html`
<mwc-list-item .value=${as_domain}>
${domainToName(this.hass.localize, as_domain)}
</mwc-list-item>
`
)}
</ha-select>`
: ""}
<ha-textfield
error-message="Domain needs to stay the same"
.value=${this._entityId}
Expand Down Expand Up @@ -512,6 +546,13 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._unit_of_measurement = ev.target.value;
}

private _switchAsChanged(ev): void {
if (ev.target.value === "") {
return;
}
this._switchAs = ev.target.value;
}

private _areaPicked(ev: CustomEvent) {
this._error = undefined;
this._areaId = ev.detail.value;
Expand Down Expand Up @@ -545,6 +586,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {

private async _updateEntry(): Promise<void> {
this._submitting = true;

const parent = (this.getRootNode() as ShadowRoot).host as HTMLElement;

const params: Partial<EntityRegistryEntryUpdateParams> = {
name: this._name.trim() || null,
icon: this._icon.trim() || null,
Expand Down Expand Up @@ -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<void> {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down