You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
786 B
TypeScript
29 lines
786 B
TypeScript
import { HA_TOKEN, HA_URL } from "./config.js";
|
|
|
|
export type HA_RESPONSE = { state: string; attributes: Record<string, unknown> };
|
|
|
|
export async function get_state(entity: string): Promise<HA_RESPONSE> {
|
|
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;
|
|
}
|