50 lines
2.5 KiB
TypeScript
50 lines
2.5 KiB
TypeScript
// Pack items with moving parts (curtains, roller screens, awnings, gates) and the cover entity they follow.
|
|
|
|
import type { Building, Floor } from "../model.ts";
|
|
import { coverReach, hasMovingParts, packItem } from "../packs.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 (st.attributes.nf_cover_control) return st;
|
|
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 {
|
|
const st = id ? hass.states[id] : undefined;
|
|
if (!st || isUnavailable(st)) return null;
|
|
const position = st.attributes.current_position;
|
|
if (typeof position === "number") return Math.min(100, Math.max(0, position));
|
|
if (st.state === "open") return 100;
|
|
if (st.state === "closed") return 0;
|
|
return st.state === "opening" || st.state === "closing" ? 50 : null;
|
|
}
|
|
|
|
/** How far out the moving parts of every such item are (0 … 1, by furniture id). */
|
|
export function coverReaches(hass: HomeAssistant, b: Building): Map<string, number> {
|
|
const out = new Map<string, number>();
|
|
for (const f of b.floors.flatMap((floor) => floor.furniture)) {
|
|
const item = packItem(f.type);
|
|
if (!item || !hasMovingParts(item)) continue;
|
|
const position = f.cover_entity ? coverPosition(hass, f.cover_entity) : null;
|
|
out.set(f.id, coverReach(item, position !== null && f.cover_inverted ? 100 - position : position));
|
|
}
|
|
return out;
|
|
}
|