From 9a12d563eca7a41d389910ee4e8d1aee9d50ba0a Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 3 Jul 2025 19:10:20 +0200 Subject: [PATCH] Use new format function --- src/common/translations/entity-state.ts | 14 +++-- src/components/entity/ha-entity-picker.ts | 40 ++----------- src/components/entity/ha-statistic-picker.ts | 40 ++----------- src/data/selector/format_selector_value.ts | 19 +----- src/dialogs/more-info/ha-more-info-dialog.ts | 60 ++++++++----------- src/dialogs/quick-bar/ha-quick-bar.ts | 25 ++------ .../card-editor/hui-entity-picker-table.ts | 22 +------ src/types.ts | 4 +- 8 files changed, 56 insertions(+), 168 deletions(-) diff --git a/src/common/translations/entity-state.ts b/src/common/translations/entity-state.ts index 1102ebcc46..ed52228229 100644 --- a/src/common/translations/entity-state.ts +++ b/src/common/translations/entity-state.ts @@ -7,6 +7,7 @@ import { computeDeviceName } from "../entity/compute_device_name"; import { getEntityContext } from "../entity/context/get_entity_context"; import { computeAreaName } from "../entity/compute_area_name"; import { computeFloorName } from "../entity/compute_floor_name"; +import { ensureArray } from "../array/ensure-array"; export type FormatEntityStateFunc = ( stateObj: HassEntity, @@ -22,11 +23,11 @@ export type FormatEntityAttributeNameFunc = ( attribute: string ) => string; -export type EntityNameToken = "entity" | "device" | "area" | "floor"; +export type EntityNameType = "entity" | "device" | "area" | "floor"; export type FormatEntityNameFunc = ( stateObj: HassEntity, - tokens: EntityNameToken[], + type: EntityNameType | EntityNameType[], separator?: string ) => string; @@ -74,7 +75,8 @@ export const computeFormatFunctions = async ( ), formatEntityAttributeName: (stateObj, attribute) => computeAttributeNameDisplay(localize, stateObj, entities, attribute), - formatEntityName: (stateObj, tokens, separator = " ") => { + formatEntityName: (stateObj, type, separator = " ") => { + const types = ensureArray(type); const namesList: (string | undefined)[] = []; const { device, area, floor } = getEntityContext( @@ -85,8 +87,8 @@ export const computeFormatFunctions = async ( floors ); - for (const token of tokens) { - switch (token) { + for (const t of types) { + switch (t) { case "entity": { namesList.push(computeEntityName(stateObj, entities, devices)); break; @@ -111,7 +113,7 @@ export const computeFormatFunctions = async ( } } } - return namesList.filter(Boolean).join(separator); + return namesList.filter((name) => name !== undefined).join(separator); }, }; }; diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index c547e280da..77956ea297 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -5,12 +5,8 @@ import { html, LitElement, nothing, type PropertyValues } from "lit"; import { customElement, property, query } from "lit/decorators"; import memoizeOne from "memoize-one"; import { fireEvent } from "../../common/dom/fire_event"; -import { computeAreaName } from "../../common/entity/compute_area_name"; -import { computeDeviceName } from "../../common/entity/compute_device_name"; import { computeDomain } from "../../common/entity/compute_domain"; -import { computeEntityName } from "../../common/entity/compute_entity_name"; import { computeStateName } from "../../common/entity/compute_state_name"; -import { getEntityContext } from "../../common/entity/context/get_entity_context"; import { isValidEntityId } from "../../common/entity/valid_entity_id"; import { computeRTL } from "../../common/util/compute_rtl"; import { domainToName } from "../../data/integration"; @@ -148,21 +144,9 @@ export class HaEntityPicker extends LitElement { `; } - const { area, device } = getEntityContext( - stateObj, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ); - - const entityName = computeEntityName( - stateObj, - this.hass.entities, - this.hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = this.hass.formatEntityName(stateObj, "entity"); + const deviceName = this.hass.formatEntityName(stateObj, "device"); + const areaName = this.hass.formatEntityName(stateObj, "area"); const isRTL = computeRTL(this.hass); @@ -321,22 +305,10 @@ export class HaEntityPicker extends LitElement { items = entityIds.map((entityId) => { const stateObj = hass!.states[entityId]; - const { area, device } = getEntityContext( - stateObj, - hass.entities, - hass.devices, - hass.areas, - hass.floors - ); - const friendlyName = computeStateName(stateObj); // Keep this for search - const entityName = computeEntityName( - stateObj, - hass.entities, - hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = this.hass.formatEntityName(stateObj, "entity"); + const deviceName = this.hass.formatEntityName(stateObj, "device"); + const areaName = this.hass.formatEntityName(stateObj, "area"); const domainName = domainToName( this.hass.localize, diff --git a/src/components/entity/ha-statistic-picker.ts b/src/components/entity/ha-statistic-picker.ts index 221fbc9a22..668a030011 100644 --- a/src/components/entity/ha-statistic-picker.ts +++ b/src/components/entity/ha-statistic-picker.ts @@ -6,11 +6,7 @@ import { customElement, property, query } from "lit/decorators"; import memoizeOne from "memoize-one"; import { ensureArray } from "../../common/array/ensure-array"; import { fireEvent } from "../../common/dom/fire_event"; -import { computeAreaName } from "../../common/entity/compute_area_name"; -import { computeDeviceName } from "../../common/entity/compute_device_name"; -import { computeEntityName } from "../../common/entity/compute_entity_name"; import { computeStateName } from "../../common/entity/compute_state_name"; -import { getEntityContext } from "../../common/entity/context/get_entity_context"; import { computeRTL } from "../../common/util/compute_rtl"; import { domainToName } from "../../data/integration"; import { @@ -259,22 +255,10 @@ export class HaStatisticPicker extends LitElement { } const id = meta.statistic_id; - const { area, device } = getEntityContext( - stateObj, - hass.entities, - hass.devices, - hass.areas, - hass.floors - ); - const friendlyName = computeStateName(stateObj); // Keep this for search - const entityName = computeEntityName( - stateObj, - this.hass.entities, - this.hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = hass.formatEntityName(stateObj, "entity"); + const deviceName = hass.formatEntityName(stateObj, "device"); + const areaName = hass.formatEntityName(stateObj, "area"); const primary = entityName || deviceName || id; const secondary = [areaName, entityName ? deviceName : undefined] @@ -347,21 +331,9 @@ export class HaStatisticPicker extends LitElement { const stateObj = this.hass.states[statisticId]; if (stateObj) { - const { area, device } = getEntityContext( - stateObj, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ); - - const entityName = computeEntityName( - stateObj, - this.hass.entities, - this.hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = this.hass.formatEntityName(stateObj, "entity"); + const deviceName = this.hass.formatEntityName(stateObj, "device"); + const areaName = this.hass.formatEntityName(stateObj, "area"); const isRTL = computeRTL(this.hass); diff --git a/src/data/selector/format_selector_value.ts b/src/data/selector/format_selector_value.ts index cca90596c3..7a85abd099 100644 --- a/src/data/selector/format_selector_value.ts +++ b/src/data/selector/format_selector_value.ts @@ -1,8 +1,5 @@ import { ensureArray } from "../../common/array/ensure-array"; import { computeAreaName } from "../../common/entity/compute_area_name"; -import { computeDeviceName } from "../../common/entity/compute_device_name"; -import { computeEntityName } from "../../common/entity/compute_entity_name"; -import { getEntityContext } from "../../common/entity/context/get_entity_context"; import { blankBeforeUnit } from "../../common/translations/blank_before_unit"; import type { HomeAssistant } from "../../types"; import type { Selector } from "../selector"; @@ -79,20 +76,8 @@ export const formatSelectorValue = ( if (!stateObj) { return entityId; } - const { device } = getEntityContext( - stateObj, - hass.entities, - hass.devices, - hass.areas, - hass.floors - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const entityName = computeEntityName( - stateObj, - hass.entities, - hass.devices - ); - return [deviceName, entityName].filter(Boolean).join(" ") || entityId; + const name = hass.formatEntityName(stateObj, ["device", "entity"], " "); + return name || entityId; }) .join(", "); } diff --git a/src/dialogs/more-info/ha-more-info-dialog.ts b/src/dialogs/more-info/ha-more-info-dialog.ts index 16104cf394..140a2e3b4f 100644 --- a/src/dialogs/more-info/ha-more-info-dialog.ts +++ b/src/dialogs/more-info/ha-more-info-dialog.ts @@ -21,14 +21,8 @@ import { stopPropagation } from "../../common/dom/stop_propagation"; import { computeAreaName } from "../../common/entity/compute_area_name"; import { computeDeviceName } from "../../common/entity/compute_device_name"; import { computeDomain } from "../../common/entity/compute_domain"; -import { - computeEntityEntryName, - computeEntityName, -} from "../../common/entity/compute_entity_name"; -import { - getEntityContext, - getEntityEntryContext, -} from "../../common/entity/context/get_entity_context"; +import { computeEntityEntryName } from "../../common/entity/compute_entity_name"; +import { getEntityEntryContext } from "../../common/entity/context/get_entity_context"; import { shouldHandleRequestSelectedEvent } from "../../common/mwc/handle-request-selected-event"; import { navigate } from "../../common/navigate"; import "../../components/ha-button-menu"; @@ -304,34 +298,30 @@ export class MoreInfoDialog extends LitElement { this._initialView !== DEFAULT_VIEW && !this._childView; const showCloseIcon = isDefaultView || isSpecificInitialView; - const context = stateObj - ? getEntityContext( - stateObj, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ) - : this._entry - ? getEntityEntryContext( - this._entry, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ) + let entityName: string | undefined; + let deviceName: string | undefined; + let areaName: string | undefined; + + if (stateObj) { + entityName = this.hass.formatEntityName(stateObj, "entity"); + deviceName = this.hass.formatEntityName(stateObj, "device"); + areaName = this.hass.formatEntityName(stateObj, "area"); + } else if (this._entry) { + const context = getEntityEntryContext( + this._entry, + this.hass.entities, + this.hass.devices, + this.hass.areas, + this.hass.floors + ); + entityName = computeEntityEntryName(this._entry, this.hass.devices); + deviceName = context?.device + ? computeDeviceName(context.device) : undefined; - - const entityName = stateObj - ? computeEntityName(stateObj, this.hass.entities, this.hass.devices) - : this._entry - ? computeEntityEntryName(this._entry, this.hass.devices) - : entityId; - - const deviceName = context?.device - ? computeDeviceName(context.device) - : undefined; - const areaName = context?.area ? computeAreaName(context.area) : undefined; + areaName = context?.area ? computeAreaName(context.area) : undefined; + } else { + entityName = entityId; + } const breadcrumb = [areaName, deviceName, entityName].filter( (v): v is string => Boolean(v) diff --git a/src/dialogs/quick-bar/ha-quick-bar.ts b/src/dialogs/quick-bar/ha-quick-bar.ts index fd7ed28b9f..6c8fefd916 100644 --- a/src/dialogs/quick-bar/ha-quick-bar.ts +++ b/src/dialogs/quick-bar/ha-quick-bar.ts @@ -21,15 +21,10 @@ import { componentsWithService } from "../../common/config/components_with_servi import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { fireEvent } from "../../common/dom/fire_event"; import { computeAreaName } from "../../common/entity/compute_area_name"; -import { - computeDeviceName, - computeDeviceNameDisplay, -} from "../../common/entity/compute_device_name"; +import { computeDeviceNameDisplay } from "../../common/entity/compute_device_name"; import { computeDomain } from "../../common/entity/compute_domain"; -import { computeEntityName } from "../../common/entity/compute_entity_name"; import { computeStateName } from "../../common/entity/compute_state_name"; import { getDeviceContext } from "../../common/entity/context/get_device_context"; -import { getEntityContext } from "../../common/entity/context/get_entity_context"; import { navigate } from "../../common/navigate"; import { caseInsensitiveStringCompare } from "../../common/string/compare"; import type { ScorableTextItem } from "../../common/string/filter/sequence-matching"; @@ -631,22 +626,10 @@ export class QuickBar extends LitElement { .map((entityId) => { const stateObj = this.hass.states[entityId]; - const { area, device } = getEntityContext( - stateObj, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ); - const friendlyName = computeStateName(stateObj); // Keep this for search - const entityName = computeEntityName( - stateObj, - this.hass.entities, - this.hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = this.hass.formatEntityName(stateObj, "entity"); + const deviceName = this.hass.formatEntityName(stateObj, "device"); + const areaName = this.hass.formatEntityName(stateObj, "area"); const primary = entityName || deviceName || entityId; const secondary = [areaName, entityName ? deviceName : undefined] diff --git a/src/panels/lovelace/editor/card-editor/hui-entity-picker-table.ts b/src/panels/lovelace/editor/card-editor/hui-entity-picker-table.ts index 7e0bd296ac..f99e370548 100644 --- a/src/panels/lovelace/editor/card-editor/hui-entity-picker-table.ts +++ b/src/panels/lovelace/editor/card-editor/hui-entity-picker-table.ts @@ -5,11 +5,7 @@ import { styleMap } from "lit/directives/style-map"; import memoizeOne from "memoize-one"; import type { HASSDomEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event"; -import { computeAreaName } from "../../../../common/entity/compute_area_name"; -import { computeDeviceName } from "../../../../common/entity/compute_device_name"; import { computeDomain } from "../../../../common/entity/compute_domain"; -import { computeEntityName } from "../../../../common/entity/compute_entity_name"; -import { getEntityContext } from "../../../../common/entity/context/get_entity_context"; import type { LocalizeFunc } from "../../../../common/translations/localize"; import { computeRTL } from "../../../../common/util/compute_rtl"; import "../../../../components/data-table/ha-data-table"; @@ -66,21 +62,9 @@ export class HuiEntityPickerTable extends LitElement { (entity) => { const stateObj = this.hass.states[entity]; - const { area, device } = getEntityContext( - stateObj, - this.hass.entities, - this.hass.devices, - this.hass.areas, - this.hass.floors - ); - - const entityName = computeEntityName( - stateObj, - this.hass.entities, - this.hass.devices - ); - const deviceName = device ? computeDeviceName(device) : undefined; - const areaName = area ? computeAreaName(area) : undefined; + const entityName = this.hass.formatEntityName(stateObj, "entity"); + const deviceName = this.hass.formatEntityName(stateObj, "device"); + const areaName = this.hass.formatEntityName(stateObj, "area"); const name = [deviceName, entityName].filter(Boolean).join(" "); const domain = computeDomain(entity); diff --git a/src/types.ts b/src/types.ts index 5af3a066c1..ed7c5cc59f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,7 @@ import type { HassServiceTarget, MessageBase, } from "home-assistant-js-websocket"; -import type { EntityNameToken } from "./common/translations/entity-state"; +import type { EntityNameType } from "./common/translations/entity-state"; import type { LocalizeFunc } from "./common/translations/localize"; import type { AreaRegistryEntry } from "./data/area_registry"; import type { DeviceRegistryEntry } from "./data/device_registry"; @@ -288,7 +288,7 @@ export interface HomeAssistant { formatEntityAttributeName(stateObj: HassEntity, attribute: string): string; formatEntityName( stateObj: HassEntity, - tokens: EntityNameToken[], + type: EntityNameType | EntityNameType[], separator?: string ): string; }