diff --git a/src/panels/lovelace/cards/hui-humidifier-card.ts b/src/panels/lovelace/cards/hui-humidifier-card.ts index 4babf6ccf5..8078331f7f 100644 --- a/src/panels/lovelace/cards/hui-humidifier-card.ts +++ b/src/panels/lovelace/cards/hui-humidifier-card.ts @@ -6,7 +6,6 @@ import { customElement, property, state } from "lit/decorators"; import { styleMap } from "lit/directives/style-map"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { fireEvent } from "../../../common/dom/fire_event"; -import { computeStateName } from "../../../common/entity/compute_state_name"; import { stateColorCss } from "../../../common/entity/state_color"; import "../../../components/ha-card"; import "../../../components/ha-icon-button"; @@ -15,6 +14,7 @@ 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 { computeCardEntityName } from "../common/entity/compute-card-entity-name"; import { findEntities } from "../common/find-entities"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { @@ -133,7 +133,7 @@ export class HuiHumidifierCard extends LitElement implements LovelaceCard { `; } - const name = this._config!.name || computeStateName(stateObj); + const name = computeCardEntityName(this.hass, 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 018b671198..a78416b766 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -7,7 +7,6 @@ import { classMap } from "lit/directives/class-map"; import { styleMap } from "lit/directives/style-map"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { fireEvent } from "../../../common/dom/fire_event"; -import { computeStateName } from "../../../common/entity/compute_state_name"; import { stateColorBrightness } from "../../../common/entity/state_color"; import "../../../components/ha-card"; import "../../../components/ha-icon-button"; @@ -18,6 +17,7 @@ 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 { computeCardEntityName } from "../common/entity/compute-card-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -92,7 +92,7 @@ export class HuiLightCard extends LitElement implements LovelaceCard { ((stateObj.attributes.brightness || 0) / 255) * 100 ); - const name = this._config.name ?? computeStateName(stateObj); + const name = computeCardEntityName(this.hass, stateObj, this._config.name); return html` diff --git a/src/panels/lovelace/cards/hui-plant-status-card.ts b/src/panels/lovelace/cards/hui-plant-status-card.ts index 897c03b4fc..bac18224ef 100644 --- a/src/panels/lovelace/cards/hui-plant-status-card.ts +++ b/src/panels/lovelace/cards/hui-plant-status-card.ts @@ -11,11 +11,11 @@ import { customElement, property, state } from "lit/decorators"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { fireEvent } from "../../../common/dom/fire_event"; import { batteryLevelIcon } from "../../../common/entity/battery_icon"; -import { computeStateName } from "../../../common/entity/compute_state_name"; import "../../../components/ha-card"; import "../../../components/ha-svg-icon"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; +import { computeCardEntityName } from "../common/entity/compute-card-entity-name"; import { findEntities } from "../common/find-entities"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { createEntityNotFoundWarning } from "../components/hui-warning"; @@ -119,7 +119,7 @@ class HuiPlantStatusCard extends LitElement implements LovelaceCard { style="background-image:url(${stateObj.attributes.entity_picture})" >
- ${this._config.name || computeStateName(stateObj)} + ${computeCardEntityName(this.hass, stateObj, this._config.name)}
diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts index 9fad7a09de..e262ec3966 100644 --- a/src/panels/lovelace/cards/hui-thermostat-card.ts +++ b/src/panels/lovelace/cards/hui-thermostat-card.ts @@ -7,7 +7,6 @@ import { styleMap } from "lit/directives/style-map"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { fireEvent } from "../../../common/dom/fire_event"; import { computeDomain } from "../../../common/entity/compute_domain"; -import { computeStateName } from "../../../common/entity/compute_state_name"; import { stateColorCss } from "../../../common/entity/state_color"; import "../../../components/ha-card"; import "../../../components/ha-icon-button"; @@ -16,6 +15,7 @@ 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 { computeCardEntityName } from "../common/entity/compute-card-entity-name"; import { findEntities } from "../common/find-entities"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { @@ -132,7 +132,7 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard { } const domain = computeDomain(stateObj.entity_id); - const name = this._config!.name || computeStateName(stateObj); + const name = computeCardEntityName(this.hass, stateObj, this._config.name); const color = stateColorCss(stateObj); diff --git a/src/panels/lovelace/cards/hui-weather-forecast-card.ts b/src/panels/lovelace/cards/hui-weather-forecast-card.ts index ccb852619c..2a39dbee36 100644 --- a/src/panels/lovelace/cards/hui-weather-forecast-card.ts +++ b/src/panels/lovelace/cards/hui-weather-forecast-card.ts @@ -7,7 +7,6 @@ import { classMap } from "lit/directives/class-map"; import { formatDateWeekdayShort } from "../../../common/datetime/format_date"; import { formatTime } from "../../../common/datetime/format_time"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; -import { computeStateName } from "../../../common/entity/compute_state_name"; import { isValidEntityId } from "../../../common/entity/valid_entity_id"; import { formatNumber } from "../../../common/number/format_number"; import "../../../components/ha-card"; @@ -27,6 +26,7 @@ import { } from "../../../data/weather"; import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; +import { computeCardEntityName } from "../common/entity/compute-card-entity-name"; import { findEntities } from "../common/find-entities"; import { handleAction } from "../common/handle-action"; import { hasAction } from "../common/has-action"; @@ -229,7 +229,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { return html` ${this.hass.localize("ui.panel.lovelace.warning.entity_unavailable", { - entity: `${computeStateName(stateObj)} (${this._config.entity})`, + entity: `${computeCardEntityName(this.hass, stateObj, this._config.name)} (${this._config.entity})`, })} `; @@ -260,7 +260,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { const dayNight = forecastData?.type === "twice_daily"; const weatherStateIcon = getWeatherStateIcon(stateObj.state, this); - const name = this._config.name ?? computeStateName(stateObj); + const name = computeCardEntityName(this.hass, stateObj, this._config.name); return html` +): HomeAssistant => + ({ + formatEntityName: mockFormatEntityName, + }) as unknown as HomeAssistant; + +describe("computeCardEntityName", () => { + 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 = computeCardEntityName(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" }); + + const result = computeCardEntityName(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" }); + + const result = computeCardEntityName(hass, stateObj, undefined); + + expect(result).toBe("Formatted Name"); + expect(mockFormatEntityName).toHaveBeenCalledTimes(1); + expect(mockFormatEntityName).toHaveBeenCalledWith( + stateObj, + DEFAULT_ENTITY_NAME + ); + }); + + 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 = computeCardEntityName(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 = computeCardEntityName(hass, stateObj, nameConfig); + + expect(result).toBe("Formatted Name"); + expect(mockFormatEntityName).toHaveBeenCalledTimes(1); + expect(mockFormatEntityName).toHaveBeenCalledWith(stateObj, nameConfig); + }); +});