// The house-wide settings forms: favourites, own buttons, station presets, the start camera, and who is tracked by which sensor. import { html, nothing, type TemplateResult } from "lit"; import { entityName, outsideEntities } from "../../../devices.ts"; import type { I18nKey } from "../../../i18n.ts"; import { BUTTON_ACTIONS, uid, type ButtonAction, type Circuit, type CustomButton, type ForecastCard, type MediaPreset, type OwnAlert, type StartView } from "../../../model.ts"; import { entityOptions, isNumeric } from "../entities.ts"; import { isSymcon } from "../../../entities/symcon-values.ts"; import { renderSymconPresence } from "../symcon-fields.ts"; import { actions, checkField, colorField, entityField, hint, iconField, selectField, textField } from "../fields.ts"; import type { EditorHost } from "../host.ts"; import { likelyRoomSensors, parseButtonData, patchPresence, startViewYaml } from "../ops/house.ts"; import { moveWithin, orUndefined, patchAt, removeAt } from "../ops/lists.ts"; /** What can be favourites: things that run or switch. */ const FAVORITE_DOMAINS = ["scene", "script", "automation", "button", "input_button", "switch", "input_boolean", "light", "fan", "cover", "lock"]; const MAX_FAVORITES = 40; const MAX_BUTTONS = 20; const MAX_PRESETS = 30; const PRESET_TYPES = ["music", "url", "playlist", "SPOTIFY", "AMAZON_MUSIC", "TUNEIN", "APPLE_MUSIC"]; export const TARGET_EXAMPLES: Record = { navigate: "/lovelace/rollos", more_info: "cover.wohnzimmer", service: "script.turn_on", fire_dom_event: "" }; const iconButton = (title: string, label: string, onClick: () => void, disabled = false) => html``; /** Scenes, scripts and switches that show in the star menu of the 3D view, in the order given here. */ function favorites(host: EditorHost): TemplateResult { const list = host.doc.settings.favorites ?? []; const store = (next: string[]) => host.change((d) => (d.settings.favorites = orUndefined(next))); const floors = host.doc.floors; const only = host.doc.settings.favorite_floors ?? {}; const options = entityOptions(host.hass, (id) => FAVORITE_DOMAINS.includes(id.split(".")[0]) && !list.includes(id)); const row = (id: string, i: number) => html`
${entityName(host.hass, id)} ${floors.length > 1 ? html`` : nothing} ${iconButton(host.t("move_up"), "↑", () => store(moveWithin(list, i, -1)), i === 0)} ${iconButton(host.t("move_down"), "↓", () => store(moveWithin(list, i, 1)), i === list.length - 1)} ${iconButton(host.t("delete"), "✕", () => store(removeAt(list, i)))}
`; return html`${host.t("favorites")}${list.length > 0 ? html` ${list.length}` : nothing} ${hint(host.t("favorites_hint"))} ${list.map(row)} ${list.length < MAX_FAVORITES ? html`
${entityField(host, host.t("favorites_add"), null, undefined, options, (v) => v && v !== "none" && !list.includes(v) && store([...list, v]), { excluded: (id) => list.includes(id) ? "Diese Variable ist bereits als Favorit verknüpft." : undefined })}
` : nothing}`; } /** Devices the Outside panel leaves out (they stay where they are in the plan). */ function outsideHidden(host: EditorHost, paneView?: () => StartView | null): TemplateResult { const list = host.doc.settings.outside_hidden ?? []; const store = (next: string[]) => host.change((d) => (d.settings.outside_hidden = orUndefined(next))); // worked out once: asking for every entity would run the whole search once per entity of Home Assistant (#101) const outside = new Set(host.hass ? outsideEntities(host.hass, host.doc.floors) : []); const options = entityOptions(host.hass, (id) => !list.includes(id) && outside.has(id)); return html`${paneView ? outsideView(host, paneView) : nothing}

${host.t("outside_hidden")}${list.length > 0 ? html` ${list.length}` : nothing}

${hint(host.t("outside_hidden_hint"))} ${list.map((id, i) => html`
${entityName(host.hass, id)}${iconButton(host.t("delete"), "✕", () => store(removeAt(list, i)))}
`)}
${entityField(host, host.t("outside_hidden_add"), null, undefined, options, (v) => v && v !== "none" && !list.includes(v) && store([...list, v]), { excluded: (id) => list.includes(id) ? "Diese Variable ist hier bereits eingetragen." : undefined })}
`; } /** The camera Outside opens with: the editor's 3D pane as it stands now (#83). */ function outsideView(host: EditorHost, paneView: () => StartView | null): TemplateResult { const saved = host.doc.settings.outside_view ?? null; const remember = () => { const view = paneView(); if (view) host.change((d) => (d.settings.outside_view = view)); }; return html`${hint(host.t("outside_view_hint"))} ${actions( html``, saved ? html`` : nothing, )}`; } /** Own warnings: a text in the warning bar while an entity is in a state (the robot's water tank is empty, the dishwasher is done). */ function ownAlerts(host: EditorHost): TemplateResult { const list = host.doc.settings.own_alerts ?? []; const store = (next: OwnAlert[]) => host.change((d) => (d.settings.own_alerts = orUndefined(next))); const edit = (i: number, patch: Partial) => store(patchAt(list, i, patch)); const rooms: [string, string][] = [["", host.t("own_alert_house")], ...host.doc.floors.flatMap((f) => f.rooms.map((r): [string, string] => [r.id, `${r.name} · ${f.name}`]))]; const card = (a: OwnAlert, i: number) => html`
${entityField(host, host.t("own_alert_entity"), a.entity || null, undefined, entityOptions(host.hass, () => true), (v) => edit(i, { entity: v && v !== "none" ? v : "" }))} ${textField(host, host.t("own_alert_state"), a.state ?? "", (v) => edit(i, { state: v || null }), { placeholder: host.hass?.states[a.entity]?.state ?? "on · <20 · >80" })} ${textField(host, host.t("own_alert_text"), a.text, (v) => edit(i, { text: v || host.t("own_alert_new") }), { wide: true, maxlength: 120 })} ${selectField(host, host.t("room"), a.room_id ?? "", rooms, (v) => edit(i, { room_id: v || null }), { wide: true })} ${colorField(host, host.t("own_alert_color"), a.color ?? null, "#ffb547", (color) => edit(i, { color }))} ${selectField(host, host.t("own_alert_tap"), a.tap ?? "", [["", host.t("own_alert_tap_info")], ["toggle", host.t("own_alert_tap_toggle")], ["room", host.t("own_alert_tap_room")]], (v) => edit(i, { tap: (v || null) as OwnAlert["tap"] }))}
`; return html`

⚠ ${host.t("own_alerts")}

${hint(host.t("own_alerts_hint"))} ${list.map(card)} ${list.length < 40 ? actions(html``) : nothing}`; } /** The fuse box: circuits by name, each with the rooms it feeds (chips to add and remove). */ function circuits(host: EditorHost): TemplateResult { const list = host.doc.settings.circuits ?? []; const store = (next: Circuit[]) => host.change((d) => (d.settings.circuits = orUndefined(next))); const edit = (i: number, patch: Partial) => store(patchAt(list, i, patch)); const rooms = host.doc.floors.flatMap((f) => f.rooms.map((r) => ({ id: r.id, label: host.doc.floors.length > 1 ? `${r.name} · ${f.name}` : r.name }))); const nameOf = (id: string) => rooms.find((r) => r.id === id)?.label ?? id; const card = (c: Circuit, i: number) => html`
${textField(host, host.t("circuit_name"), c.name, (v) => edit(i, { name: v || host.t("circuit_new") }), { wide: true, maxlength: 80 })}
${c.rooms.map((id) => html`${nameOf(id)} `)}
${selectField(host, host.t("circuit_add_room"), "", [["", "–"], ...rooms.filter((r) => !c.rooms.includes(r.id)).map((r): [string, string] => [r.id, r.label])], (v) => v && edit(i, { rooms: [...c.rooms, v] }), { wide: true })}
`; return html`

🔌 ${host.t("circuits")}

${hint(host.t("circuits_hint"))} ${list.map(card)} ${list.length < 120 ? actions(html``) : nothing}`; } /** The calendars of the "Today" card: their appointments of the day show top right in the 3D view, with the bins due. */ function calendars(host: EditorHost): TemplateResult { const list = host.doc.settings.calendars ?? []; const store = (next: string[]) => host.change((d) => (d.settings.calendars = orUndefined(next))); const options = entityOptions(host.hass, (id) => id.startsWith("calendar.") && !list.includes(id)); const row = (id: string, i: number) => html`
${entityName(host.hass, id)} ${iconButton(host.t("delete"), "✕", () => store(removeAt(list, i)))}
`; const t = host.t; const days = host.doc.settings.today_days ?? 1; return html`

📅 ${t("today_calendars")}

${hint(t("today_calendars_hint"))} ${list.map(row)} ${list.length < 12 ? html`
${entityField(host, t("favorites_add"), null, undefined, options, (v) => v && v !== "none" && !list.includes(v) && store([...list, v]), { unsupported: "Dieses Feld benötigt eine Kalenderquelle mit Terminen. Eine Symcon-Variable ist kein Kalender; Kalenderquellen werden vom Symcon-Modul derzeit nicht bereitgestellt." })}
` : nothing} ${list.length ? html`
${selectField(host, t("today_days"), String(days), [1, 2, 3, 5, 7].map((n): [string, string] => [String(n), t(n === 1 ? "today_days_one" : "today_days_n", { n })]), (v) => host.change((d) => (d.settings.today_days = Number(v) > 1 ? Number(v) : undefined)))}
` : nothing} ${forecastCard(host)}`; } /** The weather forecast card (#84): off, or a number of days in a top corner; the weather entity is the one of the settings. */ function forecastCard(host: EditorHost): TemplateResult { const t = host.t; const card = host.doc.settings.forecast ?? null; const set = (next: ForecastCard | null) => host.change((d) => (d.settings.forecast = next ?? undefined)); return html`

🌦 ${t("forecast")}

${hint(t("forecast_hint"))}
${selectField(host, t("forecast_days"), card ? String(card.days) : "0", [0, 3, 5, 7].map((n): [string, string] => [String(n), n ? t("today_days_n", { n }) : t("forecast_off")]), (v) => set(Number(v) > 0 ? { days: Number(v), corner: card?.corner ?? "top_right" } : null), )} ${card ? selectField(host, t("forecast_corner"), card.corner, [["top_right", t("clock_top_right")], ["top_left", t("clock_top_left")]], (v) => set({ ...card, corner: v === "top_left" ? "top_left" : "top_right" })) : nothing}
`; } /** Stations and playlists that start on a speaker from its quick menu. */ function mediaPresets(host: EditorHost): TemplateResult { const list = host.doc.settings.media_presets ?? []; const store = (next: MediaPreset[]) => host.change((d) => (d.settings.media_presets = orUndefined(next))); const edit = (i: number, patch: Partial) => store(patchAt(list, i, patch)); // the speakers of the plan: media players placed as devices or linked to furniture const speakers = [...new Set(host.doc.floors.flatMap((f) => [...f.placements.map((p) => p.entity_id), ...f.furniture.map((m) => m.entity ?? "")]).filter((id) => id.startsWith("media_player.")))]; const card = (p: MediaPreset, i: number) => html`
${textField(host, host.t("own_button_label"), p.label, (v) => edit(i, { label: v || "Radio" }), { maxlength: 60 })} ${textField(host, host.t("preset_type"), p.type, (v) => edit(i, { type: v || "music" }), { title: host.t("preset_type_hint") })} ${textField(host, host.t("preset_content"), p.content, (v) => edit(i, { content: v }), { wide: true, title: host.t("preset_content_hint"), placeholder: "https://… · spotify:playlist:… · Rock Antenne" })} ${speakers.length > 1 ? html`
${host.t("preset_speakers")}
${speakers.map((id) => checkField(host, entityName(host.hass, id), !!p.speakers?.includes(id), (on) => edit(i, { speakers: orUndefined(on ? [...(p.speakers ?? []), id] : (p.speakers ?? []).filter((s) => s !== id)) }), { wide: false }))}` : nothing}
`; return html`

${host.t("presets")}

${hint(host.t("presets_hint"))} ${PRESET_TYPES.map((type) => html``)} ${list.map(card)} ${list.length < MAX_PRESETS ? actions(html``) : nothing}`; } /** The extra data of an own button, as JSON; a text that is not an object is flagged on the field instead of being stored. */ export function dataField(host: EditorHost, button: Pick, onData: (data: Record | null) => void): TemplateResult { const example = button.action === "fire_dom_event" ? '{"browser_mod": {"service": "browser_mod.popup", "data": {"title": "Rollos", "content": {"type": "custom:my-cover-card"}}}}' : '{"entity_id": "script.party"}'; const onChange = (e: Event) => { const area = e.target as HTMLTextAreaElement; area.setCustomValidity(""); const parsed = parseButtonData(area.value); if (parsed.ok) return onData(parsed.value); area.setCustomValidity(host.t("own_data_bad")); area.reportValidity(); }; return html``; } /** Own buttons of the star menu: label, icon, what they do, the target, and extra data. */ function ownButtons(host: EditorHost): TemplateResult { const list = host.doc.settings.buttons ?? []; const store = (next: CustomButton[]) => host.change((d) => (d.settings.buttons = orUndefined(next))); const edit = (i: number, patch: Partial) => store(patchAt(list, i, patch)); const actionChoices = BUTTON_ACTIONS.map((a): [ButtonAction, string] => [a, host.t(`own_action_${a}` as I18nKey)]); const card = (b: CustomButton, i: number) => html`
${textField(host, host.t("own_button_label"), b.label, (v) => edit(i, { label: v || host.t("own_button_new") }), { maxlength: 60 })} ${selectField(host, host.t("own_button_action"), b.action, actionChoices, (action) => edit(i, { action }))} ${iconField(host, b.icon ?? null, (icon) => edit(i, { icon }))} ${b.action !== "fire_dom_event" ? textField(host, host.t(`own_target_${b.action}` as I18nKey), b.target ?? "", (v) => edit(i, { target: v || null }), { wide: true, placeholder: TARGET_EXAMPLES[b.action] }) : nothing} ${b.action === "service" || b.action === "fire_dom_event" ? dataField(host, b, (data) => edit(i, { data })) : nothing} ${entityField(host, host.t("own_button_state"), b.state_entity ?? null, undefined, entityOptions(host.hass, (id) => /^(switch|light|input_boolean|binary_sensor|fan|cover|media_player|climate|lock|person|device_tracker)\./.test(id)), (v) => edit(i, { state_entity: v && v !== "none" ? v : null }))}
`; return html`

${host.t("own_buttons")}

${hint(host.t("own_buttons_hint"))} ${list.map(card)} ${list.length < MAX_BUTTONS ? actions(html``) : nothing}`; } /** Which facts the room panel shows in its header. */ function roomFacts(host: EditorHost): TemplateResult { const off = host.doc.settings.room_facts_off ?? []; const facts = (["sun", "comfort", "air", "circuits"] as const).map((k) => checkField(host, host.t(`room_fact_${k}` as I18nKey), !off.includes(k), (on) => host.change((d) => (d.settings.room_facts_off = orUndefined(on ? off.filter((x) => x !== k) : [...off, k])))), ); return html`

${host.t("room_facts")}

${facts}
`; } /** The favourites section with the own buttons and station presets below it (they all belong to the star menu). */ export function renderFavorites(host: EditorHost): TemplateResult { return html`
${favorites(host)} ${ownButtons(host)} ${ownAlerts(host)} ${circuits(host)} ${calendars(host)} ${mediaPresets(host)} ${roomFacts(host)}
`; } /** Outside as a section of its own (#83): the camera it opens with first, then the devices its panel leaves out. */ export function renderOutside(host: EditorHost, paneView: () => StartView | null): TemplateResult { return html`
🌳 ${host.t("outside_area")} ${outsideHidden(host, paneView)}
`; } /** The camera the house opens with: the editor's 3D pane as it stands now, or the default. */ export function renderStartView(host: EditorHost, paneView: () => StartView | null): TemplateResult { const saved = host.doc.settings.start_view ?? null; const remember = () => { const view = paneView(); if (view) host.change((d) => (d.settings.start_view = view)); }; return html`
${host.t("start_view")} ${hint(host.t("start_view_hint"))} ${actions( html``, saved ? html`` : nothing, )} ${saved ? html`${hint(host.t("start_view_saved"))} ${hint(host.t("start_view_card"))}${startViewYaml(saved)}` : nothing}
`; } /** Which room sensor says where each person is. */ export function renderPresence(host: EditorHost): TemplateResult { if (isSymcon(host.hass)) return renderSymconPresence(host); const linked = host.doc.presence.map((l) => l.person); const persons = [...new Set([...Object.keys(host.hass?.states ?? {}).filter((id) => id.startsWith("person.")).sort(), ...linked])]; const numeric = entityOptions(host.hass, isNumeric); const rooms = host.doc.floors.flatMap((f) => f.rooms.map((r) => ({ id: r.id, label: host.doc.floors.length > 1 ? `${f.name} · ${r.name}` : r.name }))); const change = (person: string, patch: { sensor?: string | null; room?: string | null }) => host.replace({ ...structuredClone(host.doc), presence: patchPresence(host.doc.presence, person, patch) }); const row = (person: string) => { const link = host.doc.presence.find((p) => p.person === person); const extra = !person.startsWith("person."); return html`${entityField(host, `${entityName(host.hass, person)} · ${host.t("presence_sensor")}`, link?.sensor ?? null, undefined, likelyRoomSensors(numeric, person), (sensor) => change(person, { sensor }))} ${extra ? html`` : nothing}`; }; const more = entityOptions(host.hass, (id) => /^(input_boolean|binary_sensor|device_tracker)\./.test(id) && !persons.includes(id)); return html`
${host.t("presence")}
${persons.length > 0 ? persons.map(row) : html`

${host.t("no_persons")}

`}
${entityField(host, host.t("presence_add"), null, undefined, more, (v) => v && v !== "none" && host.replace({ ...structuredClone(host.doc), presence: [...host.doc.presence, { person: v, sensor: null }] }))}
${hint(host.t("presence_hint"))}
`; } /** Locating Bluetooth tags by room: pick a tag, walk through the rooms, record them (the element does the work). */ export function renderBleLocate(host: EditorHost, doc: unknown): TemplateResult { if (isSymcon(host.hass)) return html``; return html`
${host.t("ble")} ${hint(host.t("ble_hint"))}
`; }