// The side panel's room part: the list of rooms, and the form of one room with its corners, packages and spot grid. import { html, nothing, type TemplateResult } from "lit"; import { formatNumber, type I18nKey } from "../../../i18n.ts"; import { bounds, DEFAULT_PAINT_INSIDE, FLOOR_MATERIALS, isAxisRect, polygonArea, type Floor, type FurnitureType, type Room } from "../../../model.ts"; 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 { 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"; import { generateWalls, innerSize, type Wall } from "../../../geometry/walls.ts"; import { countsAsLiving, roomArea } from "../../../geometry/living-area.ts"; import { roomSpeaker } from "../../../live/announce.ts"; import { isSymcon } from "../../../entities/symcon-values.ts"; /** What the room form makes the editor do, and the parts of it that the editor still draws itself. */ export interface RoomActions { select(roomId: string): void; patch(patch: Partial): void; setArea(areaId: string): void; setRect(field: RectField, value: number): void; setCorner(index: number, axis: 0 | 1, value: number): void; removeCorner(index: number): void; duplicate(): void; remove(): void; rememberView(): void; applyPackage(pkg: PackageId): void; openSpots(): void; placeSpots(): void; /** The lock button of the room (it shows the lock on the whole plan). */ lockButton: TemplateResult | typeof nothing; /** The editor's own sections of the form: heights of the walls, and the climate sensors. */ wallHeights: TemplateResult | typeof nothing; climate: TemplateResult | typeof nothing; /** The selected corner of this room, if any. */ corner: number | null; } /** All rooms of the floor with their size, to pick one. */ export function renderRoomList(host: EditorHost, floor: Floor, actions: Pick): TemplateResult | typeof nothing { if (floor.rooms.length === 0) return nothing; return html`

${host.t("rooms")}

${floor.rooms.map( (room) => html``, )}
`; } /** The planning form of a spot grid: type, columns, rows, the light to follow, and how many spots that makes. */ function spotForm(host: EditorHost, state: SideState, room: Room, actions: RoomActions): TemplateResult { const draft = state.spots!; const change = (patch: Partial) => (state.spots = { ...draft, ...patch }); const count = spotItems(room, draft).length; const lights = entityOptions(host.hass, (id) => /^(light|switch|input_boolean)\./.test(id)); const types = SPOT_TYPES.map((type): [FurnitureType, string] => [type, host.t(`furn_${type}` as I18nKey)]); return html`
${selectField(host, host.t("spots_type"), draft.type, types, (type) => change({ type }), { wide: true })} ${numberField(host, host.t("spots_cols"), draft.cols, (v) => change({ cols: spotCount(v) }), 1, 1)} ${numberField(host, host.t("spots_rows"), draft.rows, (v) => change({ rows: spotCount(v) }), 1, 1)} ${entityField(host, host.t("furn_entity_light"), draft.entity, undefined, lights, (v) => change({ entity: v === "none" ? null : v }), { types: [0, 1, 2], role: "light" })}
${hint(host.t("spots_hint"), "nf-wide")}
`; } /** The corners of a room as number pairs. They are unfolded when the room is no rectangle, so the shape can be typed in. */ function corners(host: EditorHost, room: Room, rectangle: boolean, actions: RoomActions): TemplateResult { return html`
${host.t("points")} (${room.points.length}) ${room.points.map( (point, i) => html`
${i + 1} ${numberField(host, host.t("x"), point[0], (v) => actions.setCorner(i, 0, v))} ${numberField(host, host.t("z"), point[1], (v) => actions.setCorner(i, 1, v))} ${host.admin ? html`` : nothing}
`, )}
`; } /** The room's living area (counted like the Wohnflächenverordnung), with its floor area when part of it lies under a slope. */ function livingLine(host: EditorHost, room: Room, walls: readonly Wall[]): TemplateResult | typeof nothing { const floor = host.floor; // only this room: the form draws again on every step of a drag const a = floor ? roomArea(host.doc, floor, room, walls) : undefined; if (!a || a.floor <= 0) return nothing; const t = host.t; if (!countsAsLiving(room)) return html`

${t("living_area_none", { floor: formatArea(host.hass, a.floor) })}

`; return html`

${a.low > 0.05 ? t("living_area_low", { living: formatArea(host.hass, a.living), floor: formatArea(host.hass, a.floor), low: formatArea(host.hass, a.low) }) : t("living_area", { living: formatArea(host.hass, a.living) })}

`; } /** * The room's own ceiling height: a low bathroom on a high floor, or a living room open to the floor above (#55) – its walls rise * with it and the floor above stays open over it. At the floor height it is the floor's again. */ function ceilingField(host: EditorHost, room: Room, floor: Floor, actions: RoomActions): TemplateResult { const t = host.t; const set = (v: number) => actions.patch({ ceiling: Math.abs(v - floor.height) < 0.005 ? null : Math.min(20, Math.max(1, Math.round(v * 1000) / 1000)) }); return html`${numberField(host, t("room_ceiling"), room.ceiling ?? floor.height, set, 0.01, 1)} ${room.ceiling != null ? html`` : nothing} ${hint(t("room_ceiling_tip"), "nf-wide")} ${room.ceiling != null && room.ceiling > floor.height + 0.005 ? hint(t("room_ceiling_through", { ceiling: formatNumber(host.hass, room.ceiling, 2), floor: formatNumber(host.hass, floor.height, 2) }), "nf-wide") : nothing}`; } /** Announcements in the room: its speaker (automatic, chosen, or none) and a quiet time. */ function announceFields(host: EditorHost, room: Room, floor: Floor | null, actions: RoomActions): TemplateResult { const t = host.t; if (!host.hass || isSymcon(host.hass)) return html``; const auto = roomSpeaker(host.hass, floor, { ...room, announce_speaker: null }); const speakers = entityOptions(host.hass, (id) => /^(media_player|assist_satellite)\./.test(id) || (id.startsWith("notify.") && id.endsWith("_announce"))); const quiet = room.announce_quiet ?? null; // the first time typed fills the other with the usual night (20:00–07:00), so a half quiet time is never stored const setQuiet = (part: "from" | "to", value: string) => { if (!value) return actions.patch({ announce_quiet: null }); actions.patch({ announce_quiet: { from: quiet?.from ?? "20:00", to: quiet?.to ?? "07:00", [part]: value } }); }; return html`${entityField(host, t("room_announce_speaker"), room.announce_speaker ?? null, auto, speakers, (v) => actions.patch({ announce_speaker: v }))}
${t("room_announce_quiet")} setQuiet("from", (e.target as HTMLInputElement).value)} /> – setQuiet("to", (e.target as HTMLInputElement).value)} /> ${quiet ? html`` : nothing}
${hint(t("room_announce_hint"), "nf-wide")}`; } /** The form of the selected room. */ export function renderRoomForm(host: EditorHost, state: SideState, room: Room, actions: RoomActions): TemplateResult { const t = host.t; const rectangle = isAxisRect(room.points); const box = bounds(room.points); const areas = Object.values(host.hass?.areas ?? {}).sort((a, b) => a.name.localeCompare(b.name)); const areaChoices: [string, string][] = [["", t("no_area")], ...areas.map((a): [string, string] => [a.area_id, a.name])]; // from wall face to wall face (the outline runs along the middle of inner walls) const walls = host.floor ? generateWalls(host.floor.rooms, { exterior: host.doc.settings.wall_exterior, interior: host.doc.settings.wall_interior }, host.floor.walls ?? []).walls : []; const inner = host.floor ? innerSize(room, walls) : null; const materials = FLOOR_MATERIALS.map((m): [string, string] => [m, t(`mat_${m}` as I18nKey)]); const packageButton = (pkg: PackageId) => html``; return html`

${t("room")}

${actions.lockButton}
${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`` : nothing} ${(room.sub_areas ?? []).map((a) => html`${host.hass?.areas?.[a]?.name ?? a} `)} ${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") }) : nothing} ${selectField(host, t("material"), room.floor_material, materials, (floor_material) => actions.patch({ floor_material }), { wide: true })} ${colorField(host, t("wall_paint_room"), room.wall_paint, host.doc.settings.wall_paint_inside ?? DEFAULT_PAINT_INSIDE, (wall_paint) => actions.patch({ wall_paint }))} ${host.floor ? ceilingField(host, room, host.floor, actions) : nothing} ${numberField(host, t("room_floor_offset"), room.floor_offset ?? 0, (v) => actions.patch({ floor_offset: Math.abs(v) < 0.005 ? null : Math.max(-2, Math.min(2, Math.round(v * 1000) / 1000)) }), 0.01)} ${hint(t("room_floor_offset_hint"), "nf-wide")} ${entityField(host, t("room_clean"), room.clean_entity ?? null, undefined, entityOptions(host.hass, (id) => /^(script|button|input_button)\./.test(id)), (v) => actions.patch({ clean_entity: v && v !== "none" ? v : null }))} ${room.clean_entity ? nothing : hint(t("room_clean_hint"), "nf-wide")} ${isSymcon(host.hass) ? nothing : entityField(host, t("floor_heating"), room.floor_heating ?? null, undefined, entityOptions(host.hass, (id) => /^(climate|switch|valve|binary_sensor|input_boolean)\./.test(id)), (v) => actions.patch({ floor_heating: v && v !== "none" ? v : null }))} ${room.floor_heating && !isSymcon(host.hass) ? hint(t("floor_heating_hint"), "nf-wide") : nothing} ${announceFields(host, room, host.floor ?? null, actions)} ${inner ? html`

${t("room_inner", { w: formatLength(host.hass, inner[0]), d: formatLength(host.hass, inner[1]) })}

` : nothing} ${livingLine(host, room, walls)} ${checkField(host, t("room_living"), countsAsLiving(room), (on) => actions.patch({ living: on === countsAsLiving({ name: room.name, living: null }) ? null : on }), { title: t("living_area_hint") })} ${rectangle ? html`${numberField(host, t("x"), box.x0, (v) => actions.setRect("x", v))} ${numberField(host, t("z"), box.z0, (v) => actions.setRect("z", v))} ${numberField(host, t("width"), box.x1 - box.x0, (v) => actions.setRect("w", v), 0.01, 0.05)} ${numberField(host, t("depth"), box.z1 - box.z0, (v) => actions.setRect("d", v), 0.01, 0.05)}` : nothing}
${room.start_view ? html`` : nothing}
${actions.wallHeights} ${actions.climate} ${corners(host, room, rectangle, actions)} ${host.admin ? html`
` : nothing} ${state.spots ? spotForm(host, state, room, actions) : nothing} ${state.packages ? html`
${PACKAGES.map(packageButton)}${hint(t("pkg_hint"))}
` : nothing}
`; }