fix: support Symcon numeric dimmers and independent blind slat controls
This commit is contained in:
1 parent
b934f78b9a
commit
24188f96f7
36 files changed
+1105
-683
No files matched your search
@@ -1,10 +1,23 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { entity, hassOf } from "./fixtures.ts";
|
||||
import { coverPosition, coverReaches } from "./covers.ts";
|
||||
import { box, entity, floorWith, hassOf, opening } from "./fixtures.ts";
|
||||
import { coverPosition, coverReaches, coverTiltEntity, coverWithTilt } from "./covers.ts";
|
||||
import { emptyBuilding, newFloor, type Furniture } from "../model.ts";
|
||||
import { setPacks, type PackItem } from "../packs.ts";
|
||||
|
||||
test("separate slat sources are resolved per blind and enrich only its control state", () => {
|
||||
const hass = hassOf([entity("cover.height", "open", { current_position: 40, supported_features: 4 }), entity("cover.slats", "open", { current_position: 75 })]);
|
||||
const floor = floorWith([box("r", 0, 0, 4, 4)], { openings: [opening("w", "r", "window", { cover: "cover.height", cover_tilt: "cover.slats" })] });
|
||||
assert.equal(coverTiltEntity(hass, [floor], "cover.height"), "cover.slats");
|
||||
const state = coverWithTilt(hass, hass.states["cover.height"], "cover.slats");
|
||||
assert.equal(state.attributes.current_position, 40);
|
||||
assert.equal(state.attributes.current_tilt_position, 75);
|
||||
assert.equal(state.attributes.supported_features, 132);
|
||||
assert.equal(hass.states["cover.height"].attributes.current_tilt_position, undefined, "one opening's mapping must not mutate a shared source");
|
||||
const placed = floorWith([], { placements: [{ entity_id: "cover.height", cover_tilt: "cover.slats", x: 1, z: 1, y: null }] });
|
||||
assert.equal(coverTiltEntity(hass, [placed], "cover.height"), "cover.slats", "standalone placed blinds also support a separate slat variable");
|
||||
});
|
||||
|
||||
test("a cover's position comes from its attribute, else from open / closed", () => {
|
||||
const hass = hassOf([
|
||||
entity("cover.a", "open", { current_position: 40 }),
|
||||
|
||||
@@ -1,9 +1,28 @@
|
||||
// Pack items with moving parts (curtains, roller screens, awnings, gates) and the cover entity they follow.
|
||||
|
||||
import type { Building } from "../model.ts";
|
||||
import type { Building, Floor } from "../model.ts";
|
||||
import { coverReach, hasMovingParts, packItem } from "../packs.ts";
|
||||
import type { HomeAssistant } from "../types.ts";
|
||||
import type { HassEntity, HomeAssistant } from "../types.ts";
|
||||
import { isUnavailable } from "./kinds.ts";
|
||||
import { openingEntities } from "./openings.ts";
|
||||
|
||||
/** The separately selected slat variable of either blind of an opening. */
|
||||
export function coverTiltEntity(hass: HomeAssistant, floors: readonly Floor[], id: string): string | null {
|
||||
for (const f of floors) for (const p of f.placements) if (p.entity_id === id && p.cover_tilt && p.cover_tilt !== "none") return p.cover_tilt;
|
||||
for (const e of openingEntities(hass, floors).values()) {
|
||||
if (e.cover === id && e.coverTilt) return e.coverTilt;
|
||||
if (e.cover2 === id && e.cover2Tilt) return e.cover2Tilt;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function coverWithTilt(hass: HomeAssistant, st: HassEntity, tilt: string | null | undefined): HassEntity {
|
||||
if (!tilt) return st;
|
||||
const a = hass.states[tilt]?.attributes;
|
||||
const position = a?.current_tilt_position ?? a?.current_position;
|
||||
if (typeof position !== "number") return st;
|
||||
return { ...st, attributes: { ...st.attributes, current_tilt_position: position, supported_features: Number(st.attributes.supported_features ?? 0) | 128 } };
|
||||
}
|
||||
|
||||
/** How far open a cover is in percent: its position, else 100 / 0 from open / closed (moving counts half). Null when unknown. */
|
||||
export function coverPosition(hass: HomeAssistant, id: string | null | undefined): number | null {
|
||||
|
||||
@@ -248,7 +248,7 @@ export function roomPanelEntities(hass: HomeAssistant, floor: Floor, room: Room)
|
||||
.filter((o) => o.room_id === room.id)
|
||||
.flatMap((o) => {
|
||||
const e = openingLinks.get(o.id);
|
||||
return e ? [e.cover, e.contact, e.tilt, e.contact2] : [];
|
||||
return e ? [e.cover, e.cover2, e.contact, e.tilt, e.contact2] : [];
|
||||
}),
|
||||
...(room.panel ?? []),
|
||||
];
|
||||
|
||||
@@ -184,3 +184,16 @@ test("a wide window's second blind and a venetian blind's slats", () => {
|
||||
assert.equal(state.slats, 0.3);
|
||||
assert.equal(openingState(hass, { cover: "cover.right", contact: null, tilt: null }).cover2, undefined, "no second blind");
|
||||
});
|
||||
|
||||
test("a separate integer slat variable changes the 3D slats independently of the blind height", () => {
|
||||
const hass = hassOf([
|
||||
entity("cover.height", "open", { current_position: 40 }),
|
||||
entity("cover.slats", "open", { current_position: 75 }),
|
||||
]);
|
||||
const state = openingState(hass, { cover: "cover.height", coverTilt: "cover.slats", contact: null, tilt: null });
|
||||
assert.equal(state.cover, 0.6);
|
||||
assert.equal(state.slats, 0.75);
|
||||
const double = openingState(hass, { cover: "cover.height", coverTilt: "cover.slats", cover2: "cover.slats", cover2Tilt: "cover.height", contact: null, tilt: null });
|
||||
assert.equal(double.slats, 0.75);
|
||||
assert.equal(double.slats2, 0.4);
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { roomEntities } from "./registry.ts";
|
||||
|
||||
export interface OpeningEntities {
|
||||
cover: string | null;
|
||||
coverTilt?: string | null;
|
||||
contact: string | null;
|
||||
tilt: string | null;
|
||||
/** Contact and tilt sensor of the second leaf of a double door or window. */
|
||||
@@ -28,6 +29,7 @@ export interface OpeningEntities {
|
||||
lock?: string | null;
|
||||
/** A second blind of a wide window (its right half, seen from the room). */
|
||||
cover2?: string | null;
|
||||
cover2Tilt?: string | null;
|
||||
}
|
||||
|
||||
const BLIND_CLASSES = new Set([undefined, "shutter", "blind", "awning", "shade", "curtain", "window"]);
|
||||
@@ -91,6 +93,8 @@ export function openingEntities(hass: HomeAssistant, floors: readonly Floor[]):
|
||||
angled: o.type === "door" && !!o.open_angle,
|
||||
lock: o.type === "door" ? unlessNone(o.lock) : null,
|
||||
cover2: o.type === "garage" ? null : unlessNone(o.cover2),
|
||||
coverTilt: unlessNone(o.cover_tilt),
|
||||
cover2Tilt: unlessNone(o.cover2_tilt),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -146,8 +150,9 @@ function coverClosed(hass: HomeAssistant, id: string | null | undefined): number
|
||||
}
|
||||
|
||||
/** 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): number | null {
|
||||
const tilt = id ? hass.states[id]?.attributes.current_tilt_position : undefined;
|
||||
function slatsOpen(hass: HomeAssistant, id: string | null | undefined, separate?: string | null): number | 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;
|
||||
}
|
||||
|
||||
@@ -185,6 +190,7 @@ export interface OpeningState {
|
||||
cover2?: number | null;
|
||||
/** Venetian blinds: how far the slats are open (0 shut, 1 level), from the cover's tilt position. */
|
||||
slats?: number | null;
|
||||
slats2?: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,12 +202,19 @@ 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 blindState = {
|
||||
...(e.cover2 ? { cover2: coverClosed(hass, e.cover2) } : {}),
|
||||
...(slats !== null ? { slats } : {}),
|
||||
...(slats2 !== null ? { slats2 } : {}),
|
||||
};
|
||||
|
||||
if (type === "door") {
|
||||
// a door with a roller shutter (front door, patio door): the blind comes down over it
|
||||
const leaf = positionOf(e.contact);
|
||||
const open = leaf === null ? (e.shut ? 0 : e.angled ? 1 : DOOR_DEFAULT_OPEN) : leaf === "closed" ? 0 : 1;
|
||||
return { open, open2: positionOf(e.contact2) === "open" ? 1 : 0, tilt: 0, tilt2: 0, cover, sensed: leaf !== null || cover !== null, unlocked: isUnlocked(hass, e.lock), assumed: leaf === null };
|
||||
return { open, open2: positionOf(e.contact2) === "open" ? 1 : 0, tilt: 0, tilt2: 0, cover, ...blindState, sensed: leaf !== null || cover !== null, unlocked: isUnlocked(hass, e.lock), assumed: leaf === null };
|
||||
}
|
||||
if (type === "garage") {
|
||||
// a garage door without a cover shows its contact: open or closed; so does one whose cover cannot tell (a remote without
|
||||
@@ -231,7 +244,6 @@ export function openingState(hass: HomeAssistant, e: OpeningEntities, type: Open
|
||||
tilt2: secondTilted ? 1 : 0,
|
||||
cover,
|
||||
sensed: isKnown(e.contact) || isKnown(e.tilt) || Number.isFinite(rawAngle),
|
||||
...(e.cover2 ? { cover2: coverClosed(hass, e.cover2) } : {}),
|
||||
...(slatsOpen(hass, e.cover) !== null ? { slats: slatsOpen(hass, e.cover) } : {}),
|
||||
...blindState,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user