feat: improve Symcon controls, device readings and designer help

This commit is contained in:
jhartworks committed 2026-11-10 09:19:13 +01:00
1 parent ff54f4461a
commit 00fca890de
50 files changed
+2447 -1840

No files matched your search

+28 -16
View File
@@ -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 } : {}),
};