-
Notifications
You must be signed in to change notification settings - Fork 4
/
controlmyspa.py
463 lines (426 loc) · 14.5 KB
/
controlmyspa.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
Python module to get metrics from and control Balboa ControlMySpa whirlpools
"""
import requests
import certifi
class ControlMySpa:
"""
Class representing Balboa ControlMySpa whirlpools
"""
_email = None
_password = None
def __init__(self, email, password):
"""
Initialize connection to Balboa ControlMySpa cloud API
:param email: email address used to log in
:param password: password used to log in
"""
self._email = email
self._password = password
"""
2023-12-13: iot.controlmyspa.com has a new TLS certificate, probably since
June 2023. This certificate is signed by digicert, but there is an intermediate
certificate missing in the python certifi trust store and the server does not
provide it (anymore?). Instead of disabling the TLS certificate validation, we
download the intermediate certificate from digicert over a successfully
verified TLS connection and add it to the local trust store. Sorry for the hack."""
try:
self._get_idm()
except requests.exceptions.SSLError:
print("TLS certificate missing, downloading to " + certifi.where())
customca = requests.get(
"https://cacerts.digicert.com/RapidSSLTLSRSACAG1.crt.pem", timeout=10
).content
cafile = certifi.where()
with open(cafile, "ab") as outfile:
outfile.write(b"\n")
outfile.write(customca)
outfile.close()
# log in and fetch pool info
self._get_idm()
self._do_login()
self._get_whoami()
self._get_info()
def _get_idm(self):
"""
Get URL and basic auth to log in to IDM
"""
response = requests.get(
"https://iot.controlmyspa.com/idm/tokenEndpoint", timeout=10
)
response.raise_for_status()
self._idm = response.json()
return self._idm
def _do_login(self):
"""
Log in and get API access tokens
"""
response = requests.post(
self._idm["_links"]["tokenEndpoint"]["href"],
data={
"grant_type": "password",
"password": self._password,
"scope": "openid user_name",
"username": self._email,
},
auth=(
self._idm["mobileClientId"],
self._idm["mobileClientSecret"],
),
timeout=10,
)
response.raise_for_status()
self._token = response.json()
return self._token
def _get_whoami(self):
"""
Get information about the logged in user, the owner
"""
response = requests.get(
self._idm["_links"]["whoami"]["href"],
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
self._user = response.json()
return self._user
def _get_info(self):
"""
Get all the details for the whirlpool of the logged in user
"""
response = requests.get(
"https://iot.controlmyspa.com/mobile/spas/search/findByUsername",
params={"username": self._email},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
self._info = response.json()
return self._info
@property
def current_temp(self):
"""
Get current pool temperature
"""
# update fresh info
# self._get_info()
if self._info["currentState"]["celsius"]:
return round(
(float(self._info["currentState"]["currentTemp"]) - 32) * 5 / 9, 1
)
return float(self._info["currentState"]["currentTemp"])
@property
def desired_temp(self):
"""
Get desired pool temperature
"""
# update fresh info
# self._get_info()
if self._info["currentState"]["celsius"]:
return round(
(float(self._info["currentState"]["desiredTemp"]) - 32) * 5 / 9, 1
)
return float(self._info["currentState"]["desiredTemp"])
@desired_temp.setter
def desired_temp(self, temperature):
"""
Set the desired temperature of the whirlpool
:param temperature: temperature, in celsius if the whirlpool is set to celsius or
in fahrenheit if the whirlpool is set to fahrenheit
"""
if self._info["currentState"]["celsius"]:
# convert to fahrenheit since the API always expects fahrenheit
temperature = round(temperature / 5 * 9 + 32, 1)
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setDesiredTemp",
json={"desiredTemp": temperature},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def temp_range(self):
"""
Get temp range HIGH (True) or LOW (False)
"""
# update fresh info
# self._get_info()
return self._info["currentState"]["tempRange"] == "HIGH"
@temp_range.setter
def temp_range(self, temp_range=True):
"""
Set temp range HIGH or LOW
:param temp_range: True for HIGH, False for LOW
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setTempRange",
json={"desiredState": ("HIGH" if temp_range else "LOW")},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def heater_mode(self):
"""
Get heater mode of spa READY (True) or REST (False)
"""
# update fresh info
# self._get_info()
return self._info["currentState"]["heaterMode"] == "READY"
@heater_mode.setter
def heater_mode(self, heater_mode=True):
"""
Set heater mode READY or REST
:param heater_mode: True for READY, False for REST
"""
if self.heater_mode != heater_mode:
# toggle the heater mode if current state and the parameter heater_mode differ
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/toggleHeaterMode",
json={"originatorId": ""},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def panel_lock(self):
"""
Get panel lock status, Locked = True, unlocked = False
"""
# update fresh info
# self._get_info()
return self._info["currentState"]["panelLock"]
@panel_lock.setter
def panel_lock(self, lock=True):
"""
Set panel lock
:param lock: True for locked, False for unlocked
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setPanel",
json={"desiredState": ("LOCK_PANEL" if lock else "UNLOCK_PANEL")},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
def get_jet(self, jet_number=0):
"""
get jet state HIGH = True, OFF = False
:param jet_number: My pool has jets 0, 1 and 2
"""
# update fresh info
# self._get_info()
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "PUMP" and x["port"] == str(jet_number)
][0]
def set_jet(self, jet_number=0, state=False):
"""
Enable/disable jet
:param jet_number: My pool has jets 0, 1 and 2
:param state: False to furn off, True to turn on
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setJetState",
json={
"desiredState": ("HIGH" if state else "OFF"),
"deviceNumber": jet_number,
"originatorId": "optional-Jet",
},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def jets(self):
"""
get an array of jets True/False (ON/OFF) status
"""
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "PUMP"
]
@jets.setter
def jets(self, array):
"""
set jets ON/OFF based on array of True/False
:param array: array of True/False
"""
for i, state in enumerate(array):
self.set_jet(i, state)
@property
def circulation_pumps(self):
"""
get an array of circulation pumps True/False (ON/OFF) status
(just information, cannot be set)
"""
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "CIRCULATION_PUMP"
]
@property
def ozone_generators(self):
"""
get an array of ozone generators True/False (ON/OFF) status
(just information, cannot be set)
"""
return [
x["value"] == "ON"
for x in self._info["currentState"]["components"]
if x["componentType"] == "OZONE"
]
def get_blower(self, blower_number=0):
"""
get blower state HIGH = True, OFF = False
:param blower_number: My pool has no blowers
"""
# update fresh info
# self._get_info()
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "BLOWER" and x["port"] == str(blower_number)
][0]
def set_blower(self, blower_number=0, state=False):
"""
Enable/disable jet
:param jet_number: My pool has blowers 0, 1 and 2
:param state: False to furn off, True to turn on
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setBlowerState",
json={
"desiredState": ("HIGH" if state else "OFF"),
"deviceNumber": blower_number,
"originatorId": "optional-Blower",
},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def blowers(self):
"""
get an array of blowers True/False (ON/OFF) status
"""
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "BLOWER"
]
@blowers.setter
def blowers(self, array):
"""
set blowers ON/OFF based on array of True/False
:param array: array of True/False
"""
for i, state in enumerate(array):
self.set_blower(i, state)
def get_light(self, light_number=0):
"""
get light state HIGH = True, OFF = False
:param light_number: My pool has light 0
"""
# update fresh info
# self._get_info()
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "LIGHT" and x["port"] == str(light_number)
][0]
def set_light(self, light_number=0, state=False):
"""
Enable/disable light
:param jet_number: My pool has lights 0, 1 and 2
:param state: False to furn off, True to turn on
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/setLightState",
json={
"desiredState": ("HIGH" if state else "OFF"),
"deviceNumber": light_number,
"originatorId": "optional-Light",
},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()
@property
def lights(self):
"""
get an array of lights True/False (ON/OFF) status
"""
return [
x["value"] == "HIGH"
for x in self._info["currentState"]["components"]
if x["componentType"] == "LIGHT"
]
@lights.setter
def lights(self, array):
"""
set lights ON/OFF based on array of True/False
:param array: array of True/False
"""
for i, state in enumerate(array):
self.set_light(i, state)
def get_serial(self):
"""
Get spa serial number
"""
return self._info["serialNumber"]
@property
def online(self):
"""
Get the spa online status
"""
return self._info["online"]
def set_chromazon3(self, state=False):
"""
Enable/disable Chromeazon3 lights
https://www.balboawatergroup.com/Chromazon3
:param state: False to turn off, True to turn on
"""
response = requests.post(
"https://iot.controlmyspa.com/mobile/control/"
+ self._info["_id"]
+ "/tzl/setPower",
json={
"desiredState": ("ON" if state else "OFF"),
},
headers={"Authorization": "Bearer " + self._token["access_token"]},
timeout=10,
)
response.raise_for_status()
# update the local info
self._get_info()