Skip to content

Commit f04557a

Browse files
committed
fix(#147): fallback to device area when entity area_id is empty
1 parent e587d02 commit f04557a

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/hass.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ export interface EntityRegistryEntry {
2727
translation_key?: string;
2828
}
2929

30+
export interface DeviceRegistryEntry {
31+
id: string;
32+
config_entries: string[];
33+
connections: Array<[string, string]>;
34+
identifiers: Array<[string, string]>;
35+
manufacturer: string | null;
36+
model: string | null;
37+
name: string | null;
38+
sw_version: string | null;
39+
hw_version: string | null;
40+
serial_number: string | null;
41+
via_device_id: string | null;
42+
area_id: string | null;
43+
name_by_user: string | null;
44+
entry_type: 'service' | null;
45+
disabled_by: 'user' | 'integration' | 'config_entry' | null;
46+
configuration_url: string | null;
47+
}
48+
3049
export interface ExtEntityRegistryEntry extends EntityRegistryEntry {
3150
capabilities: Record<string, unknown>;
3251
original_icon?: string;
@@ -43,14 +62,31 @@ export const getExtendedEntityRegistryEntry = (
4362
entity_id: entityId,
4463
});
4564

65+
let devicesCache: DeviceRegistryEntry[] = [];
66+
export const fetchDeviceRegistry = async (hass: HomeAssistant): Promise<DeviceRegistryEntry[]> => {
67+
if (devicesCache.length) {
68+
return Promise.resolve(devicesCache);
69+
}
70+
return (devicesCache = await hass.callWS({
71+
type: 'config/device_registry/list',
72+
}));
73+
}
74+
4675
export async function getEntityArea(hass: HomeAssistant, entityId: string) {
4776
try {
4877
const extended = await getExtendedEntityRegistryEntry(hass, entityId);
49-
return extended.area_id;
78+
if (extended.area_id) {
79+
return extended.area_id;
80+
}
81+
const devices = await fetchDeviceRegistry(hass);
82+
const device = devices.find(d => d.id === extended.device_id);
83+
if (device && device.area_id) {
84+
return device.area_id;
85+
}
5086
} catch (e) {
5187
console.error(e);
52-
return null;
5388
}
89+
return null;
5490
}
5591

5692
export async function getEntitiesByArea(hass: HomeAssistantReal, entityIds: string[]) {

0 commit comments

Comments
 (0)