Repository navigation
Expand file tree
/
Copy pathdriver.py
More file actions
203 lines (164 loc) · 6.31 KB
/
Copy pathdriver.py
File metadata and controls
203 lines (164 loc) · 6.31 KB
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
import dataclasses
from binascii import crc32
from typing import Optional
import requests
@dataclasses.dataclass
class WinixDeviceStub:
id: str
mac: str
alias: str
location_code: str
filter_replace_date: str
class WinixAccount:
def __init__(self, access_token: str):
self._uuid: Optional[str] = None
self.access_token = access_token
def check_access_token(self):
"""Register the Cognito Token with the Winix backen (again)"""
from winix import auth
payload = {
"cognitoClientSecretKey": auth.COGNITO_CLIENT_SECRET_KEY,
"accessToken": self.access_token,
"uuid": self.get_uuid(),
"osVersion": "26", # oreo
"mobileLang": "en",
}
resp = requests.post(
"https://us.mobile.winix-iot.com/checkAccessToken", json=payload
)
if resp.status_code != 200:
raise Exception(
f"Error while performing RPC checkAccessToken ({resp.status_code}): {resp.text}"
)
def get_device_info_list(self):
resp = requests.post(
"https://us.mobile.winix-iot.com/getDeviceInfoList",
json={
"accessToken": self.access_token,
"uuid": self.get_uuid(),
},
)
if resp.status_code != 200:
raise Exception(
f"Error while performing RPC checkAccessToken ({resp.status_code}): {resp.text}"
)
return [
WinixDeviceStub(
id=d["deviceId"],
mac=d["mac"],
alias=d["deviceAlias"],
location_code=d["deviceLocCode"],
filter_replace_date=d["filterReplaceDate"],
)
for d in resp.json()["deviceInfoList"]
]
def register_user(self, email: str):
"""Register the logged-in android login/android user uuid with the backend"""
# Call after getting a cognito token but before check_access_token
# necessary for the winix backend to recognize the Android "uuid" we send
# in most API requests
from winix import auth
resp = requests.post(
"https://us.mobile.winix-iot.com/registerUser",
json={
"cognitoClientSecretKey": auth.COGNITO_CLIENT_SECRET_KEY,
"accessToken": self.access_token,
"uuid": self.get_uuid(),
"email": email,
"osType": "android",
"osVersion": "29",
"mobileLang": "en",
},
)
if resp.status_code != 200:
raise Exception(
f"Error while performing RPC registerUser ({resp.status_code}): {resp.text}"
)
def get_uuid(self) -> str:
# We construct our fake secure Android ID as
# CRC32("github.com/hfern/winixctl" + userid) + CRC32("HGF" + userid)
# where userid is the formatted uuid string from cognito
if self._uuid is None:
from jose import jwt
userid_b = jwt.get_unverified_claims(self.access_token)["sub"].encode()
p1 = crc32(b"github.com/hfern/winixctl" + userid_b)
p2 = crc32(b"HGF" + userid_b)
self._uuid = f"{p1:08x}{p2:08x}"
return self._uuid
class WinixDevice:
CTRL_URL = "https://us.api.winix-iot.com/common/control/devices/{deviceid}/A211/{attribute}:{value}"
STATE_URL = "https://us.api.winix-iot.com/common/event/sttus/devices/{deviceid}"
category_keys = {
"power": "A02",
"mode": "A03",
"airflow": "A04",
"aqi": "A05",
"plasma": "A07",
"filter_hour": "A21",
"air_quality": "S07",
"air_qvalue": "S08",
"ambient_light": "S14",
}
state_keys = {
"power": {"off": "0", "on": "1"},
"mode": {"auto": "01", "manual": "02"},
"airflow": {
"low": "01",
"medium": "02",
"high": "03",
"turbo": "05",
"sleep": "06",
},
"plasma": {"off": "0", "on": "1"},
"air_quality": {"good": "01", "fair": "02", "poor": "03"},
}
def __init__(self, id):
self.id = id
def off(self):
self._rpc_attr(self.category_keys["power"], self.state_keys["power"]["off"])
def on(self):
self._rpc_attr(self.category_keys["power"], self.state_keys["power"]["on"])
def auto(self):
self._rpc_attr(self.category_keys["mode"], self.state_keys["mode"]["auto"])
def manual(self):
self._rpc_attr(self.category_keys["mode"], self.state_keys["mode"]["manual"])
def plasmawave_off(self):
self._rpc_attr(self.category_keys["plasma"], self.state_keys["plasma"]["off"])
def plasmawave_on(self):
self._rpc_attr(self.category_keys["plasma"], self.state_keys["plasma"]["on"])
def low(self):
self._rpc_attr(self.category_keys["airflow"], self.state_keys["airflow"]["low"])
def medium(self):
self._rpc_attr(
self.category_keys["airflow"], self.state_keys["airflow"]["medium"]
)
def high(self):
self._rpc_attr(
self.category_keys["airflow"], self.state_keys["airflow"]["high"]
)
def turbo(self):
self._rpc_attr(
self.category_keys["airflow"], self.state_keys["airflow"]["turbo"]
)
def sleep(self):
self._rpc_attr(
self.category_keys["airflow"], self.state_keys["airflow"]["sleep"]
)
def _rpc_attr(self, attr: str, value: str):
requests.get(
self.CTRL_URL.format(deviceid=self.id, attribute=attr, value=value)
)
def get_state(self):
r = requests.get(self.STATE_URL.format(deviceid=self.id))
payload = r.json()["body"]["data"][0]["attributes"]
output = dict()
for (payload_key, attribute) in payload.items():
for (category, local_key) in self.category_keys.items():
if payload_key == local_key:
if category in self.state_keys.keys():
for (value_key, value) in self.state_keys[category].items():
if attribute == value:
output[category] = value_key
else:
output[category] = int(attribute)
return output