-
Notifications
You must be signed in to change notification settings - Fork 3
/
AutomaticLOCAL.html
105 lines (85 loc) · 3.67 KB
/
AutomaticLOCAL.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
<!DOCTYPE html>
<html>
<body onload="arr()">
<script>
//Created with <3 by Nir K.
function arr() {
var hassaddress="http://"; //Your homeassistant address. Ex: http://192.168.1.10:8123
var hasspass=""; //Your homeassistant long lived token
//Build an array containing HASS device records.
var hassdevices = new Array();
var resp;
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
resp = JSON.parse(xmlHttp.responseText);
if (resp != null)
{
for (var t = 0; t < resp.length; t++)
{
var currid = resp[t]['entity_id'];
if (currid.indexOf("group.") == -1 && currid.indexOf("automation.") == -1 && currid.indexOf("persistent_notification.") == -1 && currid.indexOf("sensor.") == -1 && currid.indexOf("script.") == -1 && currid.indexOf("weather.") == -1 && currid.indexOf("updater.updater") == -1 && currid.indexOf("sun.") == -1)
{
var currname = resp[t]['attributes']['friendly_name'];
//SET YOUR MEDIA PLAYER HERE
if (currid.indexOf('media_player.spotify') != -1)
{
hassdevices.push("<p>"+currname+"<br><button id="+currid+" type=button onclick=homefunc(this.id,'media_play_pause')>Play</button><button id="+currid+" type=button onclick=homefunc(this.id,'media_play_pause')>Pause</button><br><button id="+currid+" type=button onclick=homefunc(this.id,'media_previous_track')>Previous</button><button id=" + currid+ " type=button onclick=homefunc(this.id,'media_next_track')>Next</button></p>");
}
else
{
hassdevices.push("<p>"+currname+"<br><button id="+currid+" type=button onclick=homefunc(this.id,'turn_on')>On</button> <button id=" + currid+ " type=button onclick=homefunc(this.id,'turn_off')>Off</button></p>");
}
}
}
//Create a HTML Table element.
var table = document.createElement("TABLE");
table.border = "1";
table.width = "99%";
//Get the count of columns.
var columnCount = 5;
var rowcount = Math.ceil(hassdevices.length/columnCount);
var pushcount = 0;
//Add the data rows.
for (var i = 0; i < columnCount; i++)
{
row = table.insertRow(-1);
for (var j = 0; j < rowcount; j++) {
var cell = row.insertCell(-1);
if (pushcount < hassdevices.length)
{
cell.innerHTML = hassdevices[pushcount];
pushcount = pushcount + 1;
//console.log(cell.innerHTML);
}
}
}
var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
}
}
}
xmlHttp.open("GET", hassaddress+"/api/states", true); // true for asynchronous
xmlHttp.setRequestHeader('Authorization', "Bearer "+hasspass);
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.send(null);
};
function homefunc(id, state) {
var hassaddress="http://"; //Your homeassistant address. Ex: http://192.168.1.10:8123
var hasspass=""; //Your homeassistant long lived token
if ((id != null) && (state != null))
{
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", hassaddress+"/api/services/"+id.substring(0,id.indexOf('.'))+"/"+state);
xmlhttp.setRequestHeader('Authorization', "Bearer "+hasspass);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify({entity_id:id}));
}
}
</script>
<div id="dvTable">
</div>
</body>
</html>