feat: show metered devices below rooms in utility overview

This commit is contained in:
jhartworks committed 2026-11-12 14:44:02 +01:00
1 parent 173d02e3cc
commit df4a6f36a3
14 files changed
+1791 -1577

No files matched your search

+10 -4
View File
@@ -2,6 +2,7 @@ import { UTILITY_KINDS, type Floor, type Room, type UtilityKind } from "./model.
import type { HassEntity, HomeAssistant, SymconObject } from "./types.ts";
import { isSymcon, nativeNumber } from "./entities/symcon-values.ts";
import { formatNumber } from "./i18n.ts";
import { roomDevicePowerLinks, type DevicePowerLink } from "./utility-device-power.ts";
export const UTILITY_LABELS: Record<UtilityKind, string> = { electricity: "Strom", water: "Wasser", gas: "Gas", heating: "Heizen", cooling: "Kühlen" };
export const UTILITY_ICONS: Record<UtilityKind, string> = { electricity: "⚡", water: "💧", gas: "🔥", heating: "♨", cooling: "❄" };
@@ -15,7 +16,7 @@ export interface UtilityReading {
export interface UtilityArea {
id: string; name: string; path: string; readings: UtilityReading[];
rooms: { id: string; name: string; readings: UtilityReading[] }[];
rooms: { id: string; name: string; readings: UtilityReading[]; devices: { 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();
@@ -144,12 +145,13 @@ export function roomUtilityReadings(hass: HomeAssistant, room: Room, rooms: read
}
/** 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[] {
export function floorUtilityAreas(hass: HomeAssistant, floor: Floor, rooms: readonly Room[] = floor.rooms, withDevices = false): UtilityArea[] {
if (!isSymcon(hass)) return [];
const devices = withDevices ? roomDevicePowerLinks(hass, floor) : new Map<string, DevicePowerLink[]>();
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");
if (!refs.length && (roomMeterSources(hass, room).length || devices.get(room.id)?.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);
@@ -163,7 +165,10 @@ export function floorUtilityAreas(hass: HomeAssistant, floor: Floor, rooms: read
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);
const children = group.members.map((r) => ({
id: r.id, name: r.name, readings: ownRoomUtilityReadings(hass, r),
devices: (devices.get(r.id) ?? []).map((device) => ({ id: device.id, name: device.name, readings: readings(hass, [{ kind: "electricity", metric: "power", id: device.source, period: "total" }], true) })),
})).filter((r) => r.readings.length || r.devices.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);
}
@@ -196,6 +201,7 @@ export function utilityWatched(hass: HomeAssistant, floors: readonly Floor[]): s
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)),
...[...roomDevicePowerLinks(hass, f).values()].flatMap((devices) => devices.map((device) => device.source)),
...UTILITY_KINDS.flatMap((kind) => [floorMeter(hass, f, kind, "power"), floorMeter(hass, f, kind, "energy")]).filter((id): id is string => !!id),
]))];
}