-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHOMEASSISTANT1.plugin.txt
More file actions
35 lines (33 loc) · 1.79 KB
/
HOMEASSISTANT1.plugin.txt
File metadata and controls
35 lines (33 loc) · 1.79 KB
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
///////// <version>1.0.2</version> /////////
///////// HOMEASSISTANT1 /////////
///////// Plugin to extract Home Assistant Solar data for Toon /////////
///////// By Bonno /////////
//////// v1.0.2 changed http to https
function getSolarData(password, username, apiKey, siteId, urlString, totalPower) {
if (debugOutput) console.log("*********SolarPanel Start getHomeAssistantData")
var url = "https://" + urlString + "/api/states/sensor.solar_panel"
var http = new XMLHttpRequest();
if (debugOutput) console.log("*********SolarPanel HA url " + url)
http.open("GET", url, true)
http.setRequestHeader("Authorization", "Bearer " + apiKey);
http.onreadystatechange = function() {
if (http.readyState == XMLHttpRequest.DONE) {
if (http.status === 200 || http.status === 300 || http.status === 302) {
try {
var sensorResult = http.responseText
var sensorObject = JSON.parse(sensorResult)
var currentPower = parseInt(sensorObject.attributes.current)
var todayPower = parseInt(sensorObject.attributes.today)
var totalPower = parseFloat(sensorObject.attributes.total)
parseReturnData(currentPower, totalPower, todayPower, 0, 0, 0, 0, http.status, "succes")
} catch (e) {
parseReturnData(0, totalPower, 0, 0, 0, 0, 0, http.status, "error")
}
} else {
if (debugOutput) console.log("*********SolarPanel error: " + http.status)
parseReturnData(0, totalPower, 0, 0, 0, 0, 0, http.status, "error")
}
}
}
http.send();
}