Add button for strych heating

master
Kuba Orlik 8 months ago
parent 86c8f583d8
commit 03569ea123

@ -12,3 +12,17 @@ export async function get_state(entity: string): Promise<HA_RESPONSE> {
).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;
}

@ -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<string, string>) => {
console.log({ inputs });
return {
...state,
elements: [...state.elements, inputs.element_to_add || "new element"],
};
activateStrychHeating: (state: State, inputs: Record<string, string>) => {
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<State, typeof actio
))}
</tbody>
</table>
{ctx.headers["just-data"]
? ""
: /* HTML */ `<script>
function refresh() {
let xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("GET", "/dashboard");
xhr.setRequestHeader("just-data", "true");
xhr.onload = function () {
document.querySelector("body").innerHTML =
xhr.response;
};
xhr.send();
}
setInterval(refresh, 15 * 1000);
</script>`}
{this.makeActionButton(state, {
action: "activateStrychHeating",
label: "Włącz-ogrzewanie-na-strychu-na-1h",
})}
</div>
);
}

Loading…
Cancel
Save