Files
NextFloor/frontend/src/components/room-hvac.ts
T

92 lines
6.8 KiB
TypeScript

import { css, html, LitElement, nothing } from "lit";
import type { Room } from "../model.ts";
import type { HomeAssistant } from "../types.ts";
import { ROOM_HVAC_FIELDS } from "../entities/room-hvac.ts";
import { nativeBool, nativeNumber, nativeText, nativeValue } from "../entities/symcon-values.ts";
import { controls, tokens } from "../styles.ts";
import { trendButton } from "./trend-button.ts";
interface SchedulePoint { hour: number; minute: number; second: number; action: number }
interface ScheduleGroup { id: number; days: number; points: SchedulePoint[] }
interface ScheduleAction { id: number; name: string; color: number }
export class NfRoomHvac extends LitElement {
static properties = { hass: { attribute: false }, room: { attribute: false }, _pending: { state: true }, _error: { state: true } };
declare hass: HomeAssistant;
declare room: Room;
private declare _pending: Set<string>;
private declare _error: string;
constructor() { super(); this._pending = new Set(); this._error = ""; }
private async send(id: string, domain: string, service: string, data: Record<string, unknown>) {
this._pending = new Set([...this._pending, id]);
this._error = "";
try { await this.hass.callService(domain, service, { entity_id: id, ...data }); }
catch (error) { this._error = error instanceof Error ? error.message : String(error); }
finally { this._pending = new Set([...this._pending].filter((x) => x !== id)); }
}
private value(label: string, id: string, control: boolean) {
const st = this.hass.states[id], value = nativeValue(st), a = st?.attributes;
const writable = control && !!a?.symcon_has_action && value !== null && !this._pending.has(id);
return html`<div class="value"><span>${label}</span><b>${nativeText(st)}${trendButton(this.hass, [id], label)}</b>
${control && a?.symcon_has_action && a.symcon_variable_type === 0 ? html`<button class="nf-chip" aria-pressed=${nativeBool(st) === true} ?disabled=${!writable} @click=${() => void this.send(id, "symcon", "set_value", { value: !nativeBool(st) })}>${nativeBool(st) ? "Ein" : "Aus"}</button>`
: control && a?.symcon_has_action && [1, 2].includes(Number(a.symcon_variable_type)) ? html`<input type="number" aria-label=${label} .value=${String(nativeNumber(st) ?? "")} min=${String(a.symcon_min ?? 0)} max=${String(a.symcon_max ?? 100)} step=${String(a.symcon_step || (a.symcon_variable_type === 1 ? 1 : 0.1))} ?disabled=${!writable}
@change=${(event: Event) => { const input = event.target as HTMLInputElement; if (input.value.trim() && input.checkValidity()) void this.send(id, "symcon", "set_value", { value: Number(input.value) }); }} />` : nothing}
</div>`;
}
private schedule(id: string) {
const st = this.hass.states[id];
if (!st?.attributes.symcon_schedule) return html`<p class="missing">Schaltuhr nicht verfügbar</p>`;
const actions = (st.attributes.actions ?? []) as ScheduleAction[];
const groups = (st.attributes.groups ?? []) as ScheduleGroup[];
const next = Number(st.attributes.next_run ?? 0);
const nextAction = actions.find((a) => a.id === st.attributes.next_action);
const days = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];
const time = (p: SchedulePoint) => `${String(p.hour).padStart(2, "0")}:${String(p.minute).padStart(2, "0")}${p.second ? `:${String(p.second).padStart(2, "0")}` : ""}`;
return html`<details class="schedule"><summary>${String(st.attributes.friendly_name ?? "Schaltuhr")}</summary>
<label class="nf-check"><input type="checkbox" .checked=${st.state === "on"} ?disabled=${this._pending.has(id)} @change=${(e: Event) => void this.send(id, "schedule", "set_active", { active: (e.target as HTMLInputElement).checked })} /> Aktiv</label>
${next > 0 ? html`<p class="next">Nächster Schaltpunkt: ${new Date(next * 1000).toLocaleString(this.hass.language)}${nextAction ? ` · ${nextAction.name}` : ""}</p>` : nothing}
${groups.length ? groups.map((group) => html`<div class="schedule-group"><b>${days.filter((_, i) => group.days & (1 << i)).join(" · ")}</b><div class="points">
${[...group.points].sort((a, b) => a.hour * 3600 + a.minute * 60 + a.second - (b.hour * 3600 + b.minute * 60 + b.second)).map((point) => {
const action = actions.find((a) => a.id === point.action);
return html`<span class="point"><time>${time(point)}</time><span class="dot" style=${`background:#${Number(action?.color ?? 0x8796a9).toString(16).padStart(6, "0").slice(-6)}`}></span>${action?.name || "Aktion"}</span>`;
})}
</div></div>`) : html`<p class="missing">Keine Schaltpunkte vorhanden.</p>`}
</details>`;
}
render() {
if (!this.hass || !this.room) return nothing;
const rows = ROOM_HVAC_FIELDS.filter((field) => field.key !== "status" && this.room.hvac?.[field.key] && this.room.hvac[field.key] !== "none");
const plans = this.room.hvac?.schedules ?? [];
const heating = this.room.floor_heating && this.room.floor_heating !== "none" ? this.room.floor_heating : null;
return html`${heating || rows.length ? html`<section><h3>Raumklima</h3>${heating ? this.value("Fußbodenheizung", heating, true) : nothing}${rows.map((field) => this.value(field.label, this.room.hvac![field.key]!, !!field.control))}</section>` : nothing}
${plans.length ? html`<section><h3>Schaltuhren</h3>${plans.map((id) => this.schedule(id))}</section>` : nothing}
${this._error ? html`<p role="alert" class="error">${this._error}</p>` : nothing}`;
}
static styles = [tokens, controls, css`
:host { display:block; }
section { margin:18px 0; }
h3 { font-size:12px; text-transform:uppercase; letter-spacing:.05em; color:var(--nf-muted); margin:0 0 8px; }
.value { display:grid; grid-template-columns:minmax(0,1fr) auto; gap:8px; align-items:center; padding:10px 0; border-bottom:1px solid var(--nf-line); }
.value b { font-size:13px; color:var(--nf-muted); }
.value input,.value button { grid-column:2; max-width:110px; min-width:0; }
input[type=number] { background:var(--nf-fill); color:var(--nf-text); border:1px solid var(--nf-line); border-radius:8px; padding:6px 8px; font:inherit; box-sizing:border-box; }
.schedule { margin:8px 0; padding:10px; border:1px solid var(--nf-line); border-radius:10px; }
summary { cursor:pointer; font-weight:600; }
.nf-check { margin-top:10px; }
.next,.missing { color:var(--nf-muted); font-size:12px; }
.schedule-group { padding:10px 0; border-top:1px solid var(--nf-line); font-size:12px; }
.points { display:flex; gap:6px 14px; flex-wrap:wrap; margin-top:8px; }
.point { display:inline-flex; align-items:center; gap:6px; }
.point time { font-variant-numeric:tabular-nums; }
.dot { width:8px; height:8px; border-radius:50%; }
.error { color:var(--nf-danger,#ff6b7a); font-size:12px; }
`];
}
if (!customElements.get("nf-room-hvac")) customElements.define("nf-room-hvac", NfRoomHvac);