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