mirror of
https://github.com/home-assistant/frontend.git
synced 2026-04-18 07:56:44 +01:00
Use explicit default name in entity name picker and lovelace cards (#30189)
This commit is contained in:
@@ -6,7 +6,10 @@ import { repeat } from "lit/directives/repeat";
|
|||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { ensureArray } from "../../common/array/ensure-array";
|
import { ensureArray } from "../../common/array/ensure-array";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import type { EntityNameItem } from "../../common/entity/compute_entity_name_display";
|
import {
|
||||||
|
DEFAULT_ENTITY_NAME,
|
||||||
|
type EntityNameItem,
|
||||||
|
} from "../../common/entity/compute_entity_name_display";
|
||||||
import { getEntityContext } from "../../common/entity/context/get_entity_context";
|
import { getEntityContext } from "../../common/entity/context/get_entity_context";
|
||||||
import type { EntityNameType } from "../../common/translations/entity-state";
|
import type { EntityNameType } from "../../common/translations/entity-state";
|
||||||
import type { LocalizeKeys } from "../../common/translations/localize";
|
import type { LocalizeKeys } from "../../common/translations/localize";
|
||||||
@@ -293,13 +296,13 @@ export class HaEntityNamePicker extends LitElement {
|
|||||||
}
|
}
|
||||||
return [{ type: "text", text: value } satisfies EntityNameItem];
|
return [{ type: "text", text: value } satisfies EntityNameItem];
|
||||||
}
|
}
|
||||||
return value ? ensureArray(value) : [];
|
return value ? ensureArray(value) : [...DEFAULT_ENTITY_NAME];
|
||||||
});
|
});
|
||||||
|
|
||||||
private _toValue = memoizeOne(
|
private _toValue = memoizeOne(
|
||||||
(items: EntityNameItem[]): typeof this.value => {
|
(items: EntityNameItem[]): typeof this.value => {
|
||||||
if (items.length === 0) {
|
if (items.length === 0) {
|
||||||
return undefined;
|
return "";
|
||||||
}
|
}
|
||||||
if (items.length === 1) {
|
if (items.length === 1) {
|
||||||
const item = items[0];
|
const item = items[0];
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import type { HassEntity } from "home-assistant-js-websocket";
|
import type { HassEntity } from "home-assistant-js-websocket";
|
||||||
import { ensureArray } from "../../../../common/array/ensure-array";
|
import { ensureArray } from "../../../../common/array/ensure-array";
|
||||||
import type { EntityNameItem } from "../../../../common/entity/compute_entity_name_display";
|
import {
|
||||||
import { computeStateName } from "../../../../common/entity/compute_state_name";
|
DEFAULT_ENTITY_NAME,
|
||||||
|
type EntityNameItem,
|
||||||
|
} from "../../../../common/entity/compute_entity_name_display";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,15 +19,14 @@ export const computeLovelaceEntityName = (
|
|||||||
stateObj: HassEntity | undefined,
|
stateObj: HassEntity | undefined,
|
||||||
config: string | EntityNameItem | EntityNameItem[] | undefined
|
config: string | EntityNameItem | EntityNameItem[] | undefined
|
||||||
): string => {
|
): string => {
|
||||||
// If no config is provided, fall back to the default state name
|
if (typeof config === "string") {
|
||||||
if (!config) {
|
return config;
|
||||||
return stateObj ? computeStateName(stateObj) : "";
|
|
||||||
}
|
|
||||||
if (typeof config !== "object") {
|
|
||||||
return String(config);
|
|
||||||
}
|
}
|
||||||
if (stateObj) {
|
if (stateObj) {
|
||||||
return hass.formatEntityName(stateObj, config);
|
return hass.formatEntityName(stateObj, config ?? DEFAULT_ENTITY_NAME);
|
||||||
|
}
|
||||||
|
if (!config) {
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
// If entity is not found, fall back to text parts in config
|
// If entity is not found, fall back to text parts in config
|
||||||
// This allows for static names even when the entity is missing
|
// This allows for static names even when the entity is missing
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ describe("computeLovelaceEntityName", () => {
|
|||||||
expect(mockFormatEntityName).not.toHaveBeenCalled();
|
expect(mockFormatEntityName).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("return state name when nameConfig is empty string", () => {
|
it("returns empty string when nameConfig is empty string", () => {
|
||||||
const mockFormatEntityName = vi.fn();
|
const mockFormatEntityName = vi.fn();
|
||||||
const hass = createMockHass(mockFormatEntityName);
|
const hass = createMockHass(mockFormatEntityName);
|
||||||
const stateObj = mockStateObj({
|
const stateObj = mockStateObj({
|
||||||
@@ -32,11 +32,11 @@ describe("computeLovelaceEntityName", () => {
|
|||||||
|
|
||||||
const result = computeLovelaceEntityName(hass, stateObj, "");
|
const result = computeLovelaceEntityName(hass, stateObj, "");
|
||||||
|
|
||||||
expect(result).toBe("Kitchen Light");
|
expect(result).toBe("");
|
||||||
expect(mockFormatEntityName).not.toHaveBeenCalled();
|
expect(mockFormatEntityName).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("return state name when nameConfig is undefined", () => {
|
it("calls formatEntityName with DEFAULT_ENTITY_NAME when nameConfig is undefined", () => {
|
||||||
const mockFormatEntityName = vi.fn(() => "Formatted Name");
|
const mockFormatEntityName = vi.fn(() => "Formatted Name");
|
||||||
const hass = createMockHass(mockFormatEntityName);
|
const hass = createMockHass(mockFormatEntityName);
|
||||||
const stateObj = mockStateObj({
|
const stateObj = mockStateObj({
|
||||||
@@ -46,8 +46,12 @@ describe("computeLovelaceEntityName", () => {
|
|||||||
|
|
||||||
const result = computeLovelaceEntityName(hass, stateObj, undefined);
|
const result = computeLovelaceEntityName(hass, stateObj, undefined);
|
||||||
|
|
||||||
expect(result).toBe("Kitchen Light");
|
expect(result).toBe("Formatted Name");
|
||||||
expect(mockFormatEntityName).not.toHaveBeenCalled();
|
expect(mockFormatEntityName).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockFormatEntityName).toHaveBeenCalledWith(stateObj, [
|
||||||
|
{ type: "device" },
|
||||||
|
{ type: "entity" },
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("calls formatEntityName with EntityNameItem config", () => {
|
it("calls formatEntityName with EntityNameItem config", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user