feat: improve Symcon controls, device readings and designer help
This commit is contained in:
1 parent
ff54f4461a
commit
00fca890de
50 files changed
+2447
-1840
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { box, entity, floorWith, hassOf, opening } from "./fixtures.ts";
|
||||
import { openingEntities, openingState } from "./openings.ts";
|
||||
import { isPositionVariable, positionPercent } from "./position-value.ts";
|
||||
import { withCoverControls } from "./cover-controls.ts";
|
||||
|
||||
test("Symcon position feedback accepts numeric actuator domains and respects native integer/float ranges", () => {
|
||||
const h = hassOf([
|
||||
entity("cover.feedback", "open", { symcon_id: 1, symcon_variable_type: 1, symcon_numeric_value: 128, symcon_min: 0, symcon_max: 255 }),
|
||||
entity("light.fraction", "on", { symcon_id: 2, symcon_variable_type: 2, symcon_numeric_value: 0.4, symcon_min: 0, symcon_max: 1 }),
|
||||
entity("cover.one", "open", { symcon_id: 3, symcon_variable_type: 1, symcon_numeric_value: 1, symcon_min: 0, symcon_max: 100 }),
|
||||
entity("sensor.text", "50", { symcon_id: 4, symcon_variable_type: 3 }),
|
||||
]);
|
||||
assert.equal(isPositionVariable(h, "cover.feedback"), true);
|
||||
assert.equal(isPositionVariable(h, "light.fraction"), true);
|
||||
assert.equal(isPositionVariable(h, "sensor.text"), false);
|
||||
assert.equal(positionPercent(h, "cover.feedback"), 128 / 255 * 100);
|
||||
assert.equal(positionPercent(h, "light.fraction"), 40);
|
||||
assert.equal(positionPercent(h, "cover.one"), 1, "integer 1 is one percent, not a normalized float at full travel");
|
||||
});
|
||||
|
||||
test("blind and slat inversions act independently on feedback, 3D, sliders, buttons and grouped commands", async () => {
|
||||
for (const inverted of [false, true]) for (const tiltInverted of [false, true]) {
|
||||
const calls: unknown[] = [];
|
||||
const raw = hassOf([entity("cover.height", "open", { current_position: 5 }), entity("cover.slats", "open", { current_position: 30, symcon_id: 9 }), entity("cover.feedback", "open", { symcon_numeric_value: 40, symcon_min: 0, symcon_max: 100 }), entity("cover.normal", "open", { current_position: 90 })], {}, { callService: async (domain: string, service: string, data: unknown) => { calls.push([domain, service, data]); } });
|
||||
const floor = floorWith([box("r", 0, 0, 4, 4)], { openings: [opening("w", "r", "window", { cover: "cover.height", cover_tilt: "cover.slats", position: "cover.feedback", cover_inverted: inverted, cover_tilt_inverted: tiltInverted })] });
|
||||
const view = withCoverControls(raw, [floor]);
|
||||
assert.equal(view.states["cover.height"].attributes.current_position, inverted ? 60 : 40);
|
||||
assert.equal(view.states["cover.height"].attributes.current_tilt_position, tiltInverted ? 70 : 30);
|
||||
const links = openingEntities(raw, [floor]).get("w")!;
|
||||
const fromRaw = openingState(raw, links), fromView = openingState(view, links);
|
||||
assert.equal(fromRaw.cover, fromView.cover, "3D feedback cannot be inverted twice");
|
||||
assert.equal(fromRaw.slats, fromView.slats);
|
||||
await view.callService("cover", "set_cover_position", { entity_id: "cover.height", position: 75 });
|
||||
await view.callService("cover", "set_cover_tilt_position", { entity_id: "cover.height", tilt_position: 20 });
|
||||
await view.callService("cover", "open_cover", { entity_id: ["cover.height", "cover.normal"] });
|
||||
assert.deepEqual(calls, [
|
||||
["cover", "set_cover_position", { entity_id: "cover.height", position: inverted ? 25 : 75 }],
|
||||
["cover", "set_cover_tilt_position", { entity_id: "cover.slats", tilt_position: tiltInverted ? 80 : 20 }],
|
||||
["cover", inverted ? "close_cover" : "open_cover", { entity_id: "cover.height" }],
|
||||
["cover", "open_cover", { entity_id: "cover.normal" }],
|
||||
]);
|
||||
assert.equal(raw.states["cover.height"].attributes.current_position, 5, "the native state remains unchanged");
|
||||
}
|
||||
});
|
||||
|
||||
test("standalone covers, feedback-only inversion, facade reuse and changed settings remain consistent", async () => {
|
||||
const raw = hassOf([entity("cover.height", "closed", { current_position: 0, current_tilt_position: 20 }), entity("sensor.feedback", "25", { unit_of_measurement: "%" })]);
|
||||
const floor = floorWith([], { placements: [{ entity_id: "cover.height", x: 1, z: 1, y: null, position: "sensor.feedback", position_inverted: true, cover_tilt_inverted: true }] });
|
||||
const floors = [floor];
|
||||
const first = withCoverControls(raw, floors);
|
||||
assert.equal(first.states["cover.height"].attributes.current_position, 75);
|
||||
assert.equal(first.states["cover.height"].attributes.current_tilt_position, 80);
|
||||
assert.equal(withCoverControls(first, floors), first, "nested views cannot invert an already adapted facade again");
|
||||
const next = withCoverControls(first, [{ ...floor, placements: [{ ...floor.placements[0], cover_inverted: true }] }]);
|
||||
assert.equal(next.states["cover.height"].attributes.current_position, 25);
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
// Present a plan's cover wiring as one consistent logical direction to every view and command.
|
||||
import type { Floor } from "../model.ts";
|
||||
import type { HassEntity, HomeAssistant } from "../types.ts";
|
||||
import { openingEntities } from "./openings.ts";
|
||||
import { positionPercent } from "./position-value.ts";
|
||||
|
||||
interface Binding {
|
||||
inverted?: boolean;
|
||||
tiltInverted?: boolean;
|
||||
tilt?: string | null;
|
||||
position?: string | null;
|
||||
positionInverted?: boolean;
|
||||
}
|
||||
|
||||
const cache = new WeakMap<HomeAssistant, WeakMap<readonly Floor[], HomeAssistant>>();
|
||||
const sources = new WeakMap<HomeAssistant, HomeAssistant>();
|
||||
const swapped: Record<string, string> = { open_cover: "close_cover", close_cover: "open_cover", open_cover_tilt: "close_cover_tilt", close_cover_tilt: "open_cover_tilt" };
|
||||
|
||||
export function withCoverControls(hass: HomeAssistant, floors: readonly Floor[]): HomeAssistant {
|
||||
const raw = sources.get(hass) ?? hass;
|
||||
const memo = cache.get(raw) ?? new WeakMap<readonly Floor[], HomeAssistant>();
|
||||
cache.set(raw, memo);
|
||||
const existing = memo.get(floors);
|
||||
if (existing) return existing;
|
||||
const bindings = new Map<string, Binding>();
|
||||
for (const f of floors) for (const p of f.placements) if (p.entity_id.startsWith("cover.")) {
|
||||
bindings.set(p.entity_id, { inverted: p.cover_inverted, tiltInverted: p.cover_tilt_inverted, tilt: p.cover_tilt === "none" ? null : p.cover_tilt, position: p.position, positionInverted: p.position_inverted });
|
||||
}
|
||||
for (const e of openingEntities(raw, floors).values()) {
|
||||
if (e.cover && !bindings.has(e.cover)) bindings.set(e.cover, { inverted: e.coverInverted, tiltInverted: e.coverTiltInverted, tilt: e.coverTilt, position: e.position, positionInverted: e.positionInverted });
|
||||
if (e.cover2 && !bindings.has(e.cover2)) bindings.set(e.cover2, { inverted: e.cover2Inverted, tiltInverted: e.cover2TiltInverted, tilt: e.cover2Tilt });
|
||||
}
|
||||
const states = { ...raw.states };
|
||||
for (const [id, b] of bindings) {
|
||||
const st = raw.states[id];
|
||||
if (!st) continue;
|
||||
const a: Record<string, unknown> = { ...st.attributes, nf_cover_control: true };
|
||||
let position = positionPercent(raw, b.position);
|
||||
if (position !== null && b.positionInverted) position = 100 - position;
|
||||
if (position === null) position = typeof st.attributes.current_position === "number" ? st.attributes.current_position : null;
|
||||
if (position !== null) a.current_position = b.inverted ? 100 - position : position;
|
||||
const tiltState = b.tilt ? raw.states[b.tilt] : st;
|
||||
let tilt = typeof tiltState?.attributes.current_tilt_position === "number" ? tiltState.attributes.current_tilt_position : b.tilt ? positionPercent(raw, b.tilt) : null;
|
||||
if (tilt !== null) {
|
||||
if (b.tiltInverted) tilt = 100 - tilt;
|
||||
a.current_tilt_position = tilt;
|
||||
a.supported_features = Number(a.supported_features ?? 0) | 128;
|
||||
}
|
||||
let state = st.state;
|
||||
if (b.inverted) state = ({ open: "closed", closed: "open", opening: "closing", closing: "opening" } as Record<string, string>)[state] ?? state;
|
||||
if (typeof a.current_position === "number" && state !== "unavailable" && state !== "unknown" && state !== "opening" && state !== "closing") state = a.current_position > 0 ? "open" : "closed";
|
||||
states[id] = { ...st, state, attributes: a } satisfies HassEntity;
|
||||
}
|
||||
const result: HomeAssistant = { ...raw, states, callService: async (domain, service, data = {}) => {
|
||||
if (domain !== "cover") return raw.callService(domain, service, data);
|
||||
const ids = Array.isArray(data.entity_id) ? data.entity_id : [data.entity_id];
|
||||
const send = async (id: unknown) => {
|
||||
const b = typeof id === "string" ? bindings.get(id) : undefined;
|
||||
if (!b) return raw.callService(domain, service, { ...data, entity_id: id });
|
||||
const payload: Record<string, unknown> = { ...data, entity_id: id };
|
||||
let command = service;
|
||||
if (command === "toggle" || command === "toggle_cover") command = states[String(id)]?.state === "closed" ? "open_cover" : "close_cover";
|
||||
const tilt = command.includes("tilt");
|
||||
if (tilt && b.tilt) payload.entity_id = b.tilt;
|
||||
if (tilt ? b.tiltInverted : b.inverted) {
|
||||
command = swapped[command] ?? command;
|
||||
const key = tilt ? "tilt_position" : "position";
|
||||
if (typeof payload[key] === "number") payload[key] = 100 - payload[key];
|
||||
}
|
||||
// A separate HA position-only cover controls its slats through its position service.
|
||||
const target = b.tilt ? raw.states[b.tilt] : undefined;
|
||||
if (tilt && b.tilt && !target?.attributes.symcon_id && typeof target?.attributes.current_tilt_position !== "number") {
|
||||
if (command === "set_cover_tilt_position") { command = "set_cover_position"; payload.position = payload.tilt_position; delete payload.tilt_position; }
|
||||
else if (command === "open_cover_tilt") command = "open_cover";
|
||||
else if (command === "close_cover_tilt") command = "close_cover";
|
||||
}
|
||||
return raw.callService(domain, command, payload);
|
||||
};
|
||||
return Array.isArray(data.entity_id) ? Promise.all(ids.map(send)) : send(ids[0]);
|
||||
} };
|
||||
sources.set(result, raw);
|
||||
memo.set(floors, result);
|
||||
return result;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ export function coverTiltEntity(hass: HomeAssistant, floors: readonly Floor[], i
|
||||
}
|
||||
|
||||
export function coverWithTilt(hass: HomeAssistant, st: HassEntity, tilt: string | null | undefined): HassEntity {
|
||||
if (st.attributes.nf_cover_control) return st;
|
||||
if (!tilt) return st;
|
||||
const a = hass.states[tilt]?.attributes;
|
||||
const position = a?.current_tilt_position ?? a?.current_position;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { entity, hassOf } from "./fixtures.ts";
|
||||
import { deviceValueReadouts, deviceValuesText } from "./device-values.ts";
|
||||
|
||||
test("several temperatures and other device values keep independent symbols, labels and native readings", () => {
|
||||
const h = hassOf([
|
||||
entity("cover.temp", "open", { symcon_numeric_value: 22.7, unit_of_measurement: "°C", device_class: "temperature" }),
|
||||
entity("sensor.out", "48.5", { unit_of_measurement: "°C", friendly_name: "Ausgang", device_class: "temperature" }),
|
||||
entity("sensor.humidity", "55", { unit_of_measurement: "%", device_class: "humidity" }),
|
||||
entity("sensor.status", "normal", {}),
|
||||
]);
|
||||
const links = [{ id: "a", entity: "cover.temp", icon: "❄", label: "Innen" }, { id: "b", entity: "sensor.out", icon: "🔥" }, { id: "c", entity: "sensor.humidity" }, { id: "d", entity: "sensor.status", icon: "✓" }];
|
||||
const rows = deviceValueReadouts(h, links);
|
||||
assert.deepEqual(rows.map((v) => v.icon), ["❄", "🔥", "💧", "✓"]);
|
||||
assert.equal(rows[0].text, "22.7 °C", "an inferred actuator domain cannot replace the numeric reading with an on/off state");
|
||||
assert.equal(rows[1].label, "Ausgang");
|
||||
assert.equal(rows[3].text, "normal");
|
||||
assert.match(deviceValuesText(h, links), /❄ Innen: 22.7 °C.*🔥 48.5 °C.*💧 55 %/);
|
||||
assert.equal(deviceValueReadouts(h, [links[1]])[0].id, "b", "removing one link keeps the others intact");
|
||||
});
|
||||
|
||||
test("unassigned values stay hidden and linked unavailable readings retain a dash", () => {
|
||||
const h = hassOf([entity("sensor.offline", "unavailable"), entity("sensor.native", "on", { symcon_formatted: "Ein" })]);
|
||||
const rows = deviceValueReadouts(h, [{ id: "empty", entity: null }, { id: "none", entity: "none" }, { id: "offline", entity: "sensor.offline" }, { id: "native", entity: "sensor.native" }]);
|
||||
assert.deepEqual(rows.map((v) => v.text), ["–", "Ein"]);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { DeviceValueLink } from "../model.ts";
|
||||
import type { HassEntity, HomeAssistant } from "../types.ts";
|
||||
import { formatNumber } from "../i18n.ts";
|
||||
import { entityName, isUnavailable } from "./kinds.ts";
|
||||
|
||||
export function valueSymbol(st: HassEntity | undefined): string {
|
||||
const a = st?.attributes;
|
||||
return ({ temperature: "🌡", humidity: "💧", carbon_dioxide: "☁", illuminance: "☀", power: "⚡", energy: "⚡" } as Record<string, string>)[String(a?.device_class)] ?? (a?.unit_of_measurement === "°C" ? "🌡" : "◉");
|
||||
}
|
||||
|
||||
export function deviceValueReadouts(hass: HomeAssistant, links: readonly DeviceValueLink[] = []): { id: string; entity: string; icon: string; label: string; text: string }[] {
|
||||
return links.flatMap((link) => {
|
||||
if (!link.entity || link.entity === "none") return [];
|
||||
const st = hass.states[link.entity];
|
||||
let text = "–";
|
||||
if (st && !isUnavailable(st)) {
|
||||
const a = st.attributes;
|
||||
if (typeof a.symcon_formatted === "string" && a.symcon_formatted.trim()) text = a.symcon_formatted;
|
||||
else {
|
||||
const raw = a.symcon_numeric_value ?? st.state;
|
||||
const n = typeof raw === "number" || (typeof raw === "string" && raw.trim()) ? Number(raw) : NaN;
|
||||
text = Number.isFinite(n) ? `${formatNumber(hass, n, hass.entities?.[link.entity]?.display_precision ?? (a.symcon_variable_type === 1 ? 0 : 1))}${a.unit_of_measurement ? ` ${a.unit_of_measurement}` : ""}` : String(raw ?? "–");
|
||||
}
|
||||
}
|
||||
return [{ id: link.id, entity: link.entity, icon: link.icon?.trim() || valueSymbol(st), label: link.label?.trim() || entityName(hass, link.entity), text }];
|
||||
});
|
||||
}
|
||||
|
||||
export function deviceValuesText(hass: HomeAssistant, links: readonly DeviceValueLink[] = []): string {
|
||||
return deviceValueReadouts(hass, links).map((v) => `${v.icon} ${v.label && links.find((l) => l.id === v.id)?.label ? `${v.label}: ` : ""}${v.text}`).join(" · ");
|
||||
}
|
||||
@@ -4,10 +4,13 @@ import type { Floor, Opening } from "../model.ts";
|
||||
import type { HassEntity, HomeAssistant } from "../types.ts";
|
||||
import { isUnavailable, kindOf } from "./kinds.ts";
|
||||
import { roomEntities } from "./registry.ts";
|
||||
import { positionPercent } from "./position-value.ts";
|
||||
|
||||
export interface OpeningEntities {
|
||||
cover: string | null;
|
||||
coverTilt?: string | null;
|
||||
coverInverted?: boolean;
|
||||
coverTiltInverted?: boolean;
|
||||
contact: string | null;
|
||||
tilt: string | null;
|
||||
/** Contact and tilt sensor of the second leaf of a double door or window. */
|
||||
@@ -30,6 +33,8 @@ export interface OpeningEntities {
|
||||
/** A second blind of a wide window (its right half, seen from the room). */
|
||||
cover2?: string | null;
|
||||
cover2Tilt?: string | null;
|
||||
cover2Inverted?: boolean;
|
||||
cover2TiltInverted?: boolean;
|
||||
}
|
||||
|
||||
const BLIND_CLASSES = new Set([undefined, "shutter", "blind", "awning", "shade", "curtain", "window"]);
|
||||
@@ -95,6 +100,10 @@ export function openingEntities(hass: HomeAssistant, floors: readonly Floor[]):
|
||||
cover2: o.type === "garage" ? null : unlessNone(o.cover2),
|
||||
coverTilt: unlessNone(o.cover_tilt),
|
||||
cover2Tilt: unlessNone(o.cover2_tilt),
|
||||
coverInverted: !!o.cover_inverted,
|
||||
coverTiltInverted: !!o.cover_tilt_inverted,
|
||||
cover2Inverted: !!o.cover2_inverted,
|
||||
cover2TiltInverted: !!o.cover2_tilt_inverted,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -131,39 +140,42 @@ export const DOOR_DEFAULT_OPEN = 0.5;
|
||||
|
||||
/** How far a blind is open according to a position sensor: a percentage or a 0-1 fraction. Null when unknown. */
|
||||
function reportedPosition(hass: HomeAssistant, id: string | null | undefined): number | null {
|
||||
const st = id ? hass.states[id] : undefined;
|
||||
const value = st && !isUnavailable(st) ? Number(st.state) : NaN;
|
||||
if (!Number.isFinite(value)) return null;
|
||||
const isPercent = st!.attributes.unit_of_measurement === "%" || value > 1;
|
||||
return Math.min(1, Math.max(0, isPercent ? value / 100 : value));
|
||||
const value = positionPercent(hass, id);
|
||||
return value === null ? null : value / 100;
|
||||
}
|
||||
|
||||
/** How closed the blind is (0 open, 1 closed) from the position sensor, else from the cover entity. Null without cover data. */
|
||||
/** How far a cover entity is closed (0 up, 1 down), or null without one. */
|
||||
function coverClosed(hass: HomeAssistant, id: string | null | undefined): number | null {
|
||||
function coverClosed(hass: HomeAssistant, id: string | null | undefined, inverted = false): number | null {
|
||||
const cover = id ? hass.states[id] : undefined;
|
||||
if (!cover || isUnavailable(cover)) return id ? 0 : null;
|
||||
const percent = cover.attributes.current_position;
|
||||
if (typeof percent === "number") return 1 - Math.min(100, Math.max(0, percent)) / 100;
|
||||
if (cover.state === "closed") return 1;
|
||||
if (typeof percent === "number") { const closed = 1 - Math.min(100, Math.max(0, percent)) / 100; return inverted && !cover.attributes.nf_cover_control ? 1 - closed : closed; }
|
||||
if (cover.state === "closed") return inverted && !cover.attributes.nf_cover_control ? 0 : 1;
|
||||
if (cover.state === "open") return inverted && !cover.attributes.nf_cover_control ? 1 : 0;
|
||||
return cover.state === "opening" || cover.state === "closing" ? 0.5 : 0;
|
||||
}
|
||||
|
||||
/** How far the slats of a venetian blind are open (0 shut, 1 level), from its tilt position; null when it reports none. */
|
||||
function slatsOpen(hass: HomeAssistant, id: string | null | undefined, separate?: string | null): number | null {
|
||||
function slatsOpen(hass: HomeAssistant, id: string | null | undefined, separate?: string | null, inverted = false): number | null {
|
||||
const main = hass.states[id || ""];
|
||||
if (main?.attributes.nf_cover_control) return typeof main.attributes.current_tilt_position === "number" ? main.attributes.current_tilt_position / 100 : null;
|
||||
const a = hass.states[separate || id || ""]?.attributes;
|
||||
const tilt = a?.current_tilt_position ?? (separate ? a?.current_position : undefined);
|
||||
return typeof tilt === "number" ? Math.min(100, Math.max(0, tilt)) / 100 : null;
|
||||
const value = typeof tilt === "number" ? Math.min(100, Math.max(0, tilt)) : separate ? positionPercent(hass, separate) : null;
|
||||
return value === null ? null : (inverted ? 100 - value : value) / 100;
|
||||
}
|
||||
|
||||
function blindClosedFraction(hass: HomeAssistant, e: OpeningEntities): number | null {
|
||||
if (hass.states[e.cover || ""]?.attributes.nf_cover_control) return coverClosed(hass, e.cover);
|
||||
const reported = reportedPosition(hass, e.position);
|
||||
if (reported !== null) return e.positionInverted ? reported : 1 - reported;
|
||||
if (reported !== null) { const closed = e.positionInverted ? reported : 1 - reported; return e.coverInverted ? 1 - closed : closed; }
|
||||
const cover = e.cover ? hass.states[e.cover] : undefined;
|
||||
if (cover && !isUnavailable(cover)) {
|
||||
const percent = cover.attributes.current_position;
|
||||
if (typeof percent === "number") return 1 - Math.min(100, Math.max(0, percent)) / 100;
|
||||
if (cover.state === "closed") return 1;
|
||||
if (typeof percent === "number") return coverClosed(hass, e.cover, e.coverInverted);
|
||||
if (cover.state === "closed") return e.coverInverted ? 0 : 1;
|
||||
if (cover.state === "open") return e.coverInverted ? 1 : 0;
|
||||
return cover.state === "opening" || cover.state === "closing" ? 0.5 : 0;
|
||||
}
|
||||
return e.cover ? 0 : null;
|
||||
@@ -202,10 +214,10 @@ export function openingState(hass: HomeAssistant, e: OpeningEntities, type: Open
|
||||
const isKnown = (id: string | null | undefined) => !!id && !!hass.states[id] && !isUnavailable(hass.states[id]);
|
||||
const positionOf = (id: string | null | undefined) => (id ? windowPosition(hass.states[id]) : null);
|
||||
const cover = blindClosedFraction(hass, e);
|
||||
const slats = slatsOpen(hass, e.cover, e.coverTilt);
|
||||
const slats2 = slatsOpen(hass, e.cover2, e.cover2Tilt);
|
||||
const slats = slatsOpen(hass, e.cover, e.coverTilt, e.coverTiltInverted);
|
||||
const slats2 = slatsOpen(hass, e.cover2, e.cover2Tilt, e.cover2TiltInverted);
|
||||
const blindState = {
|
||||
...(e.cover2 ? { cover2: coverClosed(hass, e.cover2) } : {}),
|
||||
...(e.cover2 ? { cover2: coverClosed(hass, e.cover2, e.cover2Inverted) } : {}),
|
||||
...(slats !== null ? { slats } : {}),
|
||||
...(slats2 !== null ? { slats2 } : {}),
|
||||
};
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { HomeAssistant } from "../types.ts";
|
||||
import { symconPercent } from "../symcon-controls.ts";
|
||||
import { isUnavailable } from "./kinds.ts";
|
||||
|
||||
/** A feedback variable can be numeric even when its inferred entity domain is an actuator. */
|
||||
export function isPositionVariable(hass: HomeAssistant | undefined, id: string): boolean {
|
||||
const st = hass?.states[id];
|
||||
if (!st) return false;
|
||||
const a = st.attributes;
|
||||
if (typeof a.symcon_id === "number") return a.symcon_variable_type === 1 || a.symcon_variable_type === 2;
|
||||
return typeof a.current_position === "number" || (/^(sensor|number|input_number)\./.test(id) && !!st.state.trim() && Number.isFinite(Number(st.state)));
|
||||
}
|
||||
|
||||
/** Percent open, using native Symcon ranges before considering plain 0–100 or 0–1 sensors. */
|
||||
export function positionPercent(hass: HomeAssistant, id: string | null | undefined): number | null {
|
||||
const st = id ? hass.states[id] : undefined;
|
||||
if (!st || isUnavailable(st)) return null;
|
||||
const a = st.attributes;
|
||||
if (a.nf_cover_control && typeof a.current_position === "number") return a.current_position;
|
||||
const native = symconPercent(st);
|
||||
if (native !== null) return native;
|
||||
if (typeof a.current_position === "number") return Math.max(0, Math.min(100, a.current_position));
|
||||
if (!st.state.trim()) return null;
|
||||
const value = Number(st.state);
|
||||
if (!Number.isFinite(value)) return null;
|
||||
const percent = a.unit_of_measurement === "%" || value > 1 || value < 0 ? value : value * 100;
|
||||
return Math.max(0, Math.min(100, percent));
|
||||
}
|
||||
@@ -206,7 +206,7 @@ export const primaryEntities = (hass: HomeAssistant, ids: readonly string[]): st
|
||||
// ---------------------------------------------------------------- room climate
|
||||
|
||||
/** Device classes of the three room values. */
|
||||
export const CLIMATE_CLASSES = { temperature: "temperature", humidity: "humidity", co2: "carbon_dioxide" } as const;
|
||||
export const CLIMATE_CLASSES = { temperature: "temperature", humidity: "humidity", co2: "carbon_dioxide", illuminance: "illuminance" } as const;
|
||||
export type ClimateKey = keyof typeof CLIMATE_CLASSES;
|
||||
|
||||
/** Numeric room values, including Symcon readings whose legacy domain was inferred from a parent name. */
|
||||
@@ -222,6 +222,7 @@ export function isClimateValueSensor(hass: HomeAssistant, id: string, key: Clima
|
||||
if (key === "temperature" && ["°c", "°f", "℃", "℉", "k"].includes(unit)) return true;
|
||||
if (key === "humidity" && !a.device_class && ["%", "%rh", "%r.h.", "%rf", "%r.f."].includes(unit)) return true;
|
||||
if (key === "co2" && unit === "ppm" && /co[₂2]/i.test(String(a.friendly_name ?? ""))) return true;
|
||||
if (key === "illuminance" && ["lx", "lux"].includes(unit)) return true;
|
||||
return a.device_class === CLIMATE_CLASSES[key];
|
||||
}
|
||||
|
||||
@@ -277,7 +278,7 @@ export function roomClimateValue(hass: HomeAssistant, floor: Floor | null, room:
|
||||
if (readings.length) return readings.reduce((sum, v) => sum + v, 0) / readings.length;
|
||||
if (room.climate?.[key]) return null;
|
||||
// no room sensor: a thermostat of the room's area tells its temperature and humidity (heating curves, the heat map)
|
||||
if (key === "co2" || room.climate?.[key] === "none") return null;
|
||||
if ((key !== "temperature" && key !== "humidity") || room.climate?.[key] === "none") return null;
|
||||
const attr = key === "temperature" ? "current_temperature" : "current_humidity";
|
||||
const climates = roomEntities(hass, room)
|
||||
.filter((id) => id.startsWith("climate."))
|
||||
|
||||
Reference in new issue
Block a user