-
Notifications
You must be signed in to change notification settings - Fork 1
/
smartac_timer.py
238 lines (195 loc) · 6.57 KB
/
smartac_timer.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
import aiohttp
import asyncio
from datetime import datetime, timedelta
import json
import logging
import os
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.responses import JSONResponse
from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles
logger = logging.getLogger('smartac_timer')
f = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
h = logging.StreamHandler()
h.setFormatter(f)
logger.setLevel(logging.INFO)
logger.addHandler(h)
# Load settings
with open(os.path.join(os.path.dirname(__file__),
'settings.py'), 'rb') as fp:
SETTINGS = {}
exec(fp.read(), SETTINGS, SETTINGS)
# Device timers
timers = {}
def get_http_client():
if get_http_client._instance is None:
get_http_client._instance = aiohttp.ClientSession(
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/74.0.3729.169 '
'Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Accept': '*/*',
},
)
if os.path.exists('cookies.bin'):
logger.info("Loading cookies from file")
get_http_client._instance.cookie_jar.load('cookies.bin')
return get_http_client._instance
get_http_client._instance = None
async def mymodlet_req(method, path, **kwargs):
http_client = get_http_client()
re_authenticated = False
while True:
async with http_client.request(
method,
'https://web.mymodlet.com' + path,
**kwargs,
) as response:
if response.status == 401 and not re_authenticated:
# Try and re-authenticate
pass
else:
response.raise_for_status()
return response
# Re-authenticate
logger.warning("Re-authenticating")
# Get fresh cookies
http_client.cookie_jar.clear()
async with http_client.get(
'https://web.mymodlet.com/'
+ 'Account/Login/?ReturnUrl=%2F&ReturnUrl=%2F',
allow_redirects=True,
) as response:
response.raise_for_status()
# Post login/password to start new session
async with http_client.post(
'https://web.mymodlet.com/Account/Login?returnUrl=/',
allow_redirects=False,
json={
'data': json.dumps({
'Email': SETTINGS['email'],
'Password': SETTINGS['password'],
}),
},
):
response.raise_for_status()
re_authenticated = True
http_client.cookie_jar.save('cookies.bin')
mymodlet_get = lambda url, **kwargs: mymodlet_req('GET', url, **kwargs)
mymodlet_post = lambda url, **kwargs: mymodlet_req('POST', url, **kwargs)
def temp_f(f):
return dict(
farenheit=f,
celsius=round((f-32) * 5 / 9),
)
async def status(request):
response = await mymodlet_get('/Devices/UpdateData')
status = await response.text()
# It's a JSON string with JSON inside (double encoded)
status = json.loads(status)
assert isinstance(status, str)
status = json.loads(status)
modlets = {}
for modlet in status['SmartACs']:
for channel in modlet['modlet']['modletChannels']:
modlets[channel['deviceId']] = modlet
devices = []
for device in status['Devices']:
device_id = device['deviceId']
if device['type'].lower() == 'air conditioner':
kind = 'ac'
else:
kind = None
device_info = dict(
kind=kind,
id=device_id,
name=device['deviceName'],
status='on' if device['isOn'] else 'off',
)
if device_id in modlets:
modlet = modlets[device_id]
if 'thermostat' in modlet:
device_info['thermostat'] = temp_f(
modlet['thermostat']['targetTemperature'],
)
device_info['temperature'] = temp_f(
modlet['thermostat']['currentTemperature'],
)
if device_id in timers:
mode, time, _ = timers[device_id]
device_info['timer'] = dict(
when=time.isoformat() + 'Z',
mode=mode,
)
devices.append(device_info)
return JSONResponse(dict(
devices=devices,
))
async def do_switch(device, mode):
try:
set_mode = dict(on='SwitchOn', off='SwitchOff')[mode]
except KeyError:
raise HTTPException(404)
# Cancel pending timers
if device in timers:
_, _, fut = timers.pop(device)
fut.cancel()
logger.info("Switching AC %s", mode)
response = await mymodlet_post(
'/Devices/%s' % set_mode,
headers={
'Referer': 'https://web.mymodlet.com/Devices',
'Content-Type': 'application/json',
},
json={
'data': '{"id":"%s"}' % device, # Yes, it is double-json-encoded
},
)
response.raise_for_status()
async def switch(request):
device = request.path_params['device']
mode = request.path_params['mode']
if mode not in ('on', 'off'):
raise HTTPException(400)
await do_switch(device, mode)
return JSONResponse({})
async def sleep_then_switch(delay, device, mode):
try:
await asyncio.sleep(delay)
timers.pop(device, None)
await asyncio.shield(do_switch(device, mode))
except asyncio.CancelledError:
pass
async def set_timer(request):
device = request.path_params['device']
obj = await request.json()
delay = obj.pop('delay')
mode = obj.pop('mode', 'off')
if mode not in ('on', 'off'):
raise HTTPException(400)
if obj:
raise HTTPException(400)
if device in timers:
_, _, fut = timers.pop(device)
fut.cancel()
fut = asyncio.get_event_loop().create_task(
sleep_then_switch(delay, device, mode),
)
timers[device] = mode, datetime.utcnow() + timedelta(seconds=delay), fut
return JSONResponse({})
routes = [
Route('/api/status', status),
Route(
'/api/switch/{device:int}/{mode}', switch,
methods=['POST'],
),
Route(
'/api/set-timer/{device:int}', set_timer,
methods=['POST'],
),
Mount('/', app=StaticFiles(directory='static', html=True)),
]
app = Starlette(routes=routes, debug=True)