Debian Codegraph @

index.html

raw
1 <!doctype html>
2 <html>
3 <!-- To try locally, run: `yarn build` and then `npx http-server -o` -->
4
5 <body>
6 <button onclick="connection.reconnect()">Reconnect</button>
7 <button onclick="setupEntitiesSubscription()">Re-subscribe</button>
8 <table>
9 <tbody></tbody>
10 </table>
11 <script type="module">
12 import {
13 getAuth,
14 getUser,
15 callService,
16 createConnection,
17 subscribeEntities,
18 ERR_HASS_HOST_REQUIRED,
19 } from "./dist/index.js";
20
21 (async () => {
22 let auth;
23 const storeAuth = true;
24 const authOptions = storeAuth
25 ? {
26 async loadTokens() {
27 try {
28 return JSON.parse(localStorage.hassTokens);
29 } catch (err) {
30 return undefined;
31 }
32 },
33 saveTokens: (tokens) => {
34 localStorage.hassTokens = JSON.stringify(tokens);
35 },
36 }
37 : {};
38 try {
39 auth = await getAuth(authOptions);
40 } catch (err) {
41 if (err === ERR_HASS_HOST_REQUIRED) {
42 authOptions.hassUrl = prompt(
43 "What host to connect to?",
44 "http://localhost:8123",
45 );
46 if (!authOptions.hassUrl) return;
47 auth = await getAuth(authOptions);
48 } else {
49 alert(`Unknown error: ${err}`);
50 return;
51 }
52 }
53 const connection = await createConnection({ auth });
54 for (const ev of ["disconnected", "ready", "reconnect-error"]) {
55 connection.addEventListener(ev, () => console.log(`Event: ${ev}`));
56 }
57
58 // Clear url if we have been able to establish a connection
59 if (location.search.includes("auth_callback=1")) {
60 history.replaceState(null, "", location.pathname);
61 }
62
63 // To play from the console
64 window.auth = auth;
65 window.connection = connection;
66 getUser(connection).then((user) => {
67 console.log("Logged in as", user);
68 window.user = user;
69 });
70
71 setupEntitiesSubscription();
72 })();
73
74 let unsubEntities;
75
76 window.setupEntitiesSubscription = async () => {
77 if (unsubEntities) {
78 unsubEntities();
79 console.log("Sleeping");
80 await new Promise((resolve) => setTimeout(resolve, 4000));
81 }
82 unsubEntities = subscribeEntities(connection, (entities) =>
83 renderEntities(connection, entities),
84 );
85 };
86
87 function renderEntities(connection, entities) {
88 window.entities = entities;
89 const root = document.querySelector("tbody");
90 while (root.lastChild) root.removeChild(root.lastChild);
91
92 Object.keys(entities)
93 .sort()
94 .forEach((entId) => {
95 const tr = document.createElement("tr");
96
97 const tdName = document.createElement("td");
98 tdName.innerHTML = entId;
99 tr.appendChild(tdName);
100
101 const tdState = document.createElement("td");
102 const text = document.createTextNode(entities[entId].state);
103 tdState.appendChild(text);
104
105 if (
106 ["switch", "light", "input_boolean"].includes(
107 entId.split(".", 1)[0],
108 )
109 ) {
110 const button = document.createElement("button");
111 button.innerHTML = "toggle";
112 button.onclick = () =>
113 callService(connection, "homeassistant", "toggle", {
114 entity_id: entId,
115 });
116 tdState.appendChild(button);
117 }
118 tr.appendChild(tdState);
119
120 root.appendChild(tr);
121 });
122 }
123 </script>
124 </body>
125 </html>