26 lines
1.2 KiB
TypeScript
26 lines
1.2 KiB
TypeScript
// A Symcon numeric variable can be explicitly used as a light or a blind, regardless of its inferred domain.
|
|
import type { HassEntity } from "./types.ts";
|
|
|
|
export function symconPercent(st: HassEntity): number | null {
|
|
const a = st.attributes;
|
|
if (typeof a.symcon_numeric_value !== "number") return null;
|
|
const min = typeof a.symcon_min === "number" ? a.symcon_min : 0;
|
|
const max = typeof a.symcon_max === "number" && a.symcon_max > min ? a.symcon_max : 100;
|
|
return Math.max(0, Math.min(100, (a.symcon_numeric_value - min) / (max - min) * 100));
|
|
}
|
|
|
|
export function symconControl(st: HassEntity, role: "light" | "cover"): HassEntity | null {
|
|
const percent = symconPercent(st);
|
|
const id = st.attributes.symcon_id;
|
|
if (percent === null || typeof id !== "number") return null;
|
|
return {
|
|
...st, entity_id: `${role}.${id}`,
|
|
state: role === "light" ? (percent > 0 ? "on" : "off") : (percent > 0 ? "open" : "closed"),
|
|
attributes: { ...st.attributes,
|
|
...(role === "light" ? { brightness: Math.round(percent * 2.55), color_mode: "brightness", supported_color_modes: ["brightness"] }
|
|
: { current_position: percent, supported_features: 4 }),
|
|
...(st.entity_id !== `${role}.${id}` ? { symcon_source_entity: st.entity_id } : {}),
|
|
},
|
|
};
|
|
}
|