fix: group utility overview by area and show explicit room meters

This commit is contained in:
jhartworks committed 2026-11-12 12:09:21 +01:00
1 parent 8c6b23bd22
commit 93c015ec77
17 files changed
+1475 -1253

No files matched your search

+48 -2
View File
@@ -13,6 +13,11 @@ export interface UtilityReading {
period: Period; sources: string[]; manual: boolean; incomplete: boolean;
}
export interface UtilityArea {
id: string; name: string; path: string; readings: UtilityReading[];
rooms: { id: string; name: string; readings: UtilityReading[] }[];
}
const normalize = (s: string) => s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/ß/g, "ss").replace(/[^a-z0-9]+/g, " ").trim();
const DUMMY = "{485D0419-BE97-4548-AA9C-C083EB82E61E}";
const kindOf = (node: SymconObject): UtilityKind | null => {
@@ -119,8 +124,48 @@ function readings(hass: HomeAssistant, sources: Source[], manual = false): Utili
return [...groups.values()];
}
function roomMeterSources(hass: HomeAssistant, room: Room): Source[] {
return UTILITY_KINDS.flatMap((kind) => (["power", "energy"] as const).flatMap((metric) => {
const ref = room.utility_meters?.[kind]?.[metric];
if (!ref || ref === "none") return [];
const id = canonical(hass, ref);
return [{ kind, metric, id, period: metric === "energy" ? periodOf(normalize(String(hass.states[id]?.attributes.friendly_name ?? ""))) : "total" }];
}));
}
/** Room details contain only the explicitly assigned room readings. */
export function ownRoomUtilityReadings(hass: HomeAssistant, room: Room): UtilityReading[] {
return isSymcon(hass) ? readings(hass, roomMeterSources(hass, room), true) : [];
}
export function roomUtilityReadings(hass: HomeAssistant, room: Room, rooms: readonly Room[] = []): UtilityReading[] {
return readings(hass, roomSources(hass, room, rooms));
const own = ownRoomUtilityReadings(hass, room);
return own.length ? own : readings(hass, roomSources(hass, room, rooms));
}
/** Group by actual native category IDs, never by plan-room names or repeated meter values. */
export function floorUtilityAreas(hass: HomeAssistant, floor: Floor, rooms: readonly Room[] = floor.rooms): UtilityArea[] {
if (!isSymcon(hass)) return [];
const groups = new Map<string, { seed: Room; members: Room[] }>();
for (const room of floor.rooms) {
const refs = [...new Set([room.area_id, ...(room.sub_areas ?? [])].filter((id): id is string => !!areaId(id)))];
if (!refs.length && roomMeterSources(hass, room).length) refs.push("unassigned");
for (const id of refs) {
const group = groups.get(id) ?? { seed: room, members: [] };
if (id === refs[0]) group.members.push(room);
groups.set(id, group);
}
}
const nodes = treeOf(hass.symconObjects!).nodes;
return [...groups].map(([id, group]) => {
const node = nodes.get(Number(areaId(id)));
const auto = id === "unassigned" ? [] : readings(hass, roomSources(hass, { ...group.seed, area_id: id, sub_areas: null }, rooms));
const own = readings(hass, group.members.flatMap((r) => roomMeterSources(hass, r)), true);
// A room is a breakdown of an existing area meter, not an additional consumer.
const covered = (r: UtilityReading) => auto.some((a) => a.kind === r.kind && a.metric === r.metric && a.unit === r.unit && a.period === r.period);
const children = group.members.map((r) => ({ id: r.id, name: r.name, readings: ownRoomUtilityReadings(hass, r) })).filter((r) => r.readings.length);
return { id, name: id === "unassigned" ? "Nicht zugeordnet" : node?.name || hass.areas?.[id]?.name || "Bereich nicht verfügbar", path: node?.path || "", readings: [...auto, ...own.filter((r) => !covered(r))], rooms: children };
}).filter((g) => g.readings.length || g.rooms.length);
}
export function floorMeter(hass: HomeAssistant, floor: Floor, kind: UtilityKind, metric: UtilityMetric): string | null {
@@ -130,7 +175,7 @@ export function floorMeter(hass: HomeAssistant, floor: Floor, kind: UtilityKind,
}
export function floorUtilityReadings(hass: HomeAssistant, floor: Floor, rooms: readonly Room[] = floor.rooms): UtilityReading[] {
const all = floor.rooms.flatMap((r) => roomSources(hass, r, rooms));
const all = floorUtilityAreas(hass, floor, rooms).flatMap((a) => a.readings.flatMap((r) => r.sources.map((id) => ({ kind: r.kind, metric: r.metric, period: r.period, id }))));
const result: UtilityReading[] = [];
for (const kind of UTILITY_KINDS) for (const metric of ["power", "energy"] as const) {
const manual = floorMeter(hass, floor, kind, metric);
@@ -150,6 +195,7 @@ export function utilityWatched(hass: HomeAssistant, floors: readonly Floor[]): s
const rooms = floors.flatMap((f) => f.rooms);
return [...new Set(floors.flatMap((f) => [
...f.rooms.flatMap((r) => roomSources(hass, r, rooms).map((s) => s.id)),
...f.rooms.flatMap((r) => roomMeterSources(hass, r).map((s) => s.id)),
...UTILITY_KINDS.flatMap((kind) => [floorMeter(hass, f, kind, "power"), floorMeter(hass, f, kind, "energy")]).filter((id): id is string => !!id),
]))];
}