73 lines
4.0 KiB
TypeScript
73 lines
4.0 KiB
TypeScript
// 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 {
|
|
tilt?: string | null;
|
|
position?: string | null;
|
|
}
|
|
|
|
const cache = new WeakMap<HomeAssistant, WeakMap<readonly Floor[], HomeAssistant>>();
|
|
const sources = new WeakMap<HomeAssistant, HomeAssistant>();
|
|
|
|
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, { 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, { 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) {
|
|
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) position = typeof st.attributes.current_position === "number" ? st.attributes.current_position : null;
|
|
if (position !== null) a.current_position = position;
|
|
const tiltState = b.tilt ? raw.states[b.tilt] : st;
|
|
const tilt = typeof tiltState?.attributes.current_tilt_position === "number" ? tiltState.attributes.current_tilt_position : b.tilt ? positionPercent(raw, b.tilt) : null;
|
|
if (tilt !== null) {
|
|
a.current_tilt_position = tilt;
|
|
a.supported_features = Number(a.supported_features ?? 0) | 128;
|
|
}
|
|
let state = st.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;
|
|
// 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;
|
|
}
|