mirror of
https://github.com/home-assistant/frontend.git
synced 2026-04-19 16:31:31 +01:00
Revert entity naming change (#30384)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import * as computeDeviceNameModule from "../../../src/common/entity/compute_device_name";
|
||||
import {
|
||||
computeEntityEntryName,
|
||||
computeEntityName,
|
||||
} from "../../../src/common/entity/compute_entity_name";
|
||||
import * as computeStateNameModule from "../../../src/common/entity/compute_state_name";
|
||||
import * as stripPrefixModule from "../../../src/common/entity/strip_prefix_from_entity_name";
|
||||
import type { HomeAssistant } from "../../../src/types";
|
||||
import {
|
||||
mockEntity,
|
||||
@@ -23,8 +25,11 @@ describe("computeEntityName", () => {
|
||||
});
|
||||
const hass = {
|
||||
entities: {},
|
||||
devices: {},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityName(stateObj, hass.entities)).toBe("Kitchen Light");
|
||||
expect(computeEntityName(stateObj, hass.entities, hass.devices)).toBe(
|
||||
"Kitchen Light"
|
||||
);
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
@@ -42,47 +47,77 @@ describe("computeEntityName", () => {
|
||||
labels: [],
|
||||
},
|
||||
},
|
||||
devices: {},
|
||||
states: {
|
||||
"light.kitchen": stateObj,
|
||||
},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityName(stateObj, hass.entities)).toBe("Ceiling Light");
|
||||
expect(computeEntityName(stateObj, hass.entities, hass.devices)).toBe(
|
||||
"Ceiling Light"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeEntityEntryName", () => {
|
||||
it("returns entry.name if present", () => {
|
||||
it("returns entry.name if no device", () => {
|
||||
const entry = mockEntity({
|
||||
entity_id: "light.kitchen",
|
||||
name: "Ceiling Light",
|
||||
});
|
||||
expect(computeEntityEntryName(entry)).toBe("Ceiling Light");
|
||||
const hass = { devices: {}, states: {} };
|
||||
expect(computeEntityEntryName(entry, hass.devices)).toBe("Ceiling Light");
|
||||
});
|
||||
|
||||
it("returns entity name as-is when device present", () => {
|
||||
it("returns device-stripped name if device present", () => {
|
||||
vi.spyOn(computeDeviceNameModule, "computeDeviceName").mockReturnValue(
|
||||
"Kitchen"
|
||||
);
|
||||
vi.spyOn(stripPrefixModule, "stripPrefixFromEntityName").mockImplementation(
|
||||
(name, prefix) => name.replace(prefix + " ", "")
|
||||
);
|
||||
const entry = mockEntity({
|
||||
entity_id: "light.kitchen",
|
||||
name: "Light",
|
||||
name: "Kitchen Light",
|
||||
device_id: "dev1",
|
||||
});
|
||||
expect(computeEntityEntryName(entry)).toBe("Light");
|
||||
const hass = {
|
||||
devices: { dev1: {} },
|
||||
states: {},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityEntryName(entry, hass.devices)).toBe("Light");
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("returns undefined if entity has no name (uses device name)", () => {
|
||||
it("returns undefined if device name equals entity name", () => {
|
||||
vi.spyOn(computeDeviceNameModule, "computeDeviceName").mockReturnValue(
|
||||
"Kitchen Light"
|
||||
);
|
||||
const entry = mockEntity({
|
||||
entity_id: "light.kitchen",
|
||||
name: "Kitchen Light",
|
||||
device_id: "dev1",
|
||||
});
|
||||
expect(computeEntityEntryName(entry)).toBeUndefined();
|
||||
const hass = {
|
||||
devices: { dev1: {} },
|
||||
states: {},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityEntryName(entry, hass.devices)).toBeUndefined();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("returns empty string if name is empty (does not fallback to original_name)", () => {
|
||||
const entry = mockEntityEntry({
|
||||
entity_id: "light.kitchen",
|
||||
name: "",
|
||||
original_name: "Old Name",
|
||||
});
|
||||
expect(computeEntityEntryName(entry)).toBe("");
|
||||
it("falls back to state name if no name and no device", () => {
|
||||
vi.spyOn(computeStateNameModule, "computeStateName").mockReturnValue(
|
||||
"Fallback Name"
|
||||
);
|
||||
const entry = mockEntity({ entity_id: "light.kitchen" });
|
||||
const hass = {
|
||||
devices: {},
|
||||
} as unknown as HomeAssistant;
|
||||
const stateObj = mockStateObj({ entity_id: "light.kitchen" });
|
||||
expect(computeEntityEntryName(entry, hass.devices, stateObj)).toBe(
|
||||
"Fallback Name"
|
||||
);
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("returns original_name if present", () => {
|
||||
@@ -90,15 +125,27 @@ describe("computeEntityEntryName", () => {
|
||||
entity_id: "light.kitchen",
|
||||
original_name: "Old Name",
|
||||
});
|
||||
expect(computeEntityEntryName(entry)).toBe("Old Name");
|
||||
const hass = {
|
||||
devices: {},
|
||||
states: {},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityEntryName(entry, hass.devices)).toBe("Old Name");
|
||||
});
|
||||
|
||||
it("returns undefined if no name or original_name", () => {
|
||||
it("returns undefined if no name, original_name, or device", () => {
|
||||
const entry = mockEntity({ entity_id: "light.kitchen" });
|
||||
expect(computeEntityEntryName(entry)).toBeUndefined();
|
||||
const hass = {
|
||||
devices: {},
|
||||
states: {},
|
||||
} as unknown as HomeAssistant;
|
||||
expect(computeEntityEntryName(entry, hass.devices)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("handles entities with numeric original_name (real bug from issue #25363)", () => {
|
||||
vi.spyOn(computeDeviceNameModule, "computeDeviceName").mockReturnValue(
|
||||
"Texas Instruments CC2652"
|
||||
);
|
||||
|
||||
const entry = {
|
||||
entity_id: "sensor.texas_instruments_cc2652_2",
|
||||
name: null, // null name
|
||||
@@ -106,21 +153,37 @@ describe("computeEntityEntryName", () => {
|
||||
device_id: "dev1",
|
||||
has_entity_name: true,
|
||||
};
|
||||
const hass = {
|
||||
devices: { dev1: {} },
|
||||
states: {},
|
||||
};
|
||||
|
||||
// Should not throw an error and should return the stringified number
|
||||
expect(() => computeEntityEntryName(entry as any)).not.toThrow();
|
||||
expect(computeEntityEntryName(entry as any)).toBe("2");
|
||||
expect(() =>
|
||||
computeEntityEntryName(entry as any, hass as any)
|
||||
).not.toThrow();
|
||||
expect(computeEntityEntryName(entry as any, hass as any)).toBe("2");
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("returns undefined when entity has device but no name or original_name", () => {
|
||||
vi.spyOn(computeDeviceNameModule, "computeDeviceName").mockReturnValue(
|
||||
"Kitchen Device"
|
||||
);
|
||||
|
||||
const entry = {
|
||||
entity_id: "sensor.kitchen_sensor",
|
||||
// No name property
|
||||
// No original_name property
|
||||
device_id: "dev1",
|
||||
};
|
||||
const hass = {
|
||||
devices: { dev1: {} },
|
||||
states: {},
|
||||
};
|
||||
|
||||
// Should return undefined to maintain function contract
|
||||
expect(computeEntityEntryName(entry as any)).toBeUndefined();
|
||||
expect(computeEntityEntryName(entry as any, hass as any)).toBeUndefined();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user