-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathentity-location.ts
More file actions
40 lines (34 loc) · 1.19 KB
/
entity-location.ts
File metadata and controls
40 lines (34 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Entity name and room resolution utilities
*/
import type { HassEntity } from 'home-assistant-js-websocket';
import { UNKNOWN_ROOM_LABEL } from '../../utils/device-location';
export function getName(entity: HassEntity, registryEntry?: { name?: string | null }): string {
if (typeof registryEntry?.name === 'string' && registryEntry.name.trim().length > 0) {
return registryEntry.name.trim();
}
return entity.attributes?.friendly_name || entity.entity_id;
}
export function resolveEntityRoom(
entityId: string,
entity: HassEntity,
areaMap: Map<string, string>,
entityRegistryMap: Map<string, { area_id?: string | null; device_id?: string | null }>,
deviceRegistryMap: Map<string, { area_id?: string | null }>
): string {
const entityEntry = entityRegistryMap.get(entityId);
const deviceEntry = entityEntry?.device_id
? deviceRegistryMap.get(entityEntry.device_id)
: undefined;
const areaId = entityEntry?.area_id ?? deviceEntry?.area_id;
if (areaId) {
const areaName = areaMap.get(areaId);
if (areaName) return areaName;
}
return (
entity.attributes?.room ||
entity.attributes?.area ||
entity.attributes?.zone ||
UNKNOWN_ROOM_LABEL
);
}