// The Lovelace card. The integration loads it automatically; no resource has to be added. import { html, LitElement, nothing, type PropertyValues, type TemplateResult } from "lit"; import { BuildingController } from "./building-controller.ts"; import type { CardConfig, CardControl } from "./card-config.ts"; import { KioskTimers } from "./card/kiosk.ts"; import { roomButtons } from "./card/room-buttons.ts"; import { cardStyles } from "./card/styles.ts"; import "./components/room-panel.ts"; import "./components/view3d.ts"; import { confirmEntities, outsideEntities } from "./devices.ts"; import { awaitingLanguage, translate } from "./i18n.ts"; import { nightActive } from "./kiosk.ts"; import { screenMuted, screenName } from "./panel/prefs.ts"; import { loadCardEditor } from "./load-card-editor.ts"; import { withTemplate, type CardTemplates } from "./card-template.ts"; import type { Building } from "./model.ts"; import { roomsOnly } from "./rooms-only.ts"; import { colorSchemeOf, controls, readableStyles, tokens } from "./styles.ts"; import type { HomeAssistant } from "./types.ts"; import { withCoverControls } from "./entities/cover-controls.ts"; import { withSymconWeather } from "./symcon-weather.ts"; import { isSymcon } from "./entities/symcon-values.ts"; import { ALL_TOPOLOGY, floorSelection, isOutdoorFloor, outsideFloor, topologyOutsideFloors, topologyParent, type TopologySelection } from "./topology.ts"; import type { WallMode } from "./viewer/viewer3d.ts"; import { lookNow } from "./themes.ts"; /** The room id the card uses for Outside (no room has it: room ids are made by uid()). */ const OUTSIDE = "nf-outside"; type HeatMode = NonNullable; type Key = Parameters[1]; interface Stage { floorId: string | null; walls: WallMode; heat: HeatMode; explode: boolean; showBar: boolean; canGoBack: boolean; } export class NextFloorCard extends LitElement { static properties = { hass: { attribute: false }, _config: { state: true }, _roomId: { state: true }, _topologyGroup: { state: true }, _floorId: { state: true }, _walls: { state: true }, _heat: { state: true }, _explode: { state: true }, _fullscreen: { state: true }, _clean: { state: true }, _night: { state: true }, _orbit: { state: true }, _roomNames: { state: true }, }; declare hass: HomeAssistant; private declare _config: CardConfig; /** The options as written on the dashboard card; `_config` is these with the card template underneath. */ private _own: CardConfig | null = null; private templatesSeen: unknown; private declare _roomId: string | null; /** The configured start room has been opened once the building arrived. */ private roomApplied = false; /** The floor chosen in the card: null is the house, undefined means none was chosen so the configured floor applies. */ private declare _topologyGroup: TopologySelection; private declare _floorId: string | null | undefined; /** Choices made with the card's own switches; null means as configured. */ private declare _walls: WallMode | null; private declare _heat: HeatMode | null; private declare _explode: boolean | null; private declare _fullscreen: boolean; /** The clean view: only the stage (option controls_hidden, the eye button, or the hide-after timer). */ private declare _clean: boolean; /** Kiosk: the night dimming is on, and the screensaver turn runs (after an idle return). */ private declare _night: boolean; private declare _orbit: boolean; private declare _roomNames: boolean | null; private readonly data = new BuildingController(this); private readonly kiosk = new KioskTimers({ config: () => this._config, hass: () => this.hass, setClean: (clean) => (this._clean = clean), isClean: () => this._clean, setOrbit: (on) => (this._orbit = on), isOrbit: () => this._orbit, setNight: (night) => (this._night = night), goHome: () => this.returnHome(), }); constructor() { super(); this._roomId = null; this._floorId = undefined; this._topologyGroup=ALL_TOPOLOGY; this._walls = null; this._heat = null; this._explode = null; this._fullscreen = false; this._clean = false; this._night = false; this._orbit = false; this._roomNames = null; } private view3d() { return this.shadowRoot?.querySelector("nf-view3d") as (HTMLElement & { resetView(): void; lookThrough(entityId: string): void }) | null; } /** Back to the start view: room closed, start floor, camera reset. The screensaver may start. */ private returnHome(): void { this._roomId = this._config?.room ?? null; this._floorId = undefined; this._topologyGroup=ALL_TOPOLOGY; this.view3d()?.resetView(); if (this._config?.idle_orbit) this._orbit = true; } /** Opens another dashboard or view the way Home Assistant's own links do, without a page reload. */ private openDashboard(path: string): void { history.pushState(null, "", path); window.dispatchEvent(new CustomEvent("location-changed", { bubbles: true, composed: true, detail: { replace: false } })); } private readonly onFullscreen = () => (this._fullscreen = !!document.fullscreenElement && this.shadowRoot?.contains(document.fullscreenElement) === true); connectedCallback(): void { super.connectedCallback(); document.addEventListener("fullscreenchange", this.onFullscreen); this.kiosk.start(); } disconnectedCallback(): void { super.disconnectedCallback(); document.removeEventListener("fullscreenchange", this.onFullscreen); this.kiosk.stop(); } private toggleFullscreen(): void { if (document.fullscreenElement) void document.exitFullscreen(); else void this.shadowRoot?.querySelector("ha-card")?.requestFullscreen?.(); } /** The visual editor in the dashboard, so no YAML is needed. */ static async getConfigElement(): Promise { await loadCardEditor(); return document.createElement("nextfloor-card-editor"); } static getStubConfig(): CardConfig { return { type: "custom:nextfloor-card" }; } setConfig(config: CardConfig): void { if (config.height !== undefined && !(config.height > 100)) throw new Error("height must be a number of pixels above 100"); this._roomNames = null; this._own = config; this._config = withTemplate(config, this.data.building?.settings.card_templates as CardTemplates | undefined); this._floorId = undefined; this._topologyGroup=ALL_TOPOLOGY; this._walls = null; this._heat = null; this._explode = null; this._orbit = false; this._night = nightActive(config.night, this.hass); this._clean = config.controls_hidden === true; this.kiosk.armClean(); this.kiosk.armIdle(); } getCardSize(): number { return Math.ceil((this._config?.height ?? 420) / 50); } getGridOptions() { return { columns: "full", rows: this._config?.fill ? 12 : Math.ceil((this._config?.height ?? 420) / 56), min_rows: 4 }; } protected willUpdate(changed: PropertyValues): void { // a card template edited in the editor reaches the card as soon as the plan reloads const templates = this.data.building?.settings.card_templates; if (this._own?.template && templates !== this.templatesSeen) { this.templatesSeen = templates; this._config = withTemplate(this._own, templates as CardTemplates | undefined); } this.toggleAttribute("readable", this._config?.readable === true); if (changed.has("hass")) this.style.colorScheme = colorSchemeOf(this.hass); if (!changed.has("hass") || !this.hass) return; this.data.setHass(this.hass); const night = nightActive(this._config?.night, this.hass); if (night !== this._night) this._night = night; } /** Floors can be switched: no fixed floor, or floor pictures to switch with. */ private get canSwitch(): boolean { return !this._config?.floor || this.thumbs; } private get thumbs(): boolean { return this._config?.floor_thumbs ?? !this._config?.floor; } /** One level up: room, then floor, then the house. */ private topologySelection(): TopologySelection { const b=this.plan(),id=this.stage().floorId; return b&&id?floorSelection(b,id,this._roomId===OUTSIDE?null:this._roomId):this._topologyGroup; } private openTopology(s:TopologySelection):void { const b=this.plan();if(!b)return; this._floorId=s.kind==="room"?s.floorId??null:s.kind==="floor"?s.id??null:outsideFloor(b,s)?.id??null; this._roomId=s.kind==="room"?s.id??null:null;this._topologyGroup=s; } private back(): void { if (this._roomId) this._roomId = null; else if(this.plan()?.topology)this.openTopology(topologyParent(this.plan()!,this.topologySelection())); else if (this.canSwitch) this._floorId = null; } private roomsMemo: { source: Building | null; ids: string; result: Building | null } | null = null; /** The plan this card shows: all of it, or only the rooms of `rooms:` (kept, so the same object comes back while nothing changes). */ private plan(): Building | null { const source = this.data.building ?? null; const ids = this._config?.rooms; if (!source || !ids?.length) return source; const key = ids.join(","); if (!this.roomsMemo || this.roomsMemo.source !== source || this.roomsMemo.ids !== key) this.roomsMemo = { source, ids: key, result: roomsOnly(source, ids) }; return this.roomsMemo.result; } private readonly t = (key: Key) => translate(this.hass, key); /** Which floor shows, the switches' current values, and whether the bar of switches and its back button are there. */ private stage(): Stage { const b = this.plan(); const c = this._config; // the configured floor is where the card starts; with floor pictures the user can switch const chosen = this._floorId === undefined ? (c?.floor ?? null) : this._floorId; const floorId = b && !b.topology && b.floors.length === 1 ? b.floors[0].id : b?.floors.some((f) => f.id === chosen) ? chosen : null; // the floor pictures have a house button of their own; a room with its panel has a close button const panelOpen = !!this._roomId && c?.room_panel !== false; const canGoBack = (!!this._roomId && c?.room_panel === false) || (!this.thumbs && this.canSwitch && !this._roomId && !!floorId && (b?.floors.length ?? 0) > 1); return { floorId, walls: this._walls ?? c?.walls ?? "auto", heat: this._heat ?? c?.heatmap ?? "none", explode: this._explode ?? c?.explode ?? true, canGoBack, // the bar of switches at the bottom: the back button belongs to it showBar: !!b && !this._clean && (canGoBack || (!!c?.controls && !panelOpen)), }; } private shows(control: CardControl): boolean { const c = this._config?.controls; return c === true || (Array.isArray(c) && c.includes(control)); } /** The bar of switches: back, wall mode, floor stacking, heat map. */ private renderBar(s: Stage): TemplateResult { const b = this.plan()!; const t = this.t; const heats = (["temperature", "humidity", "co2"] as const).filter((m) => this.shows(m)); const group = (label: string | undefined, buttons: [boolean, () => void, string][]) => html`
${buttons.map(([on, act, text]) => html``)}
`; return html`
${s.canGoBack ? html`` : nothing} ${this.shows("walls") ? group(undefined, [[s.walls === "auto", () => (this._walls = "auto"), t("walls_auto")], [s.walls === "cut", () => (this._walls = "cut"), t("walls_cut")], [s.walls === "slope", () => (this._walls = "slope"), t("walls_slope")]]) : nothing} ${this.shows("floors") && b.floors.length > 1 && !s.floorId ? group(undefined, [[s.explode, () => (this._explode = true), t("floors_apart")], [!s.explode, () => (this._explode = false), t("floors_stacked")]]) : nothing} ${heats.length > 0 ? group( t("heatmap"), (["none", ...heats] as HeatMode[]).map((m): [boolean, () => void, string] => [s.heat === m, () => (this._heat = m), t(m === "none" ? "heat_off" : (`heat_short_${m}` as Key))]), ) : nothing}
`; } /** One chip per room above the model; a tap opens the room (its floor first, when another floor shows), a second tap closes it. */ private renderRoomButtons(s: Stage): TemplateResult | typeof nothing { const b = this.plan(); if (!this._config?.room_buttons || !b || this._clean) return nothing; if(b.topology&&(!s.floorId||isOutdoorFloor(b.floors.find((f)=>f.id===s.floorId))))return nothing; const groups = roomButtons(b, s.floorId); if (!groups.length) return nothing; const several = groups.length > 1; const open = (floorId: string, roomId: string) => { if (this.canSwitch && floorId !== s.floorId) this._floorId = floorId; this._roomId = roomId === this._roomId ? null : roomId; }; // Outside: the house view with the garden's and the roof's devices in the panel const outside = !b.topology&&outsideEntities(this.hass, b.floors).length > 0; const openOutside = () => { if (this.canSwitch) this._floorId = null; this._roomId = this._roomId === OUTSIDE ? null : OUTSIDE; }; return html`
${groups.map( (g) => html`${several ? html`${g.floorName}` : nothing}${g.rooms.map( (r) => html``, )}`, )} ${outside ? html`` : nothing}
`; } /** The corner buttons: full screen, and a link to another dashboard. */ private renderCorners(): TemplateResult { const c = this._config; const t = this.t; const hidden = this._clean || (!!this._roomId && c?.room_panel !== false); return html`${c?.fullscreen_button && !hidden ? html`` : nothing} ${c?.dashboard && !hidden ? html`` : nothing}`; } private renderStage(s: Stage): TemplateResult { const b = this.plan()!; const c = this._config; const clean = () => { this._clean = !this._clean; if (this._clean) this.kiosk.stopClean(); else this.kiosk.armClean(); }; const eye = c?.controls_hidden !== undefined || (c?.controls_hide_after ?? 0) > 0; return html` ({ id: `card_${i}`, ...button })) ?? null} .heatMode=${s.heat} .theme=${lookNow(c?.theme, this.hass?.states["sun.sun"]?.state)} .clock=${c?.clock && c.clock !== "none" ? c.clock : null} .lockButton=${c?.lock_button !== false} .walkButton=${c?.walk_button !== false} .travelButton=${c?.time_travel !== false} .accent=${c?.accent ?? null} .furnitureColors=${c?.furniture_colors ?? null} .showEnergy=${c?.energy ?? true} .flows=${c?.flows ?? null} .holograms=${c?.holograms ?? null} .floorThumbs=${this.thumbs} .roomLabels=${this._roomNames ?? (c?.room_names !== false)} .roomNamesButton=${true} @room-names-change=${(e: CustomEvent<{ value: boolean }>) => (this._roomNames = e.detail.value)} .floorStack=${c?.floor_stack ?? "dim"} .panelOpen=${!!this._roomId && c?.room_panel !== false} .alerts=${c?.alerts !== false} .alertJump=${!!c?.alert_jump} .announcements=${!isSymcon(this.hass) && c?.announcements !== false && !screenMuted()} .sensorBar=${c?.sensor_bar !== false} .glassWalls=${typeof c?.glass_walls === "number" ? Math.min(100, Math.max(0, c.glass_walls)) : null} .screenName=${c?.screen_name ?? screenName()} .scenes=${c?.scenes !== false} .clean=${this._clean} .cleanButton=${eye} @clean-toggle=${clean} ?weather=${c?.weather !== false} .weatherEntityId=${c?.weather_entity ?? null} .weatherDensity=${c?.weather_density ?? 1} .dimmed=${this._night} .autoOrbit=${this._orbit} .startView=${c?.start_view ?? null} style=${s.showBar ? "--nf-bottom-inset: 52px" : ""} @room-tap=${(e: CustomEvent<{ floorId: string; roomId: string | null }>) => { // in the house view (or on another floor) a tap first opens the whole floor if (this.canSwitch && b.floors.length > 1 && e.detail.floorId && s.floorId !== e.detail.floorId) { this._floorId = e.detail.floorId; this._roomId = null; } else if (e.detail.roomId) this._roomId = e.detail.roomId === this._roomId ? null : e.detail.roomId; }} @topology-tap=${(e: CustomEvent)=>{if(this.canSwitch)this.openTopology(e.detail);}} @floor-tap=${(e: CustomEvent<{ floorId: string | null }>) => { this._floorId = e.detail.floorId; this._roomId = null;this._topologyGroup=ALL_TOPOLOGY; }} @back=${() => this.back()} >`; } private renderRoomPanel(): TemplateResult { const b = this.plan()!; return html` f.rooms).find((r) => r.id === this._roomId) ?? null} .floor=${b.floors.find((f) => f.rooms.some((r) => r.id === this._roomId)) ?? null} .outside=${this._roomId === OUTSIDE ? outsideEntities(this.hass, topologyOutsideFloors(b,this.topologySelection())) : null} .confirmEntities=${confirmEntities(this.hass, b.floors)} .phrases=${b.settings.announce_phrases ?? null} @close=${() => (this._roomId = null)} >`; } protected render() { // a language beyond German and English: its texts are fetched first, and then everything renders if (this.hass && awaitingLanguage(this.hass, this)) return nothing; const b = this.plan(); const c = this._config; // the configured room opens once, when the building is there; the kiosk return brings it back if (!this.roomApplied && b && c?.room) { this.roomApplied = true; if (b.floors.some((f) => f.rooms.some((r) => r.id === c.room))) this._roomId = c.room; } const s = this.stage(); // full screen, the screen below the dashboard header, or a fixed height const size = this._fullscreen ? "100vh" : c?.fill ? "calc(100vh - var(--header-height, 56px) - 16px)" : `${c?.height ?? 420}px`; const accent = /^#[0-9a-f]{6}$/i.test(c?.accent ?? "") ? c!.accent : null; const touch = () => this.kiosk.touched(); return html`
${b && b.floors.some((f) => f.rooms.length) ? this.renderStage(s) : html`

${this.data.error ?? (b ? translate(this.hass, "no_building") : translate(this.hass, "loading"))}

`} ${this._roomId && b && c?.room_panel !== false ? this.renderRoomPanel() : nothing} ${this.renderRoomButtons(s)} ${s.showBar && b ? this.renderBar(s) : nothing} ${this.renderCorners()}
`; } static styles = [tokens, controls, cardStyles, readableStyles]; } if (!customElements.get("nextfloor-card")) { customElements.define("nextfloor-card", NextFloorCard); const registry = window as Window & { customCards?: unknown[] }; registry.customCards = registry.customCards ?? []; registry.customCards.push({ type: "nextfloor-card", name: translate(undefined, "card_name"), description: translate(undefined, "card_description"), preview: false }); }