-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toon.py
104 lines (88 loc) · 4.67 KB
/
Toon.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
import re
import json
import sys
import uuid
import pprint
import requests
""" The Toon module impersonates the Eneco Toon mobile app, and implements
similar functionality. """
class Toon:
""" Log in to the Toon API, and do stuff. """
def __init__(self, username, password):
self.username = username
self.password = password
self.toonstate = None
self.sessiondata = None
self.debug = 0
def login(self):
""" Log in to the toon API, and set up a session. """
# First we open the login page, to get the agreement
# details.
# Agreement details and user details are stored in the
# sessiondata variable.
formdata = {"username": self.username,
"password": self.password}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/login",
params=formdata)
self.sessiondata = r.json()
# Now re-use the agreement details to do the actual
# authentication. This establishes a session, and allows
# state to be retrieved.
# TODO: check for variable existence / throw exception on
# failure
formdata = {"clientId": self.sessiondata["clientId"],
"clientIdChecksum": self.sessiondata["clientIdChecksum"],
"agreementId": self.sessiondata["agreements"][0]["agreementId"],
"agreementIdChecksum": self.sessiondata["agreements"][0]["agreementIdChecksum"],
"random": uuid.uuid1()}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/start", params=formdata)
def logout(self):
""" Log out of the API.
This is needed, as toon keeps a maximum amount of display clients.
Too many logins lead to 500 responses from the API. """
formdata = {"clientId": self.sessiondata["clientId"],
"clientIdChecksum": self.sessiondata["clientIdChecksum"],
"random": uuid.uuid1()}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/logout", params=formdata)
self.toonstate = None
self.sessiondata = None
def retrieve_toon_state(self):
if self.toonstate is not None:
return
formdata = {"clientId": self.sessiondata["clientId"],
"clientIdChecksum": self.sessiondata["clientIdChecksum"],
"random": uuid.uuid1()}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/retrieveToonState", params=formdata)
self.toonstate = r.json()
def refresh_toon_state(self):
self.toonstate = None
self.retrieve_toon_state()
def get_gas_usage(self):
self.retrieve_toon_state()
return self.toonstate["gasUsage"]
def get_power_usage(self):
self.retrieve_toon_state()
return self.toonstate["powerUsage"]
def get_thermostat_info(self):
self.retrieve_toon_state()
return self.toonstate["thermostatInfo"]
def get_thermostat_states(self):
self.retrieve_toon_state()
return self.toonstate["thermostatStates"]
def set_thermostat(self, temperature):
targettemp = int(temperature*100)
formdata = {"clientId": self.sessiondata["clientId"],
"clientIdChecksum": self.sessiondata["clientIdChecksum"],
"value": targettemp,
"random": uuid.uuid1()}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/setPoint", params=formdata)
def get_program_state(self):
self.retrieve_toon_state()
return self.toonstate["thermostatInfo"]["activeState"]
def set_program_state(self,targetstate):
formdata = {"clientId": self.sessiondata["clientId"],
"clientIdChecksum": self.sessiondata["clientIdChecksum"],
"state": 2,
"temperatureState": targetstate,
"random": uuid.uuid1()}
r = requests.get("https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/schemeState", params=formdata)