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; private declare _error: string; constructor() { super(); this._pending = new Set(); this._error = ""; } private async send(id: string, domain: string, service: string, data: Record) { 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`
${label}${nativeText(st)}${trendButton(this.hass, [id], label)} ${control && a?.symcon_has_action && a.symcon_variable_type === 0 ? html`` : control && a?.symcon_has_action && [1, 2].includes(Number(a.symcon_variable_type)) ? html` { const input = event.target as HTMLInputElement; if (input.value.trim() && input.checkValidity()) void this.send(id, "symcon", "set_value", { value: Number(input.value) }); }} />` : nothing}
`; } private schedule(id: string) { const st = this.hass.states[id]; if (!st?.attributes.symcon_schedule) return html`

Schaltuhr nicht verfügbar

`; 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`
${String(st.attributes.friendly_name ?? "Schaltuhr")} ${next > 0 ? html`

Nächster Schaltpunkt: ${new Date(next * 1000).toLocaleString(this.hass.language)}${nextAction ? ` · ${nextAction.name}` : ""}

` : nothing} ${groups.length ? groups.map((group) => html`
${days.filter((_, i) => group.days & (1 << i)).join(" · ")}
${[...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`${action?.name || "Aktion"}`; })}
`) : html`

Keine Schaltpunkte vorhanden.

`}
`; } 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`

Raumklima

${heating ? this.value("Fußbodenheizung", heating, true) : nothing}${rows.map((field) => this.value(field.label, this.room.hvac![field.key]!, !!field.control))}
` : nothing} ${plans.length ? html`

Schaltuhren

${plans.map((id) => this.schedule(id))}
` : nothing} ${this._error ? html`` : 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);