diff --git a/src/common/entity/compute_entity_name_display.ts b/src/common/entity/compute_entity_name_display.ts index 948e0da0d4..cda397a1cc 100644 --- a/src/common/entity/compute_entity_name_display.ts +++ b/src/common/entity/compute_entity_name_display.ts @@ -29,14 +29,18 @@ export interface EntityNameOptions { export const computeEntityNameDisplay = ( stateObj: HassEntity, - name: EntityNameItem | EntityNameItem[] | undefined, + name: string | EntityNameItem | EntityNameItem[] | undefined, entities: HomeAssistant["entities"], devices: HomeAssistant["devices"], areas: HomeAssistant["areas"], floors: HomeAssistant["floors"], options?: EntityNameOptions ) => { - let items = ensureArray(name || DEFAULT_ENTITY_NAME); + if (typeof name === "string") { + return name; + } + + let items = ensureArray(name ?? DEFAULT_ENTITY_NAME); const separator = options?.separator ?? DEFAULT_SEPARATOR; diff --git a/src/panels/lovelace/badges/hui-entity-badge.ts b/src/panels/lovelace/badges/hui-entity-badge.ts index db38a3cfd5..e7ac83c443 100644 --- a/src/panels/lovelace/badges/hui-entity-badge.ts +++ b/src/panels/lovelace/badges/hui-entity-badge.ts @@ -19,7 +19,6 @@ import { cameraUrlWithWidthHeight } from "../../../data/camera"; import type { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -175,11 +174,7 @@ export class HuiEntityBadge extends LitElement implements LovelaceBadge { "--badge-color": color, }; - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const stateDisplay = html` diff --git a/src/panels/lovelace/cards/hui-button-card.ts b/src/panels/lovelace/cards/hui-button-card.ts index 3ad035b7b4..edf8978eaf 100644 --- a/src/panels/lovelace/cards/hui-button-card.ts +++ b/src/panels/lovelace/cards/hui-button-card.ts @@ -41,7 +41,6 @@ import type { FrontendLocaleData } from "../../../data/translation"; import type { Themes } from "../../../data/ws-themes"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasAction } from "../common/has-action"; import { createEntityNotFoundWarning } from "../components/hui-warning"; @@ -52,6 +51,21 @@ import type { } from "../types"; import type { ButtonCardConfig } from "./types"; +const EMPTY_STATE_OBJ = { + state: "unavailable", + attributes: { + friendly_name: "", + }, + entity_id: "___.empty", + context: { + id: "", + parent_id: null, + user_id: null, + }, + last_changed: "", + last_updated: "", +} satisfies HassEntity; + export const getEntityDefaultButtonAction = (entityId?: string) => entityId && DOMAINS_TOGGLE.has(computeDomain(entityId)) ? "toggle" @@ -183,9 +197,8 @@ export class HuiButtonCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass, - stateObj, + const name = this.hass.formatEntityName( + stateObj || EMPTY_STATE_OBJ, this._config.name ); diff --git a/src/panels/lovelace/cards/hui-distribution-card.ts b/src/panels/lovelace/cards/hui-distribution-card.ts index 6b4c90d3a7..0fd3cc5a34 100644 --- a/src/panels/lovelace/cards/hui-distribution-card.ts +++ b/src/panels/lovelace/cards/hui-distribution-card.ts @@ -12,7 +12,6 @@ import { computeDomain } from "../../../common/entity/compute_domain"; import { normalizeValueBySIPrefix } from "../../../common/number/normalize-by-si-prefix"; import { MobileAwareMixin } from "../../../mixins/mobile-aware-mixin"; import type { EntityNameItem } from "../../../common/entity/compute_entity_name_display"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import "../../../components/chips/ha-assist-chip"; import "../../../components/ha-card"; import "../../../components/ha-segmented-bar"; @@ -240,7 +239,7 @@ export class HuiDistributionCard const color = entity.color ? computeCssColor(entity.color) : getGraphColorByIndex(entity.originalIndex, computedStyles); - const name = computeLovelaceEntityName(this.hass!, stateObj, entity.name); + const name = this.hass!.formatEntityName(stateObj, entity.name); const formattedValue = this.hass!.formatEntityState(stateObj); segments.push({ @@ -276,7 +275,7 @@ export class HuiDistributionCard const isZeroOrNegative = !stateObj || value <= 0 || isNaN(value); const name = stateObj - ? computeLovelaceEntityName(this.hass!, stateObj, entity.name) + ? this.hass!.formatEntityName(stateObj, entity.name) : entity.entity; const formattedValue = stateObj diff --git a/src/panels/lovelace/cards/hui-entity-card.ts b/src/panels/lovelace/cards/hui-entity-card.ts index 88cd9f4450..133ff40578 100644 --- a/src/panels/lovelace/cards/hui-entity-card.ts +++ b/src/panels/lovelace/cards/hui-entity-card.ts @@ -25,7 +25,6 @@ import { handleAction } from "../common/handle-action"; import { hasAction, hasAnyAction } from "../common/has-action"; import type { HomeAssistant } from "../../../types"; import { computeCardSize } from "../common/compute-card-size"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { createEntityNotFoundWarning } from "../components/hui-warning"; @@ -146,11 +145,7 @@ export class HuiEntityCard extends LitElement implements LovelaceCard { const indexValue = stateParts.findIndex((part) => part.type === "value"); const reversedOrder = indexUnit !== -1 && indexUnit < indexValue; - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const colored = stateObj && this._getStateColor(stateObj, this._config); diff --git a/src/panels/lovelace/cards/hui-gauge-card.ts b/src/panels/lovelace/cards/hui-gauge-card.ts index 746a5b243a..7bc8ef8479 100644 --- a/src/panels/lovelace/cards/hui-gauge-card.ts +++ b/src/panels/lovelace/cards/hui-gauge-card.ts @@ -14,7 +14,6 @@ import { UNAVAILABLE } from "../../../data/entity/entity"; import type { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction, hasAnyAction } from "../common/has-action"; @@ -124,11 +123,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); // Use `stateObj.state` as value to keep formatting (e.g trailing zeros) // for consistent value display across gauge, entity, entity-row, etc. diff --git a/src/panels/lovelace/cards/hui-glance-card.ts b/src/panels/lovelace/cards/hui-glance-card.ts index 607740e52d..a63316e65b 100644 --- a/src/panels/lovelace/cards/hui-glance-card.ts +++ b/src/panels/lovelace/cards/hui-glance-card.ts @@ -18,7 +18,6 @@ import type { import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../../../data/sensor"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction, hasAnyAction } from "../common/has-action"; @@ -252,11 +251,7 @@ export class HuiGlanceCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass!, - stateObj, - entityConf.name - ); + const name = this.hass!.formatEntityName(stateObj, entityConf.name); return html`
{ const stateObj = this.hass!.states[entity.entity]; this._names[entity.entity] = stateObj - ? computeLovelaceEntityName(this.hass!, stateObj, entity.name) + ? this.hass!.formatEntityName(stateObj, entity.name) : entity.entity; }); } diff --git a/src/panels/lovelace/cards/hui-humidifier-card.ts b/src/panels/lovelace/cards/hui-humidifier-card.ts index aa0e33439d..448ef76279 100644 --- a/src/panels/lovelace/cards/hui-humidifier-card.ts +++ b/src/panels/lovelace/cards/hui-humidifier-card.ts @@ -14,7 +14,6 @@ import "../../../state-control/humidifier/ha-state-control-humidifier-humidity"; import type { HomeAssistant } from "../../../types"; import "../card-features/hui-card-features"; import type { LovelaceCardFeatureContext } from "../card-features/types"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { @@ -133,11 +132,7 @@ export class HuiHumidifierCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const color = stateColorCss(stateObj); diff --git a/src/panels/lovelace/cards/hui-light-card.ts b/src/panels/lovelace/cards/hui-light-card.ts index 8df73d7ff0..41d1a38d24 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -17,7 +17,6 @@ import { lightSupportsBrightness } from "../../../data/light"; import type { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -92,11 +91,7 @@ export class HuiLightCard extends LitElement implements LovelaceCard { ((stateObj.attributes.brightness || 0) / 255) * 100 ); - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); return html` diff --git a/src/panels/lovelace/cards/hui-media-control-card.ts b/src/panels/lovelace/cards/hui-media-control-card.ts index ec46140079..25bcfb79ad 100644 --- a/src/panels/lovelace/cards/hui-media-control-card.ts +++ b/src/panels/lovelace/cards/hui-media-control-card.ts @@ -36,7 +36,6 @@ import { mediaPlayerPlayMedia, } from "../../../data/media-player"; import type { HomeAssistant } from "../../../types"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import "../components/hui-marquee"; @@ -242,8 +241,7 @@ export class HuiMediaControlCard extends LitElement implements LovelaceCard { .hass=${this.hass} >
- ${computeLovelaceEntityName( - this.hass, + ${this.hass.formatEntityName( this.hass!.states[this._config!.entity], this._config.name )} diff --git a/src/panels/lovelace/cards/hui-picture-entity-card.ts b/src/panels/lovelace/cards/hui-picture-entity-card.ts index 3eece413fe..dc2fe7fad3 100644 --- a/src/panels/lovelace/cards/hui-picture-entity-card.ts +++ b/src/panels/lovelace/cards/hui-picture-entity-card.ts @@ -13,7 +13,6 @@ import type { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { PersonEntity } from "../../../data/person"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -126,11 +125,7 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const entityState = this.hass.formatEntityState(stateObj); let footer: TemplateResult | string = ""; diff --git a/src/panels/lovelace/cards/hui-plant-status-card.ts b/src/panels/lovelace/cards/hui-plant-status-card.ts index 7fb3b924c2..cf3996d52e 100644 --- a/src/panels/lovelace/cards/hui-plant-status-card.ts +++ b/src/panels/lovelace/cards/hui-plant-status-card.ts @@ -15,7 +15,6 @@ import "../../../components/ha-card"; import "../../../components/ha-svg-icon"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { createEntityNotFoundWarning } from "../components/hui-warning"; @@ -119,7 +118,7 @@ class HuiPlantStatusCard extends LitElement implements LovelaceCard { style="background-image:url(${stateObj.attributes.entity_picture})" >
- ${computeLovelaceEntityName(this.hass, stateObj, this._config.name)} + ${this.hass.formatEntityName(stateObj, this._config.name)}
diff --git a/src/panels/lovelace/cards/hui-statistic-card.ts b/src/panels/lovelace/cards/hui-statistic-card.ts index 27bc35e850..ca227c4a42 100644 --- a/src/panels/lovelace/cards/hui-statistic-card.ts +++ b/src/panels/lovelace/cards/hui-statistic-card.ts @@ -23,7 +23,6 @@ import { } from "../../../data/recorder"; import type { HomeAssistant } from "../../../types"; import { computeCardSize } from "../common/compute-card-size"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { createHeaderFooterElement } from "../create-element/create-header-footer-element"; @@ -200,7 +199,7 @@ export class HuiStatisticCard extends LitElement implements LovelaceCard { const stateObj = this.hass.states[this._config.entity]; const name = (this._config.name - ? computeLovelaceEntityName(this.hass, stateObj, this._config.name) + ? this.hass.formatEntityName(stateObj, this._config.name) : "") || getStatisticLabel(this.hass, this._config.entity, this._metadata); diff --git a/src/panels/lovelace/cards/hui-statistics-graph-card.ts b/src/panels/lovelace/cards/hui-statistics-graph-card.ts index 27396ba1b5..dc074b03db 100644 --- a/src/panels/lovelace/cards/hui-statistics-graph-card.ts +++ b/src/panels/lovelace/cards/hui-statistics-graph-card.ts @@ -24,7 +24,6 @@ import { getStatisticMetadata, } from "../../../data/recorder"; import type { HomeAssistant } from "../../../types"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntitiesChanged } from "../common/has-changed"; import { processConfigEntities } from "../common/process-config-entities"; @@ -189,8 +188,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard { this._entities.forEach((config) => { const stateObj = this.hass!.states[config.entity]; this._names[config.entity] = - computeLovelaceEntityName(this.hass!, stateObj, config.name) || - config.entity; + this.hass!.formatEntityName(stateObj, config.name) || config.entity; }); } diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts index a19e7af004..772ee39350 100644 --- a/src/panels/lovelace/cards/hui-thermostat-card.ts +++ b/src/panels/lovelace/cards/hui-thermostat-card.ts @@ -15,7 +15,6 @@ import "../../../state-control/water_heater/ha-state-control-water_heater-temper import type { HomeAssistant } from "../../../types"; import "../card-features/hui-card-features"; import type { LovelaceCardFeatureContext } from "../card-features/types"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { @@ -132,11 +131,7 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard { } const domain = computeDomain(stateObj.entity_id); - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const color = stateColorCss(stateObj); diff --git a/src/panels/lovelace/cards/hui-tile-card.ts b/src/panels/lovelace/cards/hui-tile-card.ts index 71f2510831..883b0e8601 100644 --- a/src/panels/lovelace/cards/hui-tile-card.ts +++ b/src/panels/lovelace/cards/hui-tile-card.ts @@ -23,7 +23,6 @@ import "../../../state-display/state-display"; import type { HomeAssistant } from "../../../types"; import "../card-features/hui-card-features"; import type { LovelaceCardFeatureContext } from "../card-features/types"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -252,11 +251,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard { `; } - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const active = stateActive(stateObj); const color = this._computeStateColor(stateObj, this._config.color); diff --git a/src/panels/lovelace/cards/hui-weather-forecast-card.ts b/src/panels/lovelace/cards/hui-weather-forecast-card.ts index 770038daeb..163bd63aa8 100644 --- a/src/panels/lovelace/cards/hui-weather-forecast-card.ts +++ b/src/panels/lovelace/cards/hui-weather-forecast-card.ts @@ -29,7 +29,6 @@ import { } from "../../../data/weather"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -231,7 +230,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { return html` ${this.hass.localize("ui.panel.lovelace.warning.entity_unavailable", { - entity: `${computeLovelaceEntityName(this.hass, stateObj, this._config.name)} (${this._config.entity})`, + entity: `${this.hass.formatEntityName(stateObj, this._config.name)} (${this._config.entity})`, })} `; @@ -262,11 +261,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { const dayNight = forecastData?.type === "twice_daily"; const weatherStateIcon = getWeatherStateIcon(stateObj.state, this); - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this._config.name - ); + const name = this.hass.formatEntityName(stateObj, this._config.name); const temperatureFractionDigits = this._config.round_temperature ? 0 diff --git a/src/panels/lovelace/cards/types.ts b/src/panels/lovelace/cards/types.ts index d5fd108bc2..ac4a9bbe0c 100644 --- a/src/panels/lovelace/cards/types.ts +++ b/src/panels/lovelace/cards/types.ts @@ -155,7 +155,7 @@ export interface AreaCardConfig extends LovelaceCardConfig { export interface ButtonCardConfig extends LovelaceCardConfig { entity?: string; - name?: string; + name?: string | EntityNameItem | EntityNameItem[]; show_name?: boolean; icon?: string; icon_height?: string; diff --git a/src/panels/lovelace/common/entity/compute-lovelace-entity-name.ts b/src/panels/lovelace/common/entity/compute-lovelace-entity-name.ts deleted file mode 100644 index 7fc2afc096..0000000000 --- a/src/panels/lovelace/common/entity/compute-lovelace-entity-name.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { HassEntity } from "home-assistant-js-websocket"; -import { ensureArray } from "../../../../common/array/ensure-array"; -import { - DEFAULT_ENTITY_NAME, - type EntityNameItem, -} from "../../../../common/entity/compute_entity_name_display"; -import type { HomeAssistant } from "../../../../types"; - -/** - * Computes the display name for an entity in Lovelace (cards and badges). - * - * @param hass - The Home Assistant instance - * @param stateObj - The entity state object - * @param config - The name configuration (string for override, or EntityNameItem[] for structured naming) - * @returns The computed entity name - */ -export const computeLovelaceEntityName = ( - hass: HomeAssistant, - stateObj: HassEntity | undefined, - config: string | EntityNameItem | EntityNameItem[] | undefined -): string => { - if (typeof config === "string") { - return config; - } - if (stateObj) { - return hass.formatEntityName(stateObj, config ?? DEFAULT_ENTITY_NAME); - } - if (!config) { - return ""; - } - // If entity is not found, fall back to text parts in config - // This allows for static names even when the entity is missing - // e.g. for a card that doesn't require an entity - const textParts = ensureArray(config) - .filter((item) => item.type === "text") - .map((item) => ("text" in item ? item.text : "")); - if (textParts.length) { - return textParts.join(" "); - } - return ""; -}; diff --git a/src/panels/lovelace/common/generate-lovelace-config.ts b/src/panels/lovelace/common/generate-lovelace-config.ts index c4dd7bca44..175d00f094 100644 --- a/src/panels/lovelace/common/generate-lovelace-config.ts +++ b/src/panels/lovelace/common/generate-lovelace-config.ts @@ -32,7 +32,6 @@ import type { } from "../cards/types"; import type { EntityConfig } from "../entity-rows/types"; import type { ButtonsHeaderFooterConfig } from "../header-footer/types"; -import { computeLovelaceEntityName } from "./entity/compute-lovelace-entity-name"; const HIDE_DOMAIN = new Set([ "ai_task", @@ -271,14 +270,14 @@ export const computeCards = ( ? computeStateName(states[a]) : "" : states[a.entity] - ? computeLovelaceEntityName(hass, states[a.entity], a.name) + ? hass.formatEntityName(states[a.entity], a.name) : "", typeof b === "string" ? states[b] ? computeStateName(states[b]) : "" : states[b.entity] - ? computeLovelaceEntityName(hass, states[b.entity], b.name) + ? hass.formatEntityName(states[b.entity], b.name) : "" ); }); diff --git a/src/panels/lovelace/components/hui-generic-entity-row.ts b/src/panels/lovelace/components/hui-generic-entity-row.ts index c99b847640..4aafbdea67 100644 --- a/src/panels/lovelace/components/hui-generic-entity-row.ts +++ b/src/panels/lovelace/components/hui-generic-entity-row.ts @@ -18,7 +18,6 @@ import type { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { HomeAssistant } from "../../../types"; import type { EntitiesCardEntityConfig } from "../cards/types"; import { actionHandler } from "../common/directives/action-handler-directive"; -import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name"; import { handleAction } from "../common/handle-action"; import { hasAction, hasAnyAction } from "../common/has-action"; import { createEntityNotFoundWarning } from "./hui-warning"; @@ -66,11 +65,7 @@ export class HuiGenericEntityRow extends LitElement { const pointer = hasAnyAction(this.config); const hasSecondary = this.secondaryText || this.config.secondary_info; - const name = computeLovelaceEntityName( - this.hass, - stateObj, - this.config.name - ); + const name = this.hass.formatEntityName(stateObj, this.config.name); return html`
{ + it("returns string name directly", () => { + const stateObj = mockStateObj({ entity_id: "light.kitchen" }); + const hass = { + entities: {}, + devices: {}, + areas: {}, + floors: {}, + } as unknown as HomeAssistant; + + const result = computeEntityNameDisplay( + stateObj, + "Custom Name", + hass.entities, + hass.devices, + hass.areas, + hass.floors + ); + + expect(result).toBe("Custom Name"); + }); + it("returns text when all items are text", () => { const stateObj = mockStateObj({ entity_id: "light.kitchen" }); const hass = { diff --git a/test/panels/lovelace/common/entity/compute-lovelace-entity-name.test.ts b/test/panels/lovelace/common/entity/compute-lovelace-entity-name.test.ts deleted file mode 100644 index 106a8b3d99..0000000000 --- a/test/panels/lovelace/common/entity/compute-lovelace-entity-name.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { computeLovelaceEntityName } from "../../../../../src/panels/lovelace/common/entity/compute-lovelace-entity-name"; -import type { HomeAssistant } from "../../../../../src/types"; -import { mockStateObj } from "../../../../common/entity/context/context-mock"; - -const createMockHass = ( - mockFormatEntityName: ReturnType -): HomeAssistant => - ({ - formatEntityName: mockFormatEntityName, - }) as unknown as HomeAssistant; - -describe("computeLovelaceEntityName", () => { - it("returns the string directly when nameConfig is a string", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const stateObj = mockStateObj({ entity_id: "light.kitchen" }); - - const result = computeLovelaceEntityName(hass, stateObj, "Custom Name"); - - expect(result).toBe("Custom Name"); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("returns empty string when nameConfig is empty string", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const stateObj = mockStateObj({ - entity_id: "light.kitchen", - attributes: { friendly_name: "Kitchen Light" }, - }); - - const result = computeLovelaceEntityName(hass, stateObj, ""); - - expect(result).toBe(""); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("calls formatEntityName with DEFAULT_ENTITY_NAME when nameConfig is undefined", () => { - const mockFormatEntityName = vi.fn(() => "Formatted Name"); - const hass = createMockHass(mockFormatEntityName); - const stateObj = mockStateObj({ - entity_id: "light.kitchen", - attributes: { friendly_name: "Kitchen Light" }, - }); - - const result = computeLovelaceEntityName(hass, stateObj, undefined); - - expect(result).toBe("Formatted Name"); - expect(mockFormatEntityName).toHaveBeenCalledTimes(1); - expect(mockFormatEntityName).toHaveBeenCalledWith(stateObj, [ - { type: "device" }, - { type: "entity" }, - ]); - }); - - it("calls formatEntityName with EntityNameItem config", () => { - const mockFormatEntityName = vi.fn(() => "Formatted Name"); - const hass = createMockHass(mockFormatEntityName); - const stateObj = mockStateObj({ entity_id: "light.bedroom" }); - const nameConfig = { type: "device" as const }; - - const result = computeLovelaceEntityName(hass, stateObj, nameConfig); - - expect(result).toBe("Formatted Name"); - expect(mockFormatEntityName).toHaveBeenCalledTimes(1); - expect(mockFormatEntityName).toHaveBeenCalledWith(stateObj, nameConfig); - }); - - it("calls formatEntityName with array of EntityNameItems", () => { - const mockFormatEntityName = vi.fn(() => "Formatted Name"); - const hass = createMockHass(mockFormatEntityName); - const stateObj = mockStateObj({ entity_id: "light.kitchen" }); - const nameConfig = [ - { type: "device" as const }, - { type: "entity" as const }, - ]; - - const result = computeLovelaceEntityName(hass, stateObj, nameConfig); - - expect(result).toBe("Formatted Name"); - expect(mockFormatEntityName).toHaveBeenCalledTimes(1); - expect(mockFormatEntityName).toHaveBeenCalledWith(stateObj, nameConfig); - }); - - describe("when stateObj is undefined", () => { - it("returns empty string when nameConfig is undefined", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - - const result = computeLovelaceEntityName(hass, undefined, undefined); - - expect(result).toBe(""); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("returns text from single text EntityNameItem", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const nameConfig = { type: "text" as const, text: "Custom Text" }; - - const result = computeLovelaceEntityName(hass, undefined, nameConfig); - - expect(result).toBe("Custom Text"); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("returns joined text from multiple text EntityNameItems", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const nameConfig = [ - { type: "text" as const, text: "First" }, - { type: "text" as const, text: "Second" }, - ]; - - const result = computeLovelaceEntityName(hass, undefined, nameConfig); - - expect(result).toBe("First Second"); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("returns only text items when mixed with non-text items", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const nameConfig = [ - { type: "text" as const, text: "Prefix" }, - { type: "device" as const }, - { type: "text" as const, text: "Suffix" }, - { type: "entity" as const }, - ]; - - const result = computeLovelaceEntityName(hass, undefined, nameConfig); - - expect(result).toBe("Prefix Suffix"); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - - it("returns empty string when no text items in config", () => { - const mockFormatEntityName = vi.fn(); - const hass = createMockHass(mockFormatEntityName); - const nameConfig = [ - { type: "device" as const }, - { type: "entity" as const }, - ]; - - const result = computeLovelaceEntityName(hass, undefined, nameConfig); - - expect(result).toBe(""); - expect(mockFormatEntityName).not.toHaveBeenCalled(); - }); - }); -});