-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (44 loc) · 2.25 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>HomeAssistant JS Example</title>
</head>
<body>
<div class="container">
<h1>HomeAssistant JS Example</h1>
<h2>Get state of an entity</h2>
<p>Get the state of an entity, in this case the <code>sun.sun</code> entity.</p>
<p><button class="btn btn-primary" id="get-state">Get Sun state</button></p>
<p>After pressing the button, the current state should show below:</p>
<input id="state" type="text">
<h2>Get attribute of an entity</h2>
<p>Get a specific attribute of an entity, in this case the friendly name of the <code>sun.sun</code> entity.</p>
<p><button class="btn btn-primary" id="get-name">Get friendly name of entity</button></p>
<input id="friendly_name" type="text">
<h2>Toggle a light/input boolean/switch</h2>
<p>Toggle an entity that has toggle support, this includes lights, switches and input booleans. There is no state returned.</p>
<p><button class="btn btn-primary" id="toggle">Toggle entity</button></p>
<script src="secrets.js"></script>
<script src="js/ha.js"></script>
<script type="text/javascript">
const ha = new HaAPI({ instance: HA_INSTANCE, token: HA_SECRET });
const buttonA = document.getElementById('get-state');
buttonA.addEventListener('click', function() {
ha.getState('sun.sun').then(s => document.getElementById('state').value = s);
});
const buttonB = document.getElementById("get-name");
buttonB.addEventListener('click', function() {
ha.getAttribute('sun.sun', 'friendly_name').then(s => document.getElementById('friendly_name').value = s);
});
const buttonC = document.getElementById("toggle");
buttonC.addEventListener('click', function() {
ha.toggle('light.light_1');
});
</script>
</div>
</body>
</html>