Files
NextFloor/frontend/src/utility-device-power.test.ts
T

83 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict";
import { test } from "node:test";
import { box, entity, floorWith, hassOf } from "./entities/fixtures.ts";
import type { Furniture } from "./model.ts";
import { roomDevicePowerLinks } from "./utility-device-power.ts";
import { buildingUtilityReadings, floorUtilityAreas, floorUtilityReadings, utilityText, utilityWatched } from "./utilities.ts";
const meter = (id: string, value: number, unit = "W") => entity(id, "open", { symcon_variable_type: 1, symcon_value: value, unit_of_measurement: unit });
const furniture = (id: string, power: string | null, extra: Partial<Furniture> = {}): Furniture => ({ id, type: "desk", name: id, x: 2, z: 2, w: 1, d: 1, h: 1, rotation: 0, variant: null, power, entity: "none", ...extra });
const native = (states: ReturnType<typeof entity>[]) => hassOf(states, {}, { language: "de", symconObjects: [{ object_id: 10, parent_id: 0, name: "Büro", path: "Haus / Büro", object_type: 0 }] });
test("area → room → metered device retains zero and live values without manufacturing room or floor totals", () => {
const room = box("room", 0, 0, 5, 5, "symcon-category-10"), floor = floorWith([room], { furniture: [furniture("Wallbox", "cover.1")] });
const h = native([meter("cover.1", 0)]);
const area = floorUtilityAreas(h, floor, floor.rooms, true)[0];
assert.equal(area.name, "Büro"); assert.deepEqual(area.readings, []);
assert.deepEqual(area.rooms[0].readings, []);
assert.equal(area.rooms[0].devices[0].name, "Wallbox");
assert.equal(utilityText(h, area.rooms[0].devices[0].readings[0]), "0 W");
assert.deepEqual(floorUtilityAreas(h, floor), []);
assert.deepEqual(floorUtilityReadings(h, floor), []);
assert.deepEqual(buildingUtilityReadings(h, [floor]), []);
assert.ok(utilityWatched(h, [floor]).includes("cover.1"));
h.states["cover.1"] = meter("cover.1", -1.5, "kW");
assert.equal(floorUtilityAreas(h, floor, floor.rooms, true)[0].rooms[0].devices[0].readings[0].value, -1500);
h.states["cover.1"].state = "unavailable";
assert.equal(utilityText(h, floorUtilityAreas(h, floor, floor.rooms, true)[0].rooms[0].devices[0].readings[0]), "Err: –");
});
test("device-only rooms and explicitly metered rooms share an area without changing its total or floor override", () => {
const a = box("A", 0, 0, 5, 5, "symcon-category-10"), b = box("B", 5, 0, 10, 5, "symcon-category-10");
b.utility_meters = { electricity: { power: "sensor.2" } };
a.sub_areas = ["symcon-category-20"];
const floor = floorWith([a, b], { furniture: [furniture("Gerät", "light.1")], utility_meters: { electricity: { power: "sensor.3" } } });
const h = native([meter("light.1", 250), meter("sensor.2", 30), meter("sensor.3", 900), meter("sensor.12", 320)]);
h.symconObjects = [...h.symconObjects!, { object_id: 11, parent_id: 10, object_type: 0, name: "Strom", path: "Büro / Strom", hidden: false, position: 0, icon: "" }, { object_id: 12, parent_id: 11, object_type: 2, name: "Leistung", path: "", entity_id: "sensor.12", hidden: false, position: 0, icon: "" }];
const [area] = floorUtilityAreas(h, floor, floor.rooms, true);
assert.equal(area.readings[0].value, 320);
assert.deepEqual(area.rooms.map((r) => [r.id, r.readings.map((v) => v.value), r.devices.map((d) => d.name)]), [["A", [], ["Gerät"]], ["B", [30], []]]);
assert.equal(floorUtilityReadings(h, floor)[0].value, 900);
assert.equal(buildingUtilityReadings(h, [floor])[0].value, 900);
assert.equal(floorUtilityAreas(h, floor, floor.rooms, true).filter((g) => g.rooms.some((r) => r.id === "A")).length, 1);
});
test("geometry assigns devices to their own room once; outside and unlinked items do not create room headers", () => {
const floor = floorWith([box("A", 0, 0, 5, 5), box("B", 5, 0, 10, 5)], { furniture: [furniture("A", "sensor.1"), furniture("B", "sensor.1", { x: 7 }), furniture("outside", "sensor.1", { x: 20 }), furniture("empty", "none")], placements: [{ entity_id: "sensor.1", x: 2, y: 0, z: 2, outside: true }] });
const h = native([meter("sensor.1", 8)]);
assert.deepEqual([...roomDevicePowerLinks(h, floor)].map(([id, ds]) => [id, ds.map((d) => d.name)]), [["A", ["A"]], ["B", ["B"]]]);
const [group] = floorUtilityAreas(h, floor, floor.rooms, true);
assert.equal(group.name, "Nicht zugeordnet"); assert.equal(group.rooms.length, 2);
floor.rooms.push({ ...floor.rooms[0], id: "overlap" });
assert.equal(roomDevicePowerLinks(h, floor).has("overlap"), false);
assert.deepEqual(roomDevicePowerLinks(hassOf(Object.values(h.states)), floor), new Map());
});
test("native numeric power placements and additional readings use metadata regardless of their name or inferred domain", () => {
const floor = floorWith([box("room", 0, 0, 5, 5)], { placements: [{ entity_id: "cover.1", name: "Beliebiger Name", x: 2, y: 0, z: 2 }, { entity_id: "switch.2", x: 1, y: 0, z: 2, values: [{ id: "v", entity: "light.3", icon: "sun" }] }], furniture: [furniture("Zusatzleistung", "none", { values: [{ id: "v", entity: "light.3", icon: "sun" }] }), furniture("Temperatur", "none", { values: [{ id: "v", entity: "sensor.4", icon: "temperature" }] })] });
const h = native([meter("cover.1", 0), entity("switch.2", "off"), meter("light.3", 4), meter("sensor.4", 23, "°C")]);
assert.deepEqual(roomDevicePowerLinks(h, floor).get("room")?.map((d) => [d.name, d.source]), [["Zusatzleistung", "light.3"], ["Beliebiger Name", "cover.1"], ["switch.2", "light.3"]]);
});
test("associated power meters follow placed devices; aliases watch and display their canonical native source", () => {
const h = native([entity("switch.1", "off"), entity("sensor.2", "0", { device_class: "power", unit_of_measurement: "W" }), meter("cover.3", 7), entity("light.alias", "on", { symcon_source_entity: "cover.3" })]);
h.entities = { "switch.1": { entity_id: "switch.1", device_id: "plug" }, "sensor.2": { entity_id: "sensor.2", device_id: "plug" } };
const floor = floorWith([box("r", 0, 0, 5, 5)], { placements: [{ entity_id: "switch.1", x: 2, y: 0, z: 2 }], furniture: [furniture("Alias", "light.alias"), furniture("Fehlt", "sensor.deleted")] });
assert.deepEqual(roomDevicePowerLinks(h, floor).get("r")?.map((d) => d.source), ["cover.3", "sensor.deleted", "sensor.2"]);
assert.deepEqual(utilityWatched(h, [floor]).sort(), ["cover.3", "sensor.2", "sensor.deleted"]);
const ds = floorUtilityAreas(h, floor, floor.rooms, true)[0].rooms[0].devices;
assert.equal(ds[0].readings[0].value, 7); assert.equal(ds[1].readings[0].incomplete, true);
});
test("hidden furniture stays out; a manifold's pump circuits retain separate device values", () => {
const floor = floorWith([box("r", 0, 0, 5, 5)], { furniture: [furniture("Hidden", "sensor.1", { show_when: { entity: "switch.hide" } }), furniture("Verteiler", null, { type: "heating_manifold", hydronic: { circuits: [{ id: "p", kind: "pump", name: "Vorschubpumpe", power: "sensor.1" }, { id: "v", kind: "heating", name: "Nur Ventil", has_pump: false, power: "sensor.1" }] } })] });
const h = native([meter("sensor.1", 0), entity("switch.hide", "off")]);
assert.deepEqual(roomDevicePowerLinks(h, floor).get("r")?.map((d) => [d.name, d.source]), [["Verteiler · Vorschubpumpe", "sensor.1"]]);
});
test("legacy light-pin migration preserves a native power value even when its guessed domain is light", () => {
const h = native([meter("light.1", 120)]);
const floor = floorWith([box("r", 0, 0, 5, 5)], { furniture: [furniture("Serverschrank", null, { type: "lamp_ceiling", entity: "light.1" }), furniture("Abgewählt", "none", { entity: "light.1" })] });
assert.deepEqual(roomDevicePowerLinks(h, floor).get("r")?.map((d) => [d.name, d.source]), [["Serverschrank", "light.1"]]);
});