-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetatmoSIAQM.js
330 lines (290 loc) · 10.6 KB
/
NetatmoSIAQM.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
NetatmoSIAQM for Scriptable (not affiliated with Netatmo in any way)
https://github.com/fredrikdev/NetatmoSIAQM
Scriptable Widget for the Netatmo Smart Indoor Air Quality Monitor (usually interfaced with by the iOS app Netatmo Home Coach) using the Netatmo Aircare API.
1) Login to your Netatmo Account at https://dev.netatmo.com/, and create a new app: https://dev.netatmo.com/apps/createanapp (yes, unfortunately you'll need to do this, but it's pretty swift)
2) Fill in the mandatory fields, save, and continue by setting the Redirect URI field to "https://noop". Save again, and take note of the Client Id (aaa) and Client Secret (bbb) fields.
3a) Using your browser, construct and browse to the URL: https://api.netatmo.com/oauth2/authorize?client_id=aaa&scope=read_homecoach&redirect_uri=https://noop
3b) ...After authenticating with Netatmo, you'll be redirected to a non-existing URL (e.g. https://noop/?code=ccc). Copy the value (ccc) from the &code= parameter.
4) Create a new script in the iOS Scriptable app, add a name, color, glyph, and paste this script.
5) Edit the script below and set the 'params' variable with your own Client Id, Client Secret and value from the &code= parameter.
6) On your home screen, add the new Scriptable Widget as a (medium sized preferred) widget with the script.
Provided for free, MIT, as-is, by fredrikdev 2022. Inspired by https://github.com/olf/scriptable-netatmo-widget
*/
// parameters -- modify this & keep it private
let params = `
client_id:aaa
client_secret:bbb
code:ccc
`
function param(name) {
try {
return new RegExp(`${name}:([^\n\r\t ]+)`, "g").exec(params)[1]
} catch (ex) {
throw new Error(`Param "${name}" not found`)
}
}
let d = null, dd = null, ex = null
if (params == "") {
d = JSON.parse(`{"devices":[{"dashboard_data":{"Humidity":61,"Temperature":25.7,"max_temp":25.7,"Noise":48,"health_idx":0,"Pressure":1017.1,"date_min_temp":1656372621,"min_temp":23.4,"time_utc":1656398309,"CO2":446,"AbsolutePressure":1012.2,"date_max_temp":1656398309},"station_name":"Please setup the script!","firmware":59,"reachable":true}]}`)
d = d.devices[0]
dd = d.dashboard_data
} else {
try {
let client_id = param("client_id")
let client_secret = param("client_secret")
let code = param("code")
if (client_id == "" || client_secret == "" || code == "")
throw new Error("Missing configuration parameters")
let key_id = client_id + "-" + code + "-refresh_token"
let refresh_token = Keychain.contains(key_id) ? Keychain.get(key_id) : ""
let access_token = ""
if (refresh_token == "") {
// get initial tokens
let req1 = new Request("https://api.netatmo.net/oauth2/token")
req1.method = "POST"
req1.addParameterToMultipart("grant_type", "authorization_code")
req1.addParameterToMultipart("redirect_uri", "https://noop")
req1.addParameterToMultipart("scope", "read_homecoach")
req1.addParameterToMultipart("client_id", client_id)
req1.addParameterToMultipart("client_secret", client_secret)
req1.addParameterToMultipart("code", code)
let res1 = await req1.loadJSON()
if (!res1.access_token || !res1.refresh_token)
throw new Error("Authentication error (1)")
// save refresh_token for next run
Keychain.set(key_id, res1.refresh_token)
access_token = res1.access_token
} else {
// use refresh_token to get new access_token
let req1 = new Request("https://api.netatmo.net/oauth2/token")
req1.method = "POST"
req1.addParameterToMultipart("grant_type", "refresh_token")
req1.addParameterToMultipart("redirect_uri", "https://noop")
req1.addParameterToMultipart("client_id", client_id)
req1.addParameterToMultipart("client_secret", client_secret)
req1.addParameterToMultipart("refresh_token", refresh_token)
let res1 = await req1.loadJSON()
if (!res1.access_token || !res1.refresh_token)
throw new Error("Authentication error (2)")
// save refresh_token for next run (this usually doesn't change)
Keychain.set(key_id, res1.refresh_token)
access_token = res1.access_token
}
// request the data
let req2 = new Request(`https://api.netatmo.net/api/gethomecoachsdata?access_token=${encodeURI(access_token)}`)
let res2 = await req2.loadJSON()
if (!res2.body || !res2.body.devices || res2.body.devices.length == 0)
throw new Error("No data received")
// store data into d and dd
d = res2.body.devices[0]
dd = d.dashboard_data
} catch (e) {
ex = e.message
}
}
// layout data on widget
// setup
let padding = 6*Device.screenScale()
let paddingLine = 1*Device.screenScale()
let widgetFamily = config.widgetFamily || "medium";
let f = new DateFormatter()
f.useMediumDateStyle()
f.useShortTimeStyle()
let fm16 = Font.mediumSystemFont(14)
let fl16 = Font.lightSystemFont(14)
let fm40 = Font.mediumRoundedSystemFont(40)
let fl40 = Font.lightRoundedSystemFont(40)
// helper
function row(p) {
let r = p.addStack()
let vertical = 0
for (let x = 1; x < arguments.length; x++) {
if (x == 1 && arguments[1][0].center) {
r.addSpacer()
}
let i = arguments[x]
let c = r.addStack()
for (let y = 0; y < i.length; y++) {
let s = i[y]
if (s.vertical) {
c.layoutVertically()
c.centerAlignContent()
vertical = s.vertical
} else {
c.centerAlignContent()
}
let tc = c.addStack(), t1 = null, t2 = null
tc.setPadding(s.padt || 0, 0, s.padb || 0, 0)
t1 = tc.addText(s.t1)
t1.textColor = Color.white()
t1.font = s.f1|| fl16
t1.lineLimit = 1
if (s.t2) {
t2 = tc.addText(s.t2)
t2.textColor = Color.white()
t2.font = s.f2 || fl16
t2.lineLimit = 1
}
if (vertical) {
if (y == i.length - 1) {
} else {
c.addSpacer(vertical == 99 ? null : vertical)
}
} else {
if (y == i.length - 1) {
if (t1) t1.rightAlignText()
if (t2) t2.rightAlignText()
} else {
c.addSpacer()
}
}
}
if (x != arguments.length - 1) {
r.addSpacer()
}
if (x == 1 && arguments[1][0].center) {
r.addSpacer()
}
}
return r
}
// layout
let w = new ListWidget()
w.setPadding(0, 0, 0, 0)
w.minimumScaleFactor = 0.6
let g = new LinearGradient()
g.colors = [new Color("64C5E8"),new Color("2F83B9")]
g.locations = [0,1.5]
w.backgroundGradient = g
let s = w.addStack()
s.layoutVertically()
if (ex) {
// data error
let r1 = s.addStack()
let t = r1.addText(`Error loading data. Please check that you've setup the params variable correctly: ${ex}`)
t.textColor = Color.white()
t.font = fm16
r1.setPadding(padding, padding, padding, padding)
r1.backgroundColor = new Color("FF666B")
} else if (widgetFamily == "small") {
// small
padding = 6*Device.screenScale()
f.useNoDateStyle()
fm16 = Font.mediumSystemFont(12)
fl16 = Font.lightSystemFont(12)
fm40 = Font.mediumRoundedSystemFont(34)
fl40 = Font.lightRoundedSystemFont(34)
// row 1
let r1 = row(s, [
{ t1: `${(d.reachable ? "" : "(Unreachable) ") + d.station_name}`, f1: fm16 },
{ t1: `${f.string(new Date(dd.time_utc * 1000))}` } ]
)
r1.setPadding(padding, padding, 0, padding)
// row 2
let r2 = row(s, [
{ t1: `${Math.trunc(dd.Temperature)}`, f1: fm40, t2: `°`, f2: fl40 } ], [
{ t1: `⤒ ${Math.trunc(dd.max_temp || dd.Temperature)}°`, vertical: 1, padt: 6 },
{ t1: `⤓ ${Math.trunc(dd.min_temp || dd.Temperature)}°`, padb: 6 } ]
)
r2.setPadding(0, padding, paddingLine, padding)
// rows 3-6
let sN = s.addStack()
sN.layoutVertically()
sN.setPadding(paddingLine*2, padding, padding, padding)
sN.backgroundColor = g.colors[0]
row(sN, [
{ t1: `HUMIDITY` },
{ t1: `${dd.Humidity}`, f1: fm16, t2: ` %`}
]).setPadding(0, 0, paddingLine, 0)
row(sN, [
{ t1: `CO₂` },
{ t1: `${dd.CO2}`, f1: fm16, t2: ` ppm`}
]).setPadding(0, 0, paddingLine, 0)
row(sN, [
{ t1: `NOISE` },
{ t1: `${dd.Noise}`, f1: fm16, t2: ` dB`}
]).setPadding(0, 0, paddingLine, 0)
row(sN, [
{ t1: `PRESSURE` },
{ t1: `${Math.trunc(dd.AbsolutePressure)}`, f1: fm16, t2: ` mb` }]).setPadding(0, 0, 0, 0)
} else {
// medium & large
// row 1
let r1 = row(s, [
{ t1: `${(d.reachable ? "" : "(Unreachable) ") + d.station_name}`, f1: fm16 },
{ t1: `${f.string(new Date(dd.time_utc * 1000))}` } ]
)
if (widgetFamily == "medium") {
r1.setPadding(padding, padding, 0, padding)
s.addSpacer()
} else {
r1.setPadding(padding, padding, padding,padding)
s.addSpacer()
let health = "", color = "000000"
if (dd.health_idx == 0) {
health = "Healthy"
color = "83A3D2"
} else if (dd.health_idx == 1) {
health = "Fine"
color = "3EC59D"
} else if (dd.health_idx == 2) {
health = "Fair"
color = "F8E71D"
} else if (dd.health_idx == 3) {
health = "Poor"
color = "FEC470"
} else if (dd.health_idx == 4) {
health = "Unhealthy"
color = "FF666B"
}
if (health != "") {
let r1X = row(s, [
{ t1: `${health}`, f1: fm40, center: true } ]
)
r1X.setPadding(padding, padding, padding, padding)
r1X.backgroundColor = new Color(color)
}
}
// row 2
f.useNoDateStyle()
let r2 = row(s, [
{ t1: `${dd.Temperature.toFixed(1)}`, f1: fm40, t2: `°`, f2: fl40 } ], [
{ t1: `⤒ ${(dd.max_temp || dd.Temperature).toFixed(1)}° at ${f.string(new Date(dd.date_max_temp * 1000 || dd.time_utc * 1000))}`, vertical: 2, padt: 6 },
{ t1: `⤓ ${(dd.min_temp || dd.Temperature).toFixed(1)}° at ${f.string(new Date(dd.date_min_temp * 1000 || dd.time_utc * 1000))}`, padb: 6 } ]
)
if (widgetFamily == "medium") {
r2.setPadding(0, padding, 0, padding)
s.addSpacer()
} else {
r2.setPadding(padding, padding, padding, padding)
s.addSpacer()
}
// row 3
let r3 = row(s, [
{ t1: `HUMIDITY`, vertical: paddingLine },
{ t1: `${dd.Humidity}`, f1: fm16, t2: ` %`}
], [
{ t1: `CO₂`, vertical: paddingLine },
{ t1: `${dd.CO2}`, f1: fm16, t2: ` ppm`}
], [
{ t1: `NOISE`, vertical: paddingLine },
{ t1: `${dd.Noise}`, f1: fm16, t2: ` dB`}
], [
{ t1: `PRESSURE`, vertical: paddingLine },
{ t1: `${Math.trunc(dd.AbsolutePressure)}`, f1: fm16, t2: ` mbar` }
])
if (widgetFamily == "medium") {
r3.setPadding(padding/2, padding, padding/2, padding)
} else {
r3.setPadding(padding, padding, padding, padding)
}
r3.backgroundColor = g.colors[0]
}
// display
if (config.runsInApp) {
w.presentMedium()
} else {
Script.setWidget(w)
}
Script.complete()