Files
frontend/test/common/entity/context/get_entity_context.test.ts
T
Paul Bottein ec4bfc9255 Add parent device to entity context (#53766)
* Add parent device to entity context

* Fix parent device context display edge cases

* Fix parent device context in suggestions and completions

* Order entity name parts by hierarchy

* Remove unused parent device missing string

* Add parent device to logbook entry context

* Show inherited area and parent device for device targets

* Test parent device search in the entity tree

* Show inherited area and parent device in the automation target header

* Move the device domain lookup out of target row item data

* Pass registries to memoized entity name lists

* Document parent device in logbook name detail
2026-09-10 13:38:39 +02:00

265 lines
5.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { getEntityContext } from "../../../../src/common/entity/context/get_entity_context";
import type { HomeAssistant } from "../../../../src/types";
import {
mockArea,
mockDevice,
mockEntity,
mockFloor,
mockStateObj,
} from "./context-mock";
describe("getEntityContext", () => {
it("should return the correct context when the entity exists without device or area", () => {
const entity = mockEntity({
entity_id: "light.living_room",
});
const stateObj = mockStateObj({
entity_id: "light.living_room",
});
const hass = {
entities: {
"light.living_room": entity,
},
devices: {},
areas: {},
floors: {},
} as unknown as HomeAssistant;
const result = getEntityContext(
stateObj,
hass.entities,
hass.devices,
hass.areas,
hass.floors
);
expect(result).toEqual({
entity,
device: null,
parentDevice: null,
area: null,
floor: null,
});
});
it("should return the correct context when the entity has a device and area", () => {
const entity = mockEntity({
entity_id: "light.living_room",
device_id: "device_1",
});
const device = mockDevice({
id: "device_1",
area_id: "area_1",
});
const area = mockArea({
area_id: "area_1",
floor_id: "floor_1",
});
const floor = mockFloor({
floor_id: "floor_1",
});
const stateObj = mockStateObj({
entity_id: "light.living_room",
});
const hass = {
entities: {
"light.living_room": entity,
},
devices: {
device_1: device,
},
areas: {
area_1: area,
},
floors: {
floor_1: floor,
},
} as unknown as HomeAssistant;
const result = getEntityContext(
stateObj,
hass.entities,
hass.devices,
hass.areas,
hass.floors
);
expect(result).toEqual({
entity,
device,
parentDevice: null,
area,
floor,
});
});
it("should inherit the parent device's area for an entity on a child device", () => {
const entity = mockEntity({
entity_id: "switch.outlet_1",
device_id: "child_1",
});
const childDevice = mockDevice({
id: "child_1",
parent_device_id: "parent_1",
});
const parentDevice = mockDevice({
id: "parent_1",
area_id: "area_1",
});
const area = mockArea({
area_id: "area_1",
floor_id: "floor_1",
});
const floor = mockFloor({
floor_id: "floor_1",
});
const stateObj = mockStateObj({
entity_id: "switch.outlet_1",
});
const hass = {
entities: {
"switch.outlet_1": entity,
},
devices: {
child_1: childDevice,
parent_1: parentDevice,
},
areas: {
area_1: area,
},
floors: {
floor_1: floor,
},
} as unknown as HomeAssistant;
const result = getEntityContext(
stateObj,
hass.entities,
hass.devices,
hass.areas,
hass.floors
);
expect(result).toEqual({
entity,
device: childDevice,
parentDevice,
area,
floor,
});
});
it("should return the correct context when the entity has an area but no device", () => {
const entity = mockEntity({
entity_id: "sensor.kitchen",
area_id: "area_2",
});
const area = mockArea({ area_id: "area_2", floor_id: "floor_2" });
const floor = mockFloor({ floor_id: "floor_2" });
const stateObj = mockStateObj({
entity_id: "sensor.kitchen",
});
const hass = {
entities: {
"sensor.kitchen": entity,
},
devices: {},
areas: {
area_2: area,
},
floors: {
floor_2: floor,
},
} as unknown as HomeAssistant;
const result = getEntityContext(
stateObj,
hass.entities,
hass.devices,
hass.areas,
hass.floors
);
expect(result).toEqual({
entity,
device: null,
parentDevice: null,
area,
floor,
});
});
it("should return null for floor if area does not have a floor_id", () => {
const entity = mockEntity({
entity_id: "sensor.bedroom",
area_id: "area_3",
});
const area = mockArea({
area_id: "area_3",
});
const stateObj = mockStateObj({
entity_id: "sensor.bedroom",
});
const hass = {
entities: {
"sensor.bedroom": entity,
},
devices: {},
areas: {
area_3: area,
},
floors: {},
} as unknown as HomeAssistant;
const result = getEntityContext(
stateObj,
hass.entities,
hass.devices,
hass.areas,
hass.floors
);
expect(result).toEqual({
entity,
device: null,
parentDevice: null,
area,
floor: null,
});
});
it("should not resolve a parent device for an entity on a main device", () => {
const entity = mockEntity({
entity_id: "switch.strip",
device_id: "parent_1",
});
const parentDevice = mockDevice({
id: "parent_1",
});
const stateObj = mockStateObj({
entity_id: "switch.strip",
});
const result = getEntityContext(
stateObj,
{ "switch.strip": entity },
{ parent_1: parentDevice },
{},
{}
);
expect(result).toEqual({
entity,
device: parentDevice,
parentDevice: null,
area: null,
floor: null,
});
});
});