-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathde.sebastian-kaps.HABatteryStatus.js
112 lines (100 loc) · 3.31 KB
/
de.sebastian-kaps.HABatteryStatus.js
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
/*!
@author Sebastian Kaps
@title de.sebastian-kaps.HABatteryStatus
@version 0.2
*/
// access parameters
var ha_host = CF.widget("HA_HOST_IP", "PARAMETERS", "HA_MOD").label;
var ha_port = CF.widget("HA_PORT", "PARAMETERS", "HA_MOD").label;
var ha_token = CF.widget("HA_TOKEN", "PARAMETERS", "HA_MOD").label;
var sysModel = System.getModel();
var sysSerial = System.getSerial();
var entityString = sysModel + "_" + String(sysSerial);
if (entityString === "-_-") {
entityString = "TSUSIM_0000000000";
sysModel = "TSUSIM";
sysSerial = "0000000000";
}
var updateInterval = 1800 * 1000; // 30min; in msec
function getDate() {
d = new Date();
return d.getFullYear()
+ "-" + ("0" + Number(d.getMonth()+1)).slice(-2)
+ "-" + ("0" + d.getDate()).slice(-2)
+ "T" + GUI.getDisplayTime() + ":" + ("0" + d.getSeconds()).slice(-2);
}
function haSetStateAttribute(entityId, state, attribute, value) {
var request;
request = new com.philips.HttpLibrary.HttpRequest();
request.open("POST", "http://" + ha_host + ":" + ha_port + "/api/states/" + entityId, true);
request.setRequestHeader("Authorization", "Bearer " + ha_token);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Connection", "Close");
request.send('{"state": "' + state + '", "attributes": {"' + attribute + '": ' + '"' + value + '", "last_update": "' + getDate() + '", "icon": "mdi:tablet", "friendly_name": "Pronto ' + sysModel + '", "serial": "' + String(sysSerial) + '"}}');
}
function updateHA() {
var batLvl;
switch (System.getBatteryStatus()) {
case "empty":
batLvl = "0";
break;
case "critical":
batLvl = "5";
break;
case "level1":
batLvl = "25";
break;
case "level2":
batLvl = "50";
break;
case "level3":
batLvl = "75";
break;
case "level4":
batLvl = "95";
break;
case "max":
batLvl = "100";
break;
case "charging":
batLvl = "charging";
break;
default:
batLvl = "unknown";
break;
}
haSetStateAttribute("device_tracker." + entityString, "home", "battery", batLvl);
}
/*
schedule periodic updates for when the TSU sits in the charging dock
and it is powered on.
*/
function periodicUpdate(activity) {
// System.setDebugMask(9);
// System.print(getDate() + " [ScheduleAfter]");
updateHA();
activity.scheduleAfter(updateInterval, arguments.callee, activity);
}
((function () {
// debugging
// System.setDebugMask(9);
// the current activity.
var myActivity = CF.activity();
myActivity.onWake = function () {
last = System.getGlobal('ha_last_update');
ts = Date.now();
if (last === null || (ts - last > 10 * 60 * 1000)) {
// System.print(getDate() + " [Wake up]");
updateHA();
System.setGlobal('ha_last_update', ts);
}
};
// System.print("Setting up scheduled updates:");
myActivity.scheduleAfter(updateInterval, periodicUpdate, myActivity);
// System.print("Done.");
myActivity.onExit = function () {
var A = CF.activity();
A.onWake = null;
A.onExit = null;
};
}()));