52 lines
6.0 KiB
TypeScript
52 lines
6.0 KiB
TypeScript
import { html, nothing } from "lit";
|
||
import { WEATHER_VARIABLE_KEYS, type BuildingSettings, type PresenceLink, type Room, type RoomHvac } from "../../model.ts";
|
||
import { WEATHER_VARIABLE_LABELS } from "../../symcon-weather.ts";
|
||
import { ROOM_HVAC_FIELDS } from "../../entities/room-hvac.ts";
|
||
import { isSymconVariable } from "../../entities/symcon-values.ts";
|
||
import { entityName } from "../../devices.ts";
|
||
import { entityOptions } from "./entities.ts";
|
||
import { checkField, entityField, hint, textField } from "./fields.ts";
|
||
import type { EditorHost } from "./host.ts";
|
||
|
||
export function renderSymconRoomHvac(host: EditorHost, room: Room, patch: (change: Partial<Room>) => void) {
|
||
const set = (change: Partial<RoomHvac>) => patch({ hvac: { ...room.hvac, ...change } });
|
||
const schedules = room.hvac?.schedules ?? [];
|
||
const plans = entityOptions(host.hass, (id) => !!host.hass?.states[id]?.attributes.symcon_schedule);
|
||
return html`
|
||
${entityField(host, host.t("floor_heating"), room.floor_heating ?? null, undefined, entityOptions(host.hass, (id) => isSymconVariable(host.hass, id, [0, 1, 2])), (v) => patch({ floor_heating: v === "none" ? null : v }), { types: [0, 1, 2] })}
|
||
${ROOM_HVAC_FIELDS.map((field) => entityField(host, field.label, room.hvac?.[field.key] ?? null, undefined, entityOptions(host.hass, (id) => isSymconVariable(host.hass, id, field.types)), (v) => set({ [field.key]: v === "none" ? null : v }), { types: field.types }))}
|
||
${hint("Raumstatus zeigt den in Symcon hinterlegten Alias der Integer-Variable. Beschreibbare Heizungs- und Lüftungswerte können im Raummenü bedient werden.", "nf-wide")}
|
||
${schedules.map((id, i) => entityField(host, `Schaltuhr ${i + 1}`, id, undefined, plans.filter((p) => p.id === id || !schedules.includes(p.id)), (v) => set({ schedules: v && v !== "none" ? schedules.map((old, j) => i === j ? v : old) : schedules.filter((_, j) => i !== j) }), { schedule: true, excluded: (entity) => schedules.includes(entity) ? "Diese Schaltuhr ist in diesem Raum bereits verknüpft." : undefined }))}
|
||
${entityField(host, "Schaltuhr hinzufügen", null, undefined, plans.filter((p) => !schedules.includes(p.id)), (v) => v && v !== "none" && set({ schedules: [...schedules, v] }), { schedule: true, excluded: (entity) => schedules.includes(entity) ? "Diese Schaltuhr ist in diesem Raum bereits verknüpft." : undefined })}`;
|
||
}
|
||
|
||
export function renderSymconWeather(host: EditorHost, settings: BuildingSettings, set: (change: Partial<BuildingSettings>) => void) {
|
||
const types = (key: string) => key === "condition" ? [0, 1, 2, 3] : key === "wind_bearing" ? [1, 2, 3] : ["snow", "fog", "lightning"].includes(key) ? [0, 1, 2] : [1, 2];
|
||
return html`<details class="nf-wide nf-symcon-weather"><summary>Wettervariablen</summary><div class="nf-form">
|
||
${WEATHER_VARIABLE_KEYS.map((key) => entityField(host, WEATHER_VARIABLE_LABELS[key], settings.weather_variables?.[key] ?? null, undefined,
|
||
entityOptions(host.hass, (id) => isSymconVariable(host.hass, id, types(key))),
|
||
(v) => set({ weather_variables: { ...settings.weather_variables, [key]: v === "none" ? null : v } }), { types: types(key) }))}
|
||
${entityField(host, host.t("rain_sensor"), settings.rain_sensor ?? null, undefined, entityOptions(host.hass, (id) => isSymconVariable(host.hass, id, [0, 1, 2])), (v) => set({ rain_sensor: v === "none" ? null : v }), { types: [0, 1, 2] })}
|
||
${settings.rain_sensor ? checkField(host, host.t("rain_sensor_inverted"), !!settings.rain_sensor_inverted, (on) => set({ rain_sensor_inverted: on || undefined })) : nothing}
|
||
${hint("Wähle die Wetterwerte einzeln aus dem Symcon-Objektbaum. Einheiten werden aus den Variablen übernommen. Der Regensensor kann Boolean oder eine numerische Regenrate sein.", "nf-wide")}
|
||
</div></details>`;
|
||
}
|
||
|
||
export function renderSymconPresence(host: EditorHost) {
|
||
const links = host.doc.presence;
|
||
const replace = (next: PresenceLink[]) => host.replace({ ...structuredClone(host.doc), presence: next });
|
||
const patch = (i: number, change: Partial<PresenceLink>) => replace(links.map((link, j) => j === i ? { ...link, ...change, sensor: null } : link));
|
||
const rooms = host.doc.floors.flatMap((f) => f.rooms.map((r) => ({ id: r.id, name: `${f.name} · ${r.name}` })));
|
||
const options = entityOptions(host.hass, (id) => isSymconVariable(host.hass, id, [0]));
|
||
return html`<details class="nf-section nf-symcon-presence"><summary>${host.t("presence")}</summary>
|
||
${links.map((link, i) => html`<div class="nf-form">
|
||
${textField(host, "Person", link.name ?? entityName(host.hass, link.person), (name) => patch(i, { name }), { wide: true, maxlength: 100 })}
|
||
${entityField(host, "Anwesenheit (Boolean)", link.person, undefined, options.filter((o) => o.id === link.person || !links.some((l) => l.person === o.id)), (v) => v && v !== "none" ? patch(i, { person: v }) : replace(links.filter((_, j) => j !== i)), { types: [0], excluded: (entity) => links.some((l) => l.person === entity) ? "Diese Anwesenheitsvariable ist bereits mit einer Person verknüpft." : undefined })}
|
||
<label class="nf-field nf-wide">Raum für die 3D-Anzeige<select ?disabled=${!host.admin} .value=${link.room ?? ""} @change=${(e: Event) => patch(i, { room: (e.target as HTMLSelectElement).value || null })}><option value="">Kein Raum</option>${rooms.map((r) => html`<option value=${r.id}>${r.name}</option>`)}</select></label>
|
||
<button class="nf-link nf-wide" ?disabled=${!host.admin} @click=${() => replace(links.filter((_, j) => j !== i))}>× Person entfernen</button>
|
||
</div>`)}
|
||
<div class="nf-form">${entityField(host, "Person hinzufügen (Boolean)", null, undefined, options.filter((o) => !links.some((l) => l.person === o.id)), (v) => v && v !== "none" && replace([...links, { person: v, sensor: null }]), { types: [0], excluded: (entity) => links.some((l) => l.person === entity) ? "Diese Anwesenheitsvariable ist bereits mit einer Person verknüpft." : undefined })}</div>
|
||
${hint("Jede Person wird mit einer beliebigen Boolean-Variable verknüpft: true bedeutet anwesend, false abwesend. Ein Raum ist optional und bestimmt die Position der Person in 3D.")}
|
||
</details>`;
|
||
}
|