fix: restrict blind direction reversal to 3D appearance

This commit is contained in:
jhartworks committed 2026-11-10 10:31:41 +01:00
1 parent b9a095bc6e
commit 7a9b2c0a38
19 files changed
+721 -629

No files matched your search

+6 -18
View File
@@ -1,20 +1,16 @@
// Present a plan's cover wiring as one consistent logical direction to every view and command.
// Combine live cover feedback and separate slat controls without changing values or command direction.
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;
@@ -24,11 +20,11 @@ export function withCoverControls(hass: HomeAssistant, floors: readonly Floor[])
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 });
bindings.set(p.entity_id, { tilt: p.cover_tilt === "none" ? null : p.cover_tilt, position: p.position });
}
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 });
if (e.cover && !bindings.has(e.cover)) bindings.set(e.cover, { tilt: e.coverTilt, position: e.position });
if (e.cover2 && !bindings.has(e.cover2)) bindings.set(e.cover2, { tilt: e.cover2Tilt });
}
const states = { ...raw.states };
for (const [id, b] of bindings) {
@@ -36,18 +32,15 @@ export function withCoverControls(hass: HomeAssistant, floors: readonly Floor[])
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;
if (position !== null) a.current_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;
const 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;
}
@@ -62,11 +55,6 @@ export function withCoverControls(hass: HomeAssistant, floors: readonly Floor[])
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") {