fix: show incomplete Symcon utility sums with Err prefix
This commit is contained in:
1 parent
a804fc8e15
commit
bb153490b0
14 files changed
+361
-293
No files matched your search
@@ -11,7 +11,7 @@ import { canAnnounce, roomHasSpeaker } from "../live/announce.ts";
|
||||
import { isSymcon, nativeText } from "../entities/symcon-values.ts";
|
||||
import "./room-hvac.ts";
|
||||
import "./utility-report.ts";
|
||||
import { roomUtilityReadings } from "../utilities.ts";
|
||||
import { roomUtilityReadings, utilityText } from "../utilities.ts";
|
||||
import type { Building, Floor, LightMood, LightSetting, Room } from "../model.ts";
|
||||
import { roomPower, roomPowerSensors } from "../heatmap.ts";
|
||||
import { energyCost, loadRoomEnergy, type RoomEnergy } from "../live/room-energy.ts";
|
||||
@@ -586,8 +586,10 @@ export class NfRoomPanel extends LitElement {
|
||||
const lux = roomClimateValue(this.hass, this.floor ?? null, room, "illuminance");
|
||||
if (lux !== null) out.push(`☀ ${formatNumber(this.hass, lux, 0)} lx`);
|
||||
// what the room's devices draw now
|
||||
const power = this.floor && this.building ? roomPower(this.hass, this.building, this.floor, room) : null;
|
||||
if (power !== null) out.push(`⚡ ${powerText(this.hass, power)}`);
|
||||
const meter = roomUtilityReadings(this.hass, room, this.building?.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W");
|
||||
const power = !meter && this.floor && this.building ? roomPower(this.hass, this.building, this.floor, room) : null;
|
||||
if (meter) out.push(`⚡ ${utilityText(this.hass, meter)}`);
|
||||
else if (power !== null) out.push(`⚡ ${powerText(this.hass, power)}`);
|
||||
// how long its windows and doors stand open (the one open longest)
|
||||
const openSince = this.floor
|
||||
? this.floor.openings
|
||||
|
||||
@@ -14,7 +14,7 @@ class NfUtilityReport extends LitElement {
|
||||
render() {
|
||||
return html`${UTILITY_KINDS.filter((kind) => this.readings.some((r) => r.kind === kind)).map((kind) => html`<section aria-label=${UTILITY_LABELS[kind]}>
|
||||
<h4>${UTILITY_ICONS[kind]} ${UTILITY_LABELS[kind]}</h4>
|
||||
${this.readings.filter((r) => r.kind === kind).map((r) => html`<div class="reading" title=${`${r.manual ? "Stockwerkszähler" : "Bereichszähler"}${r.incomplete ? " · Wert nicht verfügbar" : ""}\n${r.sources.map((id) => this.hass.states[id]?.attributes.symcon_path || this.hass.states[id]?.attributes.friendly_name || id).join("\n")}`}>
|
||||
${this.readings.filter((r) => r.kind === kind).map((r) => html`<div class="reading" title=${`${r.manual ? "Stockwerkszähler" : "Bereichszähler"}${r.incomplete ? " · Err: Mindestens ein Wert fehlt; angezeigt wird die Summe der verfügbaren Werte." : ""}\n${r.sources.map((id) => this.hass.states[id]?.attributes.symcon_path || this.hass.states[id]?.attributes.friendly_name || id).join("\n")}`}>
|
||||
<span>${utilityMetricLabel(r)}${r.manual ? html` <small>· Zähler</small>` : ""}</span><b>${utilityText(this.hass, r)}</b>
|
||||
</div>`)}
|
||||
</section>`)}`;
|
||||
|
||||
@@ -52,7 +52,7 @@ import { fixtureGlow } from "../entities/kinds.ts";
|
||||
import { coverTiltEntity } from "../entities/covers.ts";
|
||||
import { isSymcon, isSymconVariable, nativeBool, nativeText } from "../entities/symcon-values.ts";
|
||||
import { WEATHER_VARIABLE_LABELS } from "../symcon-weather.ts";
|
||||
import { floorUtilityReadings } from "../utilities.ts";
|
||||
import { floorUtilityReadings, utilityText } from "../utilities.ts";
|
||||
import "./utility-report.ts";
|
||||
import { deviceValuesText } from "../entities/device-values.ts";
|
||||
import { furnitureShown, hiddenFurniture, withoutHidden } from "../entities/visibility.ts";
|
||||
@@ -2600,10 +2600,11 @@ export class NfView3d extends LitElement {
|
||||
const hass = this.hass;
|
||||
const rows: { icon: string; label: string; value: string }[] = [];
|
||||
const floor = this.floorId ? b.floors.find((f) => f.id === this.floorId) : undefined;
|
||||
const floorPower = floor && isSymcon(hass) ? floorUtilityReadings(hass, floor, b.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W")?.value ?? null : floor?.power_entity ? readPower(hass?.states[floor.power_entity]) : null;
|
||||
if (floor && floorPower !== null) {
|
||||
const share = consumption && consumption > 0 ? ` · ${translate(hass, "floor_power_share", { n: Math.round((Math.max(0, floorPower) / consumption) * 100) })}` : "";
|
||||
rows.push({ icon: "⚡", label: floor.name, value: `${powerText(hass, floorPower)}${share}` });
|
||||
const meter = floor && isSymcon(hass) ? floorUtilityReadings(hass, floor, b.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W") : undefined;
|
||||
const floorPower = isSymcon(hass) ? meter?.value ?? null : floor?.power_entity ? readPower(hass?.states[floor.power_entity]) : null;
|
||||
if (floor && (meter || floorPower !== null)) {
|
||||
const share = !meter?.incomplete && floorPower !== null && consumption && consumption > 0 ? ` · ${translate(hass, "floor_power_share", { n: Math.round((Math.max(0, floorPower) / consumption) * 100) })}` : "";
|
||||
rows.push({ icon: "⚡", label: floor.name, value: `${meter ? utilityText(hass, meter) : powerText(hass, floorPower!)}${share}` });
|
||||
}
|
||||
for (const [key, icon, label] of [["gas", "🔥", "energy_gas_short"], ["water", "💧", "energy_water_short"]] as const) {
|
||||
const id = b.energy[key];
|
||||
|
||||
+20
-13
@@ -3,7 +3,7 @@
|
||||
|
||||
import { fromCelsius, kindOf, openingState, primaryEntities, roomClimateValue, tempUnit, type OpeningEntities, roomEntities } from "./devices.ts";
|
||||
import { readPower } from "./energy.ts";
|
||||
import { floorUtilityReadings } from "./utilities.ts";
|
||||
import { floorUtilityReadings, utilityText } from "./utilities.ts";
|
||||
import { formatNumber, translate } from "./i18n.ts";
|
||||
import { centroid, type Building, type Floor, type Room } from "./model.ts";
|
||||
import type { HomeAssistant } from "./types.ts";
|
||||
@@ -87,6 +87,8 @@ export function personsInRooms(hass: HomeAssistant, building: Building): PersonM
|
||||
}
|
||||
|
||||
export interface FloorCounts {
|
||||
/** Native meter text includes Err: when only part of the sum is available. */
|
||||
powerLabel?: string;
|
||||
rooms: number;
|
||||
lightsOn: number;
|
||||
open: number;
|
||||
@@ -129,17 +131,21 @@ function openingsOnFloor(hass: HomeAssistant, floor: Floor, links: Map<string, O
|
||||
/** Room, light, open-window and person counts per floor, for the floor labels. */
|
||||
export function floorCounts(hass: HomeAssistant, building: Building, links: Map<string, OpeningEntities>, persons: PersonMarker[]): Map<string, FloorCounts> {
|
||||
return new Map(
|
||||
building.floors.map((floor) => [
|
||||
floor.id,
|
||||
{
|
||||
rooms: floor.rooms.length,
|
||||
lightsOn: lightsOnFloor(hass, floor),
|
||||
open: openingsOnFloor(hass, floor, links),
|
||||
persons: persons.filter((p) => p.floorId === floor.id).length,
|
||||
power: isSymcon(hass) ? floorUtilityReadings(hass, floor, building.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W")?.value ?? null : floor.power_entity ? readPower(hass.states[floor.power_entity]) : null,
|
||||
temps: tempRange(hass, floor),
|
||||
},
|
||||
]),
|
||||
building.floors.map((floor) => {
|
||||
const meter = isSymcon(hass) ? floorUtilityReadings(hass, floor, building.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W") : undefined;
|
||||
return [
|
||||
floor.id,
|
||||
{
|
||||
rooms: floor.rooms.length,
|
||||
lightsOn: lightsOnFloor(hass, floor),
|
||||
open: openingsOnFloor(hass, floor, links),
|
||||
persons: persons.filter((p) => p.floorId === floor.id).length,
|
||||
power: isSymcon(hass) ? meter?.value ?? null : floor.power_entity ? readPower(hass.states[floor.power_entity]) : null,
|
||||
...(meter ? { powerLabel: utilityText(hass, meter) } : {}),
|
||||
temps: tempRange(hass, floor),
|
||||
},
|
||||
];
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -159,7 +165,8 @@ export function floorInfoText(hass: HomeAssistant | undefined, c: FloorCounts):
|
||||
const [lo, hi] = c.temps.map((t) => formatNumber(hass, fromCelsius(hass, t), 0));
|
||||
parts.push(lo === hi ? `${lo} ${tempUnit(hass)}` : `${lo}–${hi} ${tempUnit(hass)}`);
|
||||
}
|
||||
if (c.power != null) parts.push(`⚡ ${powerText(hass, c.power)}`);
|
||||
const power = c.powerLabel ?? (c.power != null ? powerText(hass, c.power) : null);
|
||||
if (power) parts.push(`⚡ ${power}`);
|
||||
return parts.join(" · ");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { HomeAssistant, SymconObject } from "./types.ts";
|
||||
import { emptyBuilding, UTILITY_KINDS } from "./model.ts";
|
||||
import { floorMeter, floorUtilityReadings, roomUtilityReadings, utilityText, utilityWatched } from "./utilities.ts";
|
||||
import { roomPower, roomPowerSensors } from "./heatmap.ts";
|
||||
import { floorCounts } from "./presence.ts";
|
||||
import { floorCounts, floorInfoText } from "./presence.ts";
|
||||
import { watchedEntities } from "./components/view3d/watch-list.ts";
|
||||
|
||||
const DUMMY = "{485D0419-BE97-4548-AA9C-C083EB82E61E}";
|
||||
@@ -120,16 +120,38 @@ test("legacy floor power remains selected and an explicit automatic choice overr
|
||||
assert.deepEqual(floorUtilityReadings(h, floor), []);
|
||||
});
|
||||
|
||||
test("an unavailable meter makes its sum unavailable instead of reporting a partial total; live values update", () => {
|
||||
test("an unavailable meter keeps the available partial sum with Err: and clears the marker on recovery", () => {
|
||||
const f = fixture(); f.node(10, 0, "A"); f.node(11, 10, "Strom"); f.variable(12, 11, "Leistung", 1, "W");
|
||||
f.node(20, 0, "B"); f.node(21, 20, "Strom"); const id = f.variable(22, 21, "Leistung", 2, "W");
|
||||
const floor = floorWith([f.room(), f.room(20)]), h = f.hass();
|
||||
assert.equal(floorUtilityReadings(h, floor)[0].value, 3);
|
||||
const next = { ...h, states: { ...h.states, [id]: { ...h.states[id], state: "unavailable" } } } as HomeAssistant;
|
||||
assert.equal(floorUtilityReadings(next, floor)[0].value, null);
|
||||
assert.equal(floorUtilityReadings(next, floor)[0].incomplete, true);
|
||||
const partial = floorUtilityReadings(next, floor)[0];
|
||||
assert.equal(partial.value, 1);
|
||||
assert.equal(partial.incomplete, true);
|
||||
assert.equal(utilityText(next, partial), "Err: 1 W");
|
||||
const counts = floorCounts(next, { ...emptyBuilding(), floors: [floor] }, new Map(), []).get(floor.id)!;
|
||||
assert.equal(counts.power, 1);
|
||||
assert.match(floorInfoText(next, counts), /⚡ Err: 1 W$/);
|
||||
const live = { ...h, states: { ...h.states, [id]: { ...h.states[id], attributes: { ...h.states[id].attributes, symcon_value: 5 } } } };
|
||||
assert.equal(floorUtilityReadings(live, floor)[0].value, 6);
|
||||
assert.equal(utilityText(live, floorUtilityReadings(live, floor)[0]), "6 W");
|
||||
});
|
||||
|
||||
test("partial zero sums stay zero; fully unavailable room and manual meter readings show Err: dash", () => {
|
||||
const f = fixture(); f.node(10, 0, "A"); f.node(11, 10, "Wasser");
|
||||
f.node(20, 11, "Zähler A", 1); const good = f.variable(21, 20, "Verbrauch", 0, "m³");
|
||||
f.node(30, 11, "Zähler B", 1); const bad = f.variable(31, 30, "Verbrauch", 100, "m³");
|
||||
const h = f.hass(); h.states[bad] = { ...h.states[bad], state: "unavailable" };
|
||||
let rows = roomUtilityReadings(h, f.room());
|
||||
assert.equal(rows[0].value, 0);
|
||||
assert.equal(utilityText(h, rows[0]), "Err: 0 m³");
|
||||
const unavailable = { ...h, states: { ...h.states, [good]: { ...h.states[good], state: "unknown" } } };
|
||||
rows = roomUtilityReadings(unavailable, f.room());
|
||||
assert.equal(rows[0].value, null);
|
||||
assert.equal(utilityText(unavailable, rows[0]), "Err: –");
|
||||
const floor = floorWith([f.room()], { utility_meters: { water: { energy: bad } } });
|
||||
assert.equal(utilityText(h, floorUtilityReadings(h, floor)[0]), "Err: –");
|
||||
});
|
||||
|
||||
test("the view watches automatic and all five manual floor meters even without heatmap or climate cards", () => {
|
||||
|
||||
@@ -109,14 +109,14 @@ function readings(hass: HomeAssistant, sources: Source[], manual = false): Utili
|
||||
const st = hass.states[id], unit = measure(st);
|
||||
const canonicalUnit = unit?.metric === s.metric ? unit.unit : String(st?.attributes.unit_of_measurement ?? "").trim();
|
||||
const key = `${s.kind}|${s.metric}|${canonicalUnit}|${s.period}`;
|
||||
const row = groups.get(key) ?? { kind: s.kind, metric: s.metric, unit: canonicalUnit, value: 0, period: s.period, sources: [], manual, incomplete: false };
|
||||
const row = groups.get(key) ?? { kind: s.kind, metric: s.metric, unit: canonicalUnit, value: null, period: s.period, sources: [], manual, incomplete: false };
|
||||
const value = nativeNumber(st);
|
||||
if (value === null) row.incomplete = true;
|
||||
else row.value! += value * (unit?.metric === s.metric ? unit.factor : 1);
|
||||
else row.value = (row.value ?? 0) + value * (unit?.metric === s.metric ? unit.factor : 1);
|
||||
row.sources.push(id);
|
||||
groups.set(key, row);
|
||||
}
|
||||
return [...groups.values()].map((row) => row.incomplete ? { ...row, value: null } : row);
|
||||
return [...groups.values()];
|
||||
}
|
||||
|
||||
export function roomUtilityReadings(hass: HomeAssistant, room: Room, rooms: readonly Room[] = []): UtilityReading[] {
|
||||
@@ -152,9 +152,10 @@ export function utilityMetricLabel(row: UtilityReading): string {
|
||||
}
|
||||
|
||||
export function utilityText(hass: HomeAssistant | undefined, row: UtilityReading): string {
|
||||
if (row.value === null) return "–";
|
||||
const prefix = row.incomplete ? "Err: " : "";
|
||||
if (row.value === null) return `${prefix}–`;
|
||||
const value = row.unit === "W" && Math.abs(row.value) >= 1000 ? row.value / 1000 : row.value;
|
||||
const unit = row.unit === "W" && Math.abs(row.value) >= 1000 ? "kW" : row.unit;
|
||||
const digits = Number.isInteger(value) ? 0 : Math.min(6, Math.max(2, 1 - Math.floor(Math.log10(Math.abs(value)))));
|
||||
return `${formatNumber(hass, value, digits)}${unit ? ` ${unit}` : ""}`;
|
||||
return `${prefix}${formatNumber(hass, value, digits)}${unit ? ` ${unit}` : ""}`;
|
||||
}
|
||||
@@ -91,14 +91,39 @@ try {
|
||||
await p.updateComplete;
|
||||
});
|
||||
await page.waitForFunction(() => window.utilityView.shadowRoot.querySelector(".nf-utility-dialog nf-utility-report").shadowRoot.textContent.includes("18 kWh"));
|
||||
await page.evaluate(async () => {
|
||||
const p = window.nfPanel, states = { ...p.hass.states };
|
||||
for (const id of ["sensor.9201", "sensor.9202"]) states[id] = { ...states[id], state: "unavailable" };
|
||||
p.hass = { ...p.hass, states }; await p.updateComplete;
|
||||
});
|
||||
await page.waitForFunction(() => {
|
||||
const text = window.utilityView.shadowRoot.querySelector(".nf-utility-dialog nf-utility-report").shadowRoot.textContent;
|
||||
return text.includes("Err: 10 kWh") && text.includes("Err: 1 kW");
|
||||
});
|
||||
await page.evaluate(() => window.utilityView.syncDevices(true));
|
||||
await page.waitForFunction(() => [...window.utilityView.shadowRoot.querySelectorAll(".nf-pin-floor")].some((el) => el.textContent.includes("Err: 1 kW")));
|
||||
await page.evaluate(async () => {
|
||||
const p = window.nfPanel, rp = document.createElement("nf-room-panel"), floor = p.data.building.floors[0];
|
||||
Object.assign(rp, { hass: p.hass, building: p.data.building, floor, room: floor.rooms[1] });
|
||||
document.body.append(rp); await rp.updateComplete;
|
||||
const report = rp.shadowRoot.querySelector("nf-utility-report"); await report.updateComplete;
|
||||
if (!report.shadowRoot.textContent.includes("Err: –") || !rp.shadowRoot.querySelector(".nf-rp-facts").textContent.includes("Err: –")) throw new Error("Fully unavailable room meter must retain Err: in the report and header");
|
||||
rp.remove();
|
||||
});
|
||||
const out = join(root, "preview/screenshots"); mkdirSync(out, { recursive: true });
|
||||
await page.screenshot({ path: join(out, "symcon-utility-floor-desktop.png") });
|
||||
await page.setViewport({ width: 390, height: 844 });
|
||||
const bounds = await page.evaluate(() => { const r = window.utilityView.shadowRoot.querySelector(".nf-utility-dialog").getBoundingClientRect(); return { left: r.left, right: r.right, width: innerWidth }; });
|
||||
assert.ok(bounds.left >= 0 && bounds.right <= bounds.width, "meter overview fits mobile viewport");
|
||||
await page.screenshot({ path: join(out, "symcon-utility-floor-mobile.png") });
|
||||
await page.evaluate(async () => {
|
||||
const p = window.nfPanel, states = { ...p.hass.states };
|
||||
for (const [id, value] of [["sensor.9201", 500], ["sensor.9202", 8]]) states[id] = { ...states[id], state: String(value) };
|
||||
p.hass = { ...p.hass, states }; await p.updateComplete;
|
||||
});
|
||||
await page.waitForFunction(() => { const text = window.utilityView.shadowRoot.querySelector(".nf-utility-dialog nf-utility-report").shadowRoot.textContent; return text.includes("18 kWh") && !text.includes("Err:"); });
|
||||
assert.deepEqual(errors, []);
|
||||
console.log("OK: hidden category/dummy discovery, room and floor totals, ten native meter selectors, numeric cover-domain override, clear-to-automatic, live consumption update and desktop/mobile floor dialog");
|
||||
console.log("OK: hidden category/dummy discovery, room/floor totals and headers, native meter selectors, clear-to-automatic, live updates, Err: partial sums, missing readings, recovery and desktop/mobile dialog");
|
||||
} finally {
|
||||
await browser?.close(); await new Promise((done) => server.close(done));
|
||||
}
|
||||
Reference in new issue
Block a user