Skip to content

Commit cb4647f

Browse files
authored
Create lockscreen-homeassistant-temp
1 parent 12b70f7 commit cb4647f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

source/lockscreen-homeassistant-temp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
.headers = { "Authorization": "Bearer YOUR KEY", "content-type": "application/json" }
2+
let json = await req.loadJSON();
3+
4+
/* Parse data received from API */
5+
let data = {livingroom: {}}
6+
7+
data.livingroom = addData(json, data.livingroom, ['sensor.living_room_temperature', 'sensor.living_room_humidity']);
8+
9+
/* Create the widget */
10+
const widget = new ListWidget();
11+
widget.backgroundColor = new Color("#03a9f4", 1.0);
12+
13+
/* Add the sensor entries */
14+
const bodyStack = widget.addStack();
15+
16+
/* First, the label column */
17+
const labelStack = bodyStack.addStack();
18+
labelStack.centerAlignContent();
19+
labelStack.setPadding(0, 0, 0, 0);
20+
labelStack.borderWidth = 0;
21+
labelStack.size = new Size(50,50);
22+
addLabel(labelStack, "🛋️")
23+
24+
/* Second, the temperature column */
25+
const tempStack = bodyStack.addStack();
26+
tempStack.centerAlignContent();
27+
tempStack.setPadding(0, 11, 0, 0);
28+
tempStack.borderWidth = 0;
29+
tempStack.size = new Size(0,50);
30+
tempStack.layoutVertically();
31+
32+
tempStack.addText(" 🌡️")
33+
addTemp(tempStack, data.livingroom)
34+
35+
/* Third, the humidity column */
36+
const humidStack = bodyStack.addStack();
37+
humidStack.centerAlignContent();
38+
humidStack.setPadding(0, 5, 0, 0);
39+
humidStack.borderWidth = 0;
40+
humidStack.size = new Size(0,50);
41+
humidStack.layoutVertically();
42+
43+
humidStack.addText("💧")
44+
addHumid(humidStack, data.livingroom)
45+
46+
/* Done: Widget is now ready to be displayed */
47+
return widget;
48+
}
49+
50+
/* Adds the entries to the label column */
51+
async function addLabel(labelStack, label) {
52+
const mytext = labelStack.addText(label);
53+
mytext.font = Font.semiboldSystemFont(40);
54+
mytext.textColor = Color.black();
55+
}
56+
57+
/* Adds the entries to the temperature column */
58+
async function addTemp(tempStack, data) {
59+
const mytext = tempStack.addText(data.temp + "°C");
60+
mytext.font = Font.heavyMonospacedSystemFont(13);
61+
mytext.textColor = Color.white();
62+
}
63+
64+
/* Adds the entries to the humidity column */
65+
async function addHumid(humidStack, data) {
66+
const mytext = humidStack.addText(data.humid + "%");
67+
mytext.font = Font.mediumMonospacedSystemFont(13);
68+
mytext.textColor = Color.white();
69+
mytext.textOpacity = 0.8;
70+
}
71+
72+
/* Searches for the respective sensor values ('state') in the API response of Home Assistant */
73+
function addData(json, room, sensors) {
74+
room.temp = "N/A";
75+
room.humid = "N/A";
76+
var i;
77+
for (i = 0; i < json.length; i++) {
78+
if (json[i]['entity_id'] == sensors[0]) {
79+
room.temp = Number(json[i]['state']);
80+
room.temp = room.temp.toFixed(1);
81+
}
82+
if (json[i]['entity_id'] == sensors[1]) {
83+
room.humid = Math.round(json[i]['state']);
84+
}
85+
}
86+
return room;
87+
}

0 commit comments

Comments
 (0)