feat: add floating room climate cards and fix Symcon humidity selection

This commit is contained in:
jhartworks committed 2026-11-09 17:04:46 +01:00
1 parent 24188f96f7
commit ff54f4461a
21 files changed
+764 -398

No files matched your search

+31 -3
View File
@@ -18,6 +18,8 @@ import { curvePath } from "./day-curves.ts";
import type { PrinterInfo } from "./printer.ts";
import type { HeatPumpInfo } from "./heat-pump.ts";
import type { PlanPoint } from "../viewer/live-energy.ts";
import type { RoomClimateCard } from "./room-climate-card.ts";
import { fromCelsius, tempUnit } from "../devices.ts";
export interface HouseCard {
kind: "house";
@@ -60,7 +62,7 @@ export interface HeatPumpCard {
pump: HeatPumpInfo;
}
export type LiveCard = HouseCard | CarCard | MediaCard | PrinterCard | HeatPumpCard;
export type LiveCard = HouseCard | CarCard | MediaCard | PrinterCard | HeatPumpCard | RoomClimateCard;
/** A button on a card was pressed: the 3D view calls the service (with extra data such as the volume). */
export type LiveCardAction = (domain: string, service: string, entity: string, data?: Record<string, unknown>) => void;
@@ -312,10 +314,21 @@ function heatPumpCard(hass: HomeAssistant | undefined, c: HeatPumpCard, i: numbe
</div>`;
}
export function renderLhCards(hass: HomeAssistant | undefined, cards: readonly LiveCard[], act: LiveCardAction, fold?: LiveCardFold): TemplateResult | typeof nothing {
function roomClimateCard(hass: HomeAssistant | undefined, c: RoomClimateCard, i: number): TemplateResult {
const value = (n: number | null, digits: number, unit: string) => n === null ? "–" : `${formatNumber(hass, n, digits)} ${unit}`;
return html`<div class="live-card live-card-roomclimate" data-i=${i} data-room=${c.roomId}>
<div class="live-title">${c.name || translate(hass, "room")}</div>
<div class="live-row live-climate-temperature"><span>🌡 ${translate(hass, "climate_temperature")}</span><b>${value(c.temperature === null ? null : hass ? fromCelsius(hass, c.temperature) : c.temperature, 1, hass ? tempUnit(hass) : "°C")}</b></div>
<div class="live-row live-climate-humidity"><span>💧 ${translate(hass, "climate_humidity")}</span><b>${value(c.humidity, 0, "%")}</b></div>
<div class="live-row live-climate-co2"><span>${translate(hass, "climate_co2")}</span><b>${value(c.co2, 0, "ppm")}</b></div>
${c.temperature === null && c.humidity === null && c.co2 === null ? html`<small class="live-status">${translate(hass, "climate_card_missing")}</small>` : nothing}
</div>`;
}
export function renderLhCards(hass: HomeAssistant | undefined, cards: readonly LiveCard[], act: LiveCardAction, fold?: LiveCardFold, climateOnly = false): TemplateResult | typeof nothing {
if (!cards.length) return nothing;
return html`<div class="live-cards">
${cards.map((c, i) => (c.kind === "house" ? houseCard(hass, c, i, fold) : c.kind === "car" ? carCard(hass, c, i, act) : c.kind === "printer" ? printerCard(hass, c, i) : c.kind === "heatpump" ? heatPumpCard(hass, c, i) : mediaCard(hass, c, i, act)))}
${cards.map((c, i) => (c.kind === "roomclimate" ? roomClimateCard(hass, c, i) : climateOnly ? nothing : c.kind === "house" ? houseCard(hass, c, i, fold) : c.kind === "car" ? carCard(hass, c, i, act) : c.kind === "printer" ? printerCard(hass, c, i) : c.kind === "heatpump" ? heatPumpCard(hass, c, i) : mediaCard(hass, c, i, act)))}
</div>`;
}
@@ -418,6 +431,21 @@ export const liveCardStyles = css`
gap: 2px;
min-width: 150px;
}
.live-card-roomclimate {
display: grid;
gap: 4px;
min-width: 190px;
max-width: calc(100vw - 24px);
box-sizing: border-box;
}
.live-card-roomclimate .live-title {
overflow: hidden;
text-overflow: ellipsis;
max-width: 220px;
}
.live-climate-temperature b { color: color-mix(in srgb, #ffca8a 80%, var(--nf-text)); }
.live-climate-humidity b { color: color-mix(in srgb, #7dd3fc 80%, var(--nf-text)); }
.live-climate-co2 b { color: color-mix(in srgb, #c4b5fd 80%, var(--nf-text)); }
.live-row {
display: flex;
justify-content: space-between;
@@ -0,0 +1,43 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { box, entity, floorWith, hassOf } from "../entities/fixtures.ts";
import { emptyBuilding, normalizeBuilding, pointInPolygon } from "../model.ts";
import { roomClimateCards } from "./room-climate-card.ts";
test("enabled room cards use explicit numeric sensors, averages and temperature conversion", () => {
const b = emptyBuilding();
const room = { ...box("living", 0, 0, 4, 4), name: "Wohnzimmer", climate_card: true, climate: { temperature: ["sensor.c", "sensor.f"], humidity: "cover.knx", co2: "sensor.co2" } };
b.floors = [floorWith([room, box("disabled", 5, 0, 8, 4)])];
const h = hassOf([
entity("sensor.c", "20", { unit_of_measurement: "°C" }), entity("sensor.f", "68", { unit_of_measurement: "°F" }),
entity("cover.knx", "open", { symcon_numeric_value: 48.5 }), entity("sensor.co2", "780"),
]);
const cards = roomClimateCards(h, normalizeBuilding(b), null, null);
assert.equal(cards.length, 1);
assert.deepEqual([cards[0].temperature, cards[0].humidity, cards[0].co2], [20, 48.5, 780]);
assert.equal(cards[0].name, "Wohnzimmer");
assert.ok(pointInPolygon([cards[0].at.x, cards[0].at.z], room.points));
h.states["cover.knx"] = entity("cover.knx", "open", { symcon_numeric_value: 55 });
assert.equal(roomClimateCards(h, b, null, null)[0].humidity, 55, "a fresh state updates the card values");
});
test("floor/room navigation filters cards and concave rooms keep their anchor inside", () => {
const b = emptyBuilding();
const room = { ...box("u", 0, 0, 6, 6), climate_card: true, points: [[0, 0], [6, 0], [6, 6], [4, 6], [4, 2], [2, 2], [2, 6], [0, 6]] as [number, number][] };
const upper = { ...floorWith([{ ...box("upper", 0, 0, 4, 4), climate_card: true }]), id: "upper" };
b.floors = [floorWith([room]), upper];
const h = hassOf([]);
const all = roomClimateCards(h, b, null, null);
assert.equal(all.length, 2);
assert.ok(pointInPolygon([all[0].at.x, all[0].at.z], room.points));
assert.deepEqual(roomClimateCards(h, b, "upper", null).map((c) => c.roomId), ["upper"]);
assert.deepEqual(roomClimateCards(h, b, null, "u").map((c) => c.roomId), ["u"]);
});
test("missing, unavailable and disabled readings stay missing rather than becoming zero or another thermostat", () => {
const b = emptyBuilding();
b.floors = [floorWith([{ ...box("r", 0, 0, 4, 4, "a"), climate_card: true, climate: { temperature: "sensor.offline", humidity: "sensor.empty", co2: "none" } }])];
const h = hassOf([entity("sensor.offline", "unavailable"), entity("sensor.empty", ""), entity("climate.other", "heat", { current_temperature: 30 })], { "climate.other": { area_id: "a" } });
const [card] = roomClimateCards(h, b, null, null);
assert.deepEqual([card.temperature, card.humidity, card.co2], [null, null, null]);
});
+35
View File
@@ -0,0 +1,35 @@
import { roomClimateValue } from "../devices.ts";
import { centroid, pointInPolygon, spotGrid, type Building } from "../model.ts";
import type { HomeAssistant } from "../types.ts";
import type { PlanPoint } from "../viewer/live-energy.ts";
export interface RoomClimateCard {
kind: "roomclimate";
at: PlanPoint;
roomId: string;
name: string;
/** Celsius internally, converted to the display unit when rendered. */
temperature: number | null;
humidity: number | null;
co2: number | null;
}
/** One opt-in card per room, using the exact same sensor assignments and averaging as the room panel. */
export function roomClimateCards(hass: HomeAssistant, building: Building, floorId: string | null, roomId: string | null): RoomClimateCard[] {
const cards: RoomClimateCard[] = [];
for (const floor of building.floors) {
if (floorId && floor.id !== floorId) continue;
for (const room of floor.rooms) {
if (!room.climate_card || room.points.length < 3 || (roomId && room.id !== roomId)) continue;
const middle = centroid(room.points);
const [x, z] = pointInPolygon(middle, room.points) ? middle : spotGrid(room, 9, 9).sort((a, b) => Math.hypot(a[0] - middle[0], a[1] - middle[1]) - Math.hypot(b[0] - middle[0], b[1] - middle[1]))[0] ?? room.points[0];
cards.push({ kind: "roomclimate", roomId: room.id, name: room.name,
at: { floorId: floor.id, x, z, y: (room.ceiling ?? floor.height) + 0.4 },
temperature: roomClimateValue(hass, floor, room, "temperature"),
humidity: roomClimateValue(hass, floor, room, "humidity"),
co2: roomClimateValue(hass, floor, room, "co2"),
});
}
}
return cards;
}