Compare commits

...

1 Commits

Author SHA1 Message Date
Paul Bottein 9e8d38ea63 Add entity type selector for main and additional entities 2026-06-18 17:02:21 +02:00
3 changed files with 120 additions and 42 deletions
+3 -2
View File
@@ -30,7 +30,7 @@ export const computeEntityEntryName = (
fallbackStateObj?: HassEntity
): string | undefined => {
const name =
entry.name ||
entry.name ??
("original_name" in entry && entry.original_name != null
? String(entry.original_name)
: undefined);
@@ -59,7 +59,8 @@ export const computeEntityEntryName = (
return stripPrefixFromEntityName(name, deviceName) || name;
}
return name;
// Empty name = main entity → undefined, so callers fall back to the device name.
return name || undefined;
};
export const entityUseDeviceName = (
@@ -1,4 +1,5 @@
import "@home-assistant/webawesome/dist/components/divider/divider";
import { consume } from "@lit/context";
import { mdiContentCopy, mdiRestore } from "@mdi/js";
import type { HassEntity } from "home-assistant-js-websocket";
import type { CSSResultGroup, PropertyValues } from "lit";
@@ -6,9 +7,9 @@ import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { until } from "lit/directives/until";
import memoizeOne from "memoize-one";
import { consume } from "@lit/context";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { computeDomain } from "../../../common/entity/compute_domain";
import { computeEntityEntryName } from "../../../common/entity/compute_entity_name";
import { computeObjectId } from "../../../common/entity/compute_object_id";
import { supportsFeature } from "../../../common/entity/supports-feature";
import { formatNumber } from "../../../common/number/format_number";
@@ -19,14 +20,16 @@ import type {
LocalizeKeys,
} from "../../../common/translations/localize";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import "../../../components/entity/ha-entity-picker";
import "../../../components/ha-alert";
import "../../../components/ha-area-picker";
import "../../../components/ha-color-picker";
import "../../../components/ha-dropdown-item";
import "../../../components/entity/ha-entity-picker";
import "../../../components/ha-icon";
import "../../../components/ha-icon-button";
import "../../../components/ha-icon-button-next";
import "../../../components/ha-icon-picker";
import "../../../components/ha-input-helper-text";
import "../../../components/ha-labels-picker";
import "../../../components/ha-list-item";
import "../../../components/ha-md-list-item";
@@ -45,16 +48,16 @@ import {
STREAM_TYPE_HLS,
updateCameraPrefs,
} from "../../../data/camera";
import {
dirtyStateContext,
type DirtyStateContext,
} from "../../../data/context/dirty-state";
import type { ConfigEntry } from "../../../data/config_entries";
import { deleteConfigEntry } from "../../../data/config_entries";
import {
createConfigFlow,
handleConfigFlowStep,
} from "../../../data/config_flow";
import {
dirtyStateContext,
type DirtyStateContext,
} from "../../../data/context/dirty-state";
import type { DataEntryFlowStepCreateEntry } from "../../../data/data_entry_flow";
import type { DeviceRegistryEntry } from "../../../data/device/device_registry";
import { updateDeviceRegistryEntry } from "../../../data/device/device_registry";
@@ -193,6 +196,8 @@ export class EntityRegistrySettingsEditor extends LitElement {
@state() private _name!: string;
@state() private _type: "main" | "additional" = "additional";
@state() private _icon!: string;
@state() private _entityId!: EntitySettingsState["entityId"];
@@ -261,7 +266,12 @@ export class EntityRegistrySettingsEditor extends LitElement {
return;
}
this._name = this.entry.name || "";
this._type =
this.entry.device_id &&
!computeEntityEntryName(this.entry, this.hass.devices)
? "main"
: "additional";
this._name = this._type === "main" ? "" : this.entry.name || "";
this._icon = this.entry.icon || "";
this._deviceClass =
this.entry.device_class || this.entry.original_device_class;
@@ -386,7 +396,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
this._dirtyState?.setState(
{
name: this._name.trim() || null,
name: this._computeName(),
icon: this._icon.trim() || null,
entityId: this._entityId.trim(),
areaId: this._areaId ?? null,
@@ -468,34 +478,75 @@ export class EntityRegistrySettingsEditor extends LitElement {
return html`
${this.hideName
? nothing
: html`<ha-input
inset-label
class="name"
.value=${this._name}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.name"
)}
.disabled=${this.disabled}
@input=${this._nameChanged}
>
${this._device
? html`<span slot="hint"
>${this.hass.localize(
"ui.dialogs.entity_registry.editor.device_name_tip",
{
link: html`<button
class="link"
@click=${this._resetNameAndOpenDeviceSettings}
>
${this.hass.localize(
"ui.dialogs.entity_registry.editor.open_device_settings"
)}
</button>`,
}
)}</span
>`
: nothing}
</ha-input>`}
: this._device
? html`<ha-select
class="type"
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.entity_type"
)}
.value=${this._type}
.options=${[
{
value: "main",
label: this.hass.localize(
"ui.dialogs.entity_registry.editor.main_entity"
),
},
{
value: "additional",
label: this.hass.localize(
"ui.dialogs.entity_registry.editor.additional_entity"
),
},
]}
.disabled=${this.disabled}
@selected=${this._entityNameModeChanged}
></ha-select>
<ha-input-helper-text>
${this._type === "main"
? html`${this.hass.localize(
"ui.dialogs.entity_registry.editor.main_entity_description"
)}
${this.hass.localize(
"ui.dialogs.entity_registry.editor.change_device_settings",
{
link: html`<button
class="link"
@click=${this._openDeviceSettings}
>
${this.hass.localize(
"ui.dialogs.entity_registry.editor.change_device_name_link"
)}
</button>`,
}
)}`
: this.hass.localize(
"ui.dialogs.entity_registry.editor.additional_entity_description"
)}
</ha-input-helper-text>
${this._type === "main"
? nothing
: html`<ha-input
inset-label
class="name"
.value=${this._name}
.placeholder=${this.entry.original_name || ""}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.entity_name"
)}
.disabled=${this.disabled}
@input=${this._nameChanged}
></ha-input>`}`
: html`<ha-input
inset-label
class="name"
.value=${this._name}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.name"
)}
.disabled=${this.disabled}
@input=${this._nameChanged}
></ha-input>`}
${this.hideIcon
? nothing
: html`
@@ -1178,7 +1229,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
}
const params: Partial<EntityRegistryEntryUpdateParams> = {
name: this._name.trim() || null,
name: this._computeName(),
icon: this._icon.trim() || null,
area_id: this._areaId || null,
labels: this._labels || [],
@@ -1625,9 +1676,24 @@ export class EntityRegistrySettingsEditor extends LitElement {
}
}
private _resetNameAndOpenDeviceSettings() {
this._name = this.entry.name || "";
this._openDeviceSettings();
private _entityNameModeChanged(ev: HaSelectSelectEvent): void {
const value = ev.detail.value;
if (!value) {
return;
}
this._type = value === "main" ? "main" : "additional";
this._name = this._type === "main" ? "" : this.entry.name || "";
}
private _computeName(): string | null {
if (this._device && this._type === "main") {
// No original_name → keep null; forcing main on a named entity → "".
if (this.entry.name == null && !this.entry.original_name) {
return null;
}
return "";
}
return this._name.trim() || null;
}
private _openDeviceSettings() {
@@ -1764,6 +1830,10 @@ export class EntityRegistrySettingsEditor extends LitElement {
.entityId {
direction: ltr;
}
ha-input-helper-text {
display: block;
margin: 0 0 var(--ha-space-2);
}
`,
];
}
+7
View File
@@ -1920,6 +1920,12 @@
"enable_entity": "Enable",
"open_device_settings": "Open device settings",
"device_name_tip": "Consider renaming the device instead to update all its entities at once. {link}",
"entity_type": "Entity type",
"main_entity": "Main entity",
"main_entity_description": "Represents the device.",
"additional_entity_description": "Has its own name, shown with the device name.",
"additional_entity": "Additional entity",
"entity_name": "Entity name",
"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!",
"switch_as_x_change_confirm": "This {domain_1} will be removed and will be replaced by a new {domain_2}. Your existing configurations using the {domain_1} will no longer work!",
@@ -1942,6 +1948,7 @@
"use_device_area": "Use device area",
"change_device_settings": "You can {link} in the device settings",
"change_device_area_link": "change the device area",
"change_device_name_link": "change the device name",
"configure_state": "{integration} options",
"configure_state_secondary": "Specific settings for {integration}",
"stream": {