feat: improve Symcon designer controls and KNX temperature selection

This commit is contained in:
jhartworks committed 2026-11-09 15:01:54 +01:00
1 parent 804f2d0494
commit b934f78b9a
24 files changed
+908 -562

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import assert from "node:assert/strict";
import { test, type TestContext } from "node:test";
import { SymconBridge } from "./symcon-bridge.ts";
function mockWindow(context: TestContext): void {
const previous = Object.getOwnPropertyDescriptor(globalThis, "window");
Object.defineProperty(globalThis, "window", { configurable: true, value: { __NEXTFLOOR_SYMCON__: { endpoint: "/hook/test", token: "fixture" } } });
context.after(() => {
if (previous) Object.defineProperty(globalThis, "window", previous);
else Reflect.deleteProperty(globalThis, "window");
});
}
test("manual reload waits for polling, replaces removed variables and reloads the whole tree", async (context) => {
mockWindow(context);
const variable = (id: number) => ({ entity_id: `sensor.${id}`, name: "KNX Wert", path: "Haus / KNX / Wert", state: "21.75", attributes: { unit_of_measurement: "°C" } });
const actions: string[] = [];
let releasePoll!: () => void;
const pollGate = new Promise<void>((resolve) => { releasePoll = resolve; });
context.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => {
const { action } = JSON.parse(String(init.body));
actions.push(action);
if (action === "variables/snapshot") {
await pollGate;
return new Response(JSON.stringify({ state_revision: 1, building_revision: 1 }));
}
const reload = actions.filter((a) => a === "bootstrap").length > 1;
return new Response(JSON.stringify({ state_revision: 1, building_revision: 1, variables: [variable(reload ? 2 : 1)], areas: [], objects: [{ object_id: reload ? 2 : 1 }] }));
});
const bridge = new SymconBridge();
try {
const hass = await bridge.start();
bridge.stop();
let changes = 0;
bridge.onChange(() => { changes++; });
const poll = (bridge as unknown as { refresh(): Promise<void> }).refresh();
const reload = hass.symconRefresh!();
assert.deepEqual(actions, ["bootstrap", "variables/snapshot"], "manual bootstrap must not overlap the running poll");
releasePoll();
await Promise.all([poll, reload]);
assert.deepEqual(actions, ["bootstrap", "variables/snapshot", "bootstrap"]);
assert.deepEqual(Object.keys(bridge.snapshot().states), ["sensor.2"], "even an unchanged revision cannot suppress an explicit reload");
assert.equal(bridge.snapshot().symconObjects?.[0].object_id, 2);
assert.equal(changes, 1);
} finally {
releasePoll();
bridge.stop();
}
});
test("a failed manual reload reports the error and preserves existing values for a retry", async (context) => {
mockWindow(context);
let fail = false;
context.mock.method(globalThis, "fetch", async () => fail ? new Response("offline", { status: 503 })
: new Response(JSON.stringify({ state_revision: 1, building_revision: 1, variables: [{ entity_id: "sensor.1", name: "Temperatur", path: "Haus / Temperatur", state: "21.75", attributes: {} }], objects: [], areas: [] })));
const bridge = new SymconBridge();
try {
const hass = await bridge.start();
bridge.stop();
fail = true;
await assert.rejects(hass.symconRefresh!(), /503/);
assert.equal(bridge.snapshot().states["sensor.1"].state, "21.75");
fail = false;
await hass.symconRefresh!();
assert.equal(bridge.snapshot().states["sensor.1"].state, "21.75");
} finally {
bridge.stop();
}
});