import { HA_TOKEN, HA_URL } from "./config.js"; export type HA_RESPONSE = { state: string; attributes: Record }; export async function get_state(entity: string): Promise { const response = (await ( await fetch(`${HA_URL}/api/states/${entity}`, { headers: { Authorization: `Bearer ${HA_TOKEN}`, }, }) ).json()) as HA_RESPONSE; return response; } export async function call_service(domain: string, service: string, entity_id: string) { const response = (await ( await fetch(`${HA_URL}/api/services/${domain}/${service}`, { method: "POST", headers: { Authorization: `Bearer ${HA_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ entity_id }), }) ).json()) as HA_RESPONSE; return response; }