Repository navigation
Expand file tree
/
Copy pathcmd.py
More file actions
335 lines (270 loc) · 9.07 KB
/
Copy pathcmd.py
File metadata and controls
335 lines (270 loc) · 9.07 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
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
3
8000
11
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import argparse
import json
import os
import dataclasses
import sys
from getpass import getpass
from os import path, makedirs
from typing import Optional, List
from winix import WinixAccount, WinixDevice, WinixDeviceStub
from winix.auth import WinixAuthResponse, login, refresh
DEFAULT_CONFIG_PATH = "~/.config/winix/config.json"
class UserError(Exception):
pass
class JSONEncoder(json.JSONEncoder):
def default(self, o):
return (
dataclasses.asdict(o) if dataclasses.is_dataclass(o) else super().default(o)
)
class Configuration:
exists: bool
cognito: Optional[WinixAuthResponse]
devices: List[WinixDeviceStub]
def __init__(self, config_path: str):
self.config_path = path.expanduser(config_path)
self._load_from_disk()
def device(self, selector: str) -> WinixDeviceStub:
try:
return [
d
for i, d in enumerate(self.devices)
if selector.lower() in (str(i), d.mac.lower(), d.alias.lower())
][0]
except IndexError:
raise UserError(
f'Could not find device matching "{selector}"! '
f"You can use Index, MAC, or Alias for the selector. "
f"View the list of available devices with `winixctl devices`."
)
def _load_from_disk(self):
if path.exists(self.config_path):
with open(self.config_path) as f:
js = json.load(f)
self.exists = True
self.cognito = (
WinixAuthResponse(**js["cognito"])
if js.get("cognito") is not None
else None
)
self.devices = [WinixDeviceStub(**d) for d in js.get("devices", [])]
else:
self.exists = False
self.cognito = None
self.devices = []
def save(self):
makedirs(path.dirname(self.config_path), mode=0o755, exist_ok=True)
with open(os.open(self.config_path, os.O_CREAT | os.O_WRONLY, 0o755), "w") as f:
f.truncate()
js = json.dumps(
{
"cognito": self.cognito,
"devices": self.devices,
},
cls=JSONEncoder,
)
f.write(js)
class Cmd:
def __init__(self, args: argparse.Namespace, config: Configuration):
self.args = args
self.config = config
def active_device_id(self) -> str:
return self.config.device(self.args.device_selector).id
class LoginCmd(Cmd):
parser_args = {
"name": "login",
"help": "Authenticate Winix account",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument("--username",
help="Username (email)",
required=False)
parser.add_argument("--password", help="Password", required=False)
parser.add_argument(
"--refresh",
dest="refresh",
action="store_true",
help="Refresh the Winix Cognito token instead of logging in",
)
def execute(self):
if getattr(self.args, "refresh"):
return self._refresh()
else:
return self._login()
def _login(self):
print(
"You need to signup for a Winix account & associate your device in the phone app before using this."
)
username = getattr(self.args, "username") or input("Username (email): ")
password = getattr(self.args, "password") or getpass("Password: ")
self.config.cognito = login(username, password)
account = WinixAccount(self.config.cognito.access_token)
account.register_user(username)
account.check_access_token()
self.config.devices = account.get_device_info_list()
self.config.save()
print("Ok")
def _refresh(self):
if self.config.cognito is None:
raise UserError("No Cognito token found. Please login first.")
self.config.cognito = refresh(
user_id=self.config.cognito.user_id,
refresh_token=self.config.cognito.refresh_token,
)
WinixAccount(self.config.cognito.access_token).check_access_token()
self.config.save()
print("Ok")
class DevicesCmd(Cmd):
parser_args = {
"name": "devices",
"help": "List registered Winix devices",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument(
"--expose", action="store_true", help="Expose sensitive Device ID string"
)
def execute(self):
expose = getattr(self.args, "expose", False)
print(f"{len(self.config.devices)} devices:")
for i, device in enumerate(self.config.devices):
hidden_deviceid = "_".join(
[
device.id.split("_")[0],
"*" * len(device.id.split("_", 1)[1]),
]
)
fields = (
("Device ID", device.id if expose else hidden_deviceid + " (hidden)"),
("Mac", device.mac),
("Alias", device.alias),
("Location", device.location_code),
)
label = " (default)" if i == 0 else ""
print(f"Device#{i}{label} ".ljust(50, "-"))
for f, v in fields:
print(f"{f:>15} : {v}")
print("")
print("Missing a device? You might need to run refresh.")
class FanCmd(Cmd):
parser_args = {
"name": "fan",
"help": "Fan speed controls",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument(
"level",
help="Fan level",
choices=["low", "medium", "high", "turbo", "sleep"],
)
def execute(self):
level = self.args.level
# TODO(Hunter): Support getting the fan state instead of only being able to set it
device = WinixDevice(self.active_device_id())
getattr(device, level)()
print("ok")
class PowerCmd(Cmd):
parser_args = {
"name": "power",
"help": "Power controls",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument("state", help="Power state", choices=["on", "off"])
def execute(self):
state = self.args.state
device = WinixDevice(self.active_device_id())
getattr(device, state)()
print("ok")
class ModeCmd(Cmd):
parser_args = {
"name": "mode",
"help": "Mode controls",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument("state", help="Mode state", choices=["auto", "manual"])
def execute(self):
state = self.args.state
device = WinixDevice(self.active_device_id())
getattr(device, state)()
print("ok")
class PlasmawaveCmd(Cmd):
parser_args = {
"name": "plasmawave",
"help": "Plasmawave controls",
}
@classmethod
def add_parser(cls, parser):
parser.add_argument("state", help="Plasmawave state", choices=["on", "off"])
def execute(self):
state = "plasmawave_on" if self.args.state == "on" else "plasmawave_off"
device = WinixDevice(self.active_device_id())
getattr(device, state)()
print("ok")
class RefreshCmd(Cmd):
parser_args = {
"name": "refresh",
"help": "Refresh account device metadata",
}
@classmethod
def add_parser(cls, parser):
pass
def execute(self):
# pyrefly: ignore [missing-attribute]
account = WinixAccount(self.config.cognito.access_token)
self.config.devices = account.get_device_info_list()
self.config.save()
print("Ok")
class StateCmd(Cmd):
parser_args = {
"name": "getstate",
"help": "Get device state",
}
@classmethod
def add_parser(cls, parser):
pass
def execute(self):
device = WinixDevice(self.active_device_id())
state = "get_state"
status = getattr(device, state)()
for f, v in status.items():
print(f"{f:>15} : {v}")
def main():
parser = argparse.ArgumentParser(description="Winix C545 Air Purifier Control")
parser.add_argument(
"--device",
"-D",
help="Device Index/Mac/Alias to use",
default="0",
dest="device_selector",
)
subparsers = parser.add_subparsers(dest="cmd")
commands = {
cls.parser_args["name"]: cls
for cls in (
LoginCmd,
RefreshCmd,
DevicesCmd,
StateCmd,
FanCmd,
PowerCmd,
ModeCmd,
PlasmawaveCmd,
)
}
for cls in commands.values():
sub = subparsers.add_parser(**cls.parser_args)
cls.add_parser(sub)
args = parser.parse_args()
cmd = args.cmd
if cmd is None:
parser.print_help()
return
cls = commands[cmd]
try:
cls(args, config=Configuration("~/.config/winix/config.json")).execute()
except UserError as exc:
print(str(exc), file=sys.stderr)
sys.exit(1)