feat: add native Symcon archive trends and explicit room naming

This commit is contained in:
jhartworks committed 2026-11-11 12:00:51 +01:00
1 parent bb153490b0
commit cfc77787a0
28 files changed
+2449 -1250

No files matched your search

+2 -1
View File
@@ -7,7 +7,7 @@ import { PACKAGES, type PackageId } from "../../../packages.ts";
import { entityOptions } from "../entities.ts";
import { checkField, colorField, entityField, hint, numberField, selectField, textField } from "../fields.ts";
import type { EditorHost } from "../host.ts";
import type { RectField } from "../ops/rooms.ts";
import { areaLeafName, type RectField } from "../ops/rooms.ts";
import { SPOT_TYPES, spotCount, spotItems } from "../ops/spots.ts";
import type { SideState } from "../side-state.ts";
import { formatArea, formatLength } from "../../../units.ts";
@@ -161,6 +161,7 @@ export function renderRoomForm(host: EditorHost, state: SideState, room: Room, a
<div class="nf-form">
${textField(host, t("room_name"), room.name, (v) => actions.patch({ name: v }), { wide: true })}
${selectField(host, t("area"), room.area_id ?? "", areaChoices, (id) => actions.setArea(id), { wide: true })}
${isSymcon(host.hass) && room.area_id ? html`<button type="button" class="nf-btn nf-wide nf-take-area-name" ?disabled=${!host.admin || !areaLeafName(host.hass, room.area_id)} @click=${() => actions.patch({ name: areaLeafName(host.hass, room.area_id!) })}>Bereichsnamen übernehmen</button>` : nothing}
${(room.sub_areas ?? []).map((a) => html`<span class="nf-chip nf-wide">${host.hass?.areas?.[a]?.name ?? a} <button class="nf-btn nf-field-btn" ?disabled=${!host.admin} title=${t("delete")} @click=${() => actions.patch({ sub_areas: (room.sub_areas ?? []).filter((x) => x !== a).length ? (room.sub_areas ?? []).filter((x) => x !== a) : null })}>✕</button></span>`)}
${room.area_id
? selectField(host, t("sub_area_add"), "", areaChoices.filter(([id]) => id !== room.area_id && !(room.sub_areas ?? []).includes(id)), (id) => id && actions.patch({ sub_areas: [...(room.sub_areas ?? []), id] }), { wide: true, title: t("sub_area_hint") })
@@ -1,9 +1,9 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { box, floorWith, opening } from "../../../entities/fixtures.ts";
import { box, floorWith, opening, hassOf } from "../../../entities/fixtures.ts";
import type { Placement } from "../../../model.ts";
import { setWallSlope } from "./edges.ts";
import { insertCorner, areaChoice, copyOfRoom, cornerMoved, evenCeilingLights, newRoom, rectangleAfter, removeCorner, removeRoom } from "./rooms.ts";
import { insertCorner, areaChoice, areaLeafName, copyOfRoom, cornerMoved, evenCeilingLights, newRoom, rectangleAfter, removeCorner, removeRoom } from "./rooms.ts";
const lamp = (id: string, x: number, z: number, extra: Partial<Placement> = {}): Placement => ({ entity_id: id, x, z, y: null, ...extra });
@@ -73,6 +73,14 @@ test("choosing an area names a room that only had a placeholder name", () => {
assert.deepEqual(areaChoice({ ...box("r", 0, 0, 1, 1), name: "Lounge" }, "", areas), { area_id: null });
});
test("Symcon area selection preserves the name and explicit adoption uses the real leaf category", () => {
const h = hassOf([], {}, { areas: { "symcon-category-10": { area_id: "symcon-category-10", name: "Haus / OG / Büro" } }, symconObjects: [{ object_id: 10, parent_id: 2, name: "Büro / Nord", path: "Haus / OG / Büro / Nord", object_type: 0, position: 0, hidden: true, icon: "" }] });
assert.deepEqual(areaChoice({ ...box("r", 0, 0, 1, 1), name: "Room 3" }, "symcon-category-10", h.areas!, false), { area_id: "symcon-category-10" });
assert.equal(areaLeafName(h, "symcon-category-10"), "Büro / Nord");
assert.equal(areaLeafName({ ...h, symconObjects: [] }, "symcon-category-10"), "Büro");
assert.equal(areaLeafName(h, "missing"), "");
});
test("ceiling lights spread over the room in a grid, a short last row over the full width", () => {
const room = box("r", 0, 0, 6, 4);
const lights = [lamp("light.a", 1, 1), lamp("light.b", 1.1, 1), lamp("light.c", 1.2, 1), lamp("light.d", 1.3, 1), lamp("light.e", 1.4, 1)];
+10 -2
View File
@@ -3,6 +3,7 @@
import { bounds, pointInPolygon, uid, type Floor, type Opening, type Placement, type Room, type Vec2 } from "../../../model.ts";
import { arcPoints, pieceAt, polylineLength } from "../../../geometry/arcs.ts";
import { kindOf } from "../../../devices.ts";
import type { HomeAssistant } from "../../../types.ts";
const thousandths = (v: number) => Math.round(v * 1000) / 1000;
const rounded = (points: readonly Vec2[]): Vec2[] => points.map(([x, z]) => [thousandths(x), thousandths(z)]);
@@ -73,10 +74,17 @@ const GENERIC_NAME = /^(Raum|Room) \d+$/;
* What choosing a Home Assistant area does to a room: it gets the area, and a name that was only a placeholder ("Room 3", or the
* name of some area) takes the area's name.
*/
export function areaChoice(room: Room, areaId: string, areas: Readonly<Record<string, { name: string }>>): Partial<Room> {
export function areaChoice(room: Room, areaId: string, areas: Readonly<Record<string, { name: string }>>, takeName = true): Partial<Room> {
const area = areaId ? areas[areaId] : undefined;
const placeholder = !room.name || GENERIC_NAME.test(room.name) || Object.values(areas).some((a) => a.name === room.name);
return { area_id: areaId || null, ...(area && placeholder ? { name: area.name } : {}) };
return { area_id: areaId || null, ...(takeName && area && placeholder ? { name: area.name } : {}) };
}
/** Use the actual leaf category, even when its native name contains a slash. */
export function areaLeafName(hass: HomeAssistant | undefined, areaId: string): string {
const id = /^symcon-category-(\d+)$/.exec(areaId)?.[1];
return (id ? hass?.symconObjects?.find((n) => n.object_id === Number(id))?.name : undefined)
?? hass?.areas?.[areaId]?.name.split(" / ").at(-1)?.trim() ?? "";
}
/**