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

+84
View File
@@ -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;
}