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; ).json()) as HA_RESPONSE;
return 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 { BaseContext } from "koa";
import { StatefulPage } from "@sealcode/sealgen"; import { StatefulPage } from "@sealcode/sealgen";
import html from "../html.js"; import html from "../html.js";
import { get_state, HA_RESPONSE } from "../ha_api.js"; import { call_service, get_state, HA_RESPONSE } from "../ha_api.js";
import { sleep } from "../util.js";
export const actionName = "Dashboard"; 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 = { const actions = {
add: (state: State, inputs: Record<string, string>) => { activateStrychHeating: (state: State, inputs: Record<string, string>) => {
console.log({ inputs }); call_service(
return { "automation",
...state, "trigger",
elements: [...state.elements, inputs.element_to_add || "new element"], "automation.force_heating_at_20c_in_strych_for_1h"
}; );
return state;
}, },
remove: (state: State, _: unknown, index_to_remove: number) => { remove: (state: State, _: unknown, index_to_remove: number) => {
return { return {
@ -78,25 +80,10 @@ export default new (class DashboardPage extends StatefulPage<State, typeof actio
))} ))}
</tbody> </tbody>
</table> </table>
{this.makeActionButton(state, {
{ctx.headers["just-data"] action: "activateStrychHeating",
? "" label: "Włącz-ogrzewanie-na-strychu-na-1h",
: /* 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>`}
</div> </div>
); );
} }

Loading…
Cancel
Save