feat: add floating power cards for all powered furniture
This commit is contained in:
1 parent
6bd3598779
commit
4967dd7950
19 files changed
+1125
-810
No files matched your search
@@ -22,6 +22,7 @@ import type { RoomClimateCard } from "./room-climate-card.ts";
|
||||
import { fromCelsius, tempUnit } from "../devices.ts";
|
||||
import { trendButton } from "../components/trend-button.ts";
|
||||
import { loggedEntities } from "../trend-data.ts";
|
||||
import type { FurniturePowerCard } from "./furniture-power-card.ts";
|
||||
|
||||
export interface HouseCard {
|
||||
kind: "house";
|
||||
@@ -64,7 +65,7 @@ export interface HeatPumpCard {
|
||||
pump: HeatPumpInfo;
|
||||
}
|
||||
|
||||
export type LiveCard = HouseCard | CarCard | MediaCard | PrinterCard | HeatPumpCard | RoomClimateCard;
|
||||
export type LiveCard = HouseCard | CarCard | MediaCard | PrinterCard | HeatPumpCard | RoomClimateCard | FurniturePowerCard;
|
||||
|
||||
/** 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;
|
||||
@@ -327,14 +328,26 @@ function roomClimateCard(hass: HomeAssistant | undefined, c: RoomClimateCard, i:
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function furniturePowerCard(hass: HomeAssistant | undefined, c: FurniturePowerCard, i: number): TemplateResult {
|
||||
const power = c.power === null ? "–" : `${c.power < 0 ? "−" : ""}${watts(hass, c.power)}`;
|
||||
return html`<div class="live-card live-card-power" data-i=${i} data-furniture=${c.id} role="group" aria-label=${c.name}>
|
||||
<div class="live-power-name">${c.name}</div><div class="live-row"><span aria-hidden="true">⚡</span><b>${power}</b>${trendButton(hass, [c.powerEntity], c.name)}</div>
|
||||
</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 === "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)))}
|
||||
${cards.map((c, i) => (c.kind === "roomclimate" ? roomClimateCard(hass, c, i) : c.kind === "power" ? furniturePowerCard(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>`;
|
||||
}
|
||||
|
||||
export const liveCardStyles = css`
|
||||
.live-card.live-card-power { display:grid; gap:4px; min-width:90px; max-width:220px; box-sizing:border-box; }
|
||||
.live-power-name { font-size:11px; color:var(--nf-muted); overflow:hidden; text-overflow:ellipsis; }
|
||||
.live-card-power .live-row { align-items:center; gap:6px; }
|
||||
.live-card-power .live-row b { margin-left:auto; color:color-mix(in srgb,#ffca8a 80%,var(--nf-text)); font-variant-numeric:tabular-nums; }
|
||||
.live-card-power nf-trend-button { margin-left:0; }
|
||||
.live-cards {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { box, entity, floorWith, hassOf } from "../entities/fixtures.ts";
|
||||
import { emptyBuilding, FURNITURE_TYPES, normalizeBuilding, type Furniture } from "../model.ts";
|
||||
import { furnitureEntities } from "../entities/furniture-links.ts";
|
||||
import { isElectric, setPacks } from "../packs.ts";
|
||||
import { furniturePowerCards } from "./furniture-power-card.ts";
|
||||
|
||||
const item = (id: string, extra: Partial<Furniture> = {}): Furniture => ({ id, type: "wallbox", x: 2, z: 2, w: .3, d: .3, h: .4, rotation: 0, variant: null, power: "cover.knx", power_card: true, ...extra });
|
||||
const native = (value: number, unit = "W") => entity("cover.knx", "open", { symcon_id: 123, symcon_variable_type: 2, symcon_numeric_value: value, unit_of_measurement: unit });
|
||||
const plan = (...items: Furniture[]) => ({ ...emptyBuilding(), floors: [floorWith([box("garage", 0, 0, 5, 5)], { furniture: items })] });
|
||||
|
||||
test("power-only native links work for every electric built-in and a pack wallbox with a status light", () => {
|
||||
setPacks([{ id: "test", name: "Test", publisher: "test", items: [{ id: "wallbox", name: { de: "Wallbox" }, size: [.3, .3, .4], light: "wall", parts: [] }] }]);
|
||||
try {
|
||||
const types = [...FURNITURE_TYPES.filter(isElectric), "pack:test:wallbox"];
|
||||
const b = plan(...types.map((type, n) => item(String(n), { type, entity: null })));
|
||||
const h = hassOf([native(11, "kW")]);
|
||||
const cards = furniturePowerCards(h, b, furnitureEntities(h, b.floors), null, null);
|
||||
assert.equal(cards.length, types.length);
|
||||
assert.ok(cards.every((c) => c.power === 11000 && c.powerEntity === "cover.knx"));
|
||||
assert.equal(cards.at(-1)?.name, "Wallbox");
|
||||
} finally { setPacks([]); }
|
||||
});
|
||||
|
||||
test("zero, export, unavailable and missing values retain their meaning and update live", () => {
|
||||
const b = plan(item("wallbox")), h = hassOf([native(0)]);
|
||||
const read = () => furniturePowerCards(h, b, new Map(), null, null)[0].power;
|
||||
assert.equal(read(), 0);
|
||||
h.states["cover.knx"] = native(-2.5, "kW");
|
||||
assert.equal(read(), -2500);
|
||||
h.states["cover.knx"] = entity("cover.knx", "unavailable", { symcon_id: 123, symcon_numeric_value: 1234 });
|
||||
assert.equal(read(), null);
|
||||
delete h.states["cover.knx"];
|
||||
assert.equal(read(), null);
|
||||
});
|
||||
|
||||
test("cards are opt-in, respect explicit none and use automatic links including the native source of aliases", () => {
|
||||
const b = plan(item("default", { power_card: undefined }), item("none", { power: "none" }), item("unlinked", { power: null }), item("auto", { power: null }), item("explicit"));
|
||||
const h = hassOf([native(250), entity("light.alias", "on", { symcon_source_entity: "cover.knx" })]);
|
||||
const links = new Map([ ["none", { entity: null, power: "cover.knx" }], ["auto", { entity: null, power: "light.alias" }] ]);
|
||||
assert.deepEqual(furniturePowerCards(h, b, links, null, null).map((c) => [c.id, c.powerEntity, c.power]), [["auto", "cover.knx", 250], ["explicit", "cover.knx", 250]]);
|
||||
});
|
||||
|
||||
test("floor, room and furniture visibility filter cards while anchors follow mounting and pin offsets", () => {
|
||||
const b = plan(item("garage", { name: "Meine Wallbox", mount_y: .9, pin_offset: { x: .1, y: .2, z: -.3 } }), item("outside", { x: 7 }), item("hidden", { show_when: { entity: "binary_sensor.visible" } }));
|
||||
b.floors.push(floorWith([box("office", 0, 0, 5, 5)], { id: "upper", furniture: [item("upper")] }));
|
||||
const h = hassOf([native(500), entity("binary_sensor.visible", "off")]);
|
||||
const read = (floor: string | null, room: string | null) => furniturePowerCards(h, b, new Map(), floor, room);
|
||||
assert.deepEqual(read(null, null).map((c) => c.id), ["garage", "outside", "upper"]);
|
||||
assert.deepEqual(read("upper", null).map((c) => c.id), ["upper"]);
|
||||
assert.deepEqual(read(null, "garage").map((c) => c.id), ["garage"]);
|
||||
assert.deepEqual(read("upper", "garage"), []);
|
||||
assert.equal(read(null, "garage")[0].name, "Meine Wallbox");
|
||||
const at = read(null, "garage")[0].at;
|
||||
assert.deepEqual([at.floorId, at.x, at.z], [b.floors[0].id, 2.1, 1.7]);
|
||||
assert.ok(Math.abs(at.y - 1.85) < 1e-10);
|
||||
});
|
||||
|
||||
test("the setting and links survive plan normalization and JSON persistence", () => {
|
||||
const b = normalizeBuilding(JSON.parse(JSON.stringify(plan(item("wallbox")))));
|
||||
assert.equal(b.floors[0].furniture[0].power_card, true);
|
||||
assert.equal(furniturePowerCards(hassOf([native(100)]), b, new Map(), null, null)[0].power, 100);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { mountBase } from "../packs.ts";
|
||||
import { pointInPolygon, type Building } from "../model.ts";
|
||||
import { furnitureName } from "../furniture-names.ts";
|
||||
import { furnitureShown } from "../entities/visibility.ts";
|
||||
import type { FurnitureLinks } from "../entities/furniture-links.ts";
|
||||
import type { HomeAssistant } from "../types.ts";
|
||||
import { readPower } from "../energy.ts";
|
||||
import type { PlanPoint } from "../viewer/live-energy.ts";
|
||||
|
||||
export interface FurniturePowerCard {
|
||||
kind: "power";
|
||||
id: string;
|
||||
name: string;
|
||||
powerEntity: string;
|
||||
power: number | null;
|
||||
at: PlanPoint;
|
||||
}
|
||||
|
||||
/** Power-only links are enough: never require a switch, a special furniture type, or a positive reading. */
|
||||
export function furniturePowerCards(hass: HomeAssistant, building: Building, links: ReadonlyMap<string, FurnitureLinks>, floorId: string | null, roomId: string | null): FurniturePowerCard[] {
|
||||
const cards: FurniturePowerCard[] = [];
|
||||
for (const floor of building.floors) {
|
||||
if (floorId && floor.id !== floorId) continue;
|
||||
const room = roomId ? floor.rooms.find((r) => r.id === roomId) : null;
|
||||
if (roomId && !room) continue;
|
||||
for (const item of floor.furniture) {
|
||||
if (!item.power_card || item.power === "none" || !furnitureShown(hass, item)) continue;
|
||||
if (room && !pointInPolygon([item.x, item.z], room.points)) continue;
|
||||
const source = item.power || links.get(item.id)?.power;
|
||||
if (!source || source === "none") continue;
|
||||
const powerEntity = String(hass.states[source]?.attributes.symcon_source_entity || source);
|
||||
const offset = item.pin_offset;
|
||||
cards.push({ kind: "power", id: item.id, name: item.name || furnitureName(hass, item.type), powerEntity,
|
||||
power: readPower(hass.states[powerEntity]), at: { floorId: floor.id, x: item.x + (offset?.x ?? 0), z: item.z + (offset?.z ?? 0), y: mountBase(floor, item) + item.h + .35 + (offset?.y ?? 0) } });
|
||||
}
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
Reference in new issue
Block a user