diff --git a/src/back/ha_api.ts b/src/back/ha_api.ts index e6fcd08..3fcb0cd 100644 --- a/src/back/ha_api.ts +++ b/src/back/ha_api.ts @@ -12,3 +12,17 @@ export async function get_state(entity: string): Promise { ).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; +} diff --git a/src/back/routes/dashboard.sreact.tsx b/src/back/routes/dashboard.sreact.tsx index 76e8096..7552d51 100644 --- a/src/back/routes/dashboard.sreact.tsx +++ b/src/back/routes/dashboard.sreact.tsx @@ -2,18 +2,20 @@ import { TempstreamJSX, Templatable } from "tempstream"; import { BaseContext } from "koa"; import { StatefulPage } from "@sealcode/sealgen"; import html from "../html.js"; -import { get_state, HA_RESPONSE } from "../ha_api.js"; -import { sleep } from "../util.js"; +import { call_service, get_state, HA_RESPONSE } from "../ha_api.js"; export const actionName = "Dashboard"; +//{"type":"call_service","domain":"automation","service":"trigger","service_data":{"entity_id":"automation.force_heating_at_20c_in_strych_for_1h","skip_condition":true},"id":41} + const actions = { - add: (state: State, inputs: Record) => { - console.log({ inputs }); - return { - ...state, - elements: [...state.elements, inputs.element_to_add || "new element"], - }; + activateStrychHeating: (state: State, inputs: Record) => { + call_service( + "automation", + "trigger", + "automation.force_heating_at_20c_in_strych_for_1h" + ); + return state; }, remove: (state: State, _: unknown, index_to_remove: number) => { return { @@ -78,25 +80,10 @@ export default new (class DashboardPage extends StatefulPage - - {ctx.headers["just-data"] - ? "" - : /* HTML */ ``} + {this.makeActionButton(state, { + action: "activateStrychHeating", + label: "Włącz-ogrzewanie-na-strychu-na-1h", + })} ); }