forked from Python-roborock/python-roborock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
337 lines (279 loc) · 11.6 KB
/
cli.py
File metadata and controls
337 lines (279 loc) · 11.6 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
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
import asyncio
import json
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import click
from pyshark import FileCapture # type: ignore
from pyshark.capture.live_capture import LiveCapture, UnknownInterfaceException # type: ignore
from pyshark.packet.packet import Packet # type: ignore
from roborock import RoborockException
from roborock.containers import DeviceData, HomeData, HomeDataProduct, LoginData, NetworkInfo, RoborockBase, UserData
from roborock.devices.device_manager import create_device_manager, create_home_data_api
from roborock.protocol import MessageParser
from roborock.util import run_sync
from roborock.version_1_apis.roborock_local_client_v1 import RoborockLocalClientV1
from roborock.version_1_apis.roborock_mqtt_client_v1 import RoborockMqttClientV1
from roborock.web_api import RoborockApiClient
_LOGGER = logging.getLogger(__name__)
@dataclass
class ConnectionCache(RoborockBase):
"""Cache for Roborock data.
This is used to store data retrieved from the Roborock API, such as user
data and home data to avoid repeated API calls.
This cache is superset of `LoginData` since we used to directly store that
dataclass, but now we also store additional data.
"""
user_data: UserData
email: str
home_data: HomeData | None = None
network_info: dict[str, NetworkInfo] | None = None
class RoborockContext:
roborock_file = Path("~/.roborock").expanduser()
_cache_data: ConnectionCache | None = None
def __init__(self):
self.reload()
def reload(self):
if self.roborock_file.is_file():
with open(self.roborock_file) as f:
data = json.load(f)
if data:
self._cache_data = ConnectionCache.from_dict(data)
def update(self, cache_data: ConnectionCache):
data = json.dumps(cache_data.as_dict(), default=vars, indent=4)
with open(self.roborock_file, "w") as f:
f.write(data)
self.reload()
def validate(self):
if self._cache_data is None:
raise RoborockException("You must login first")
def cache_data(self) -> ConnectionCache:
"""Get the cache data."""
self.validate()
return self._cache_data
@click.option("-d", "--debug", default=False, count=True)
@click.version_option(package_name="python-roborock")
@click.group()
@click.pass_context
def cli(ctx, debug: int):
logging_config: dict[str, Any] = {"level": logging.DEBUG if debug > 0 else logging.INFO}
logging.basicConfig(**logging_config) # type: ignore
ctx.obj = RoborockContext()
@click.command()
@click.option("--email", required=True)
@click.option(
"--password",
required=False,
help="Password for the Roborock account. If not provided, an email code will be requested.",
)
@click.pass_context
@run_sync()
async def login(ctx, email, password):
"""Login to Roborock account."""
context: RoborockContext = ctx.obj
try:
context.validate()
_LOGGER.info("Already logged in")
return
except RoborockException:
pass
client = RoborockApiClient(email)
if password is not None:
user_data = await client.pass_login(password)
else:
print(f"Requesting code for {email}")
await client.request_code()
code = click.prompt("A code has been sent to your email, please enter the code", type=str)
user_data = await client.code_login(code)
print("Login successful")
context.update(LoginData(user_data=user_data, email=email))
@click.command()
@click.pass_context
@click.option("--duration", default=10, help="Duration to run the MQTT session in seconds")
@run_sync()
async def session(ctx, duration: int):
context: RoborockContext = ctx.obj
cache_data = context.cache_data()
home_data_api = create_home_data_api(cache_data.email, cache_data.user_data)
async def home_data_cache() -> HomeData:
if cache_data.home_data is None:
cache_data.home_data = await home_data_api()
context.update(cache_data)
return cache_data.home_data
# Create device manager
device_manager = await create_device_manager(cache_data.user_data, home_data_cache)
devices = await device_manager.get_devices()
click.echo(f"Discovered devices: {', '.join([device.name for device in devices])}")
click.echo("MQTT session started. Querying devices...")
for device in devices:
if not (status_trait := device.traits.get("status")):
click.echo(f"Device {device.name} does not have a status trait")
continue
try:
status = await status_trait.get_status()
except RoborockException as e:
click.echo(f"Failed to get status for {device.name}: {e}")
else:
click.echo(f"Device {device.name} status: {status.as_dict()}")
click.echo("Listening for messages.")
await asyncio.sleep(duration)
# Close the device manager (this will close all devices and MQTT session)
await device_manager.close()
async def _discover(ctx):
context: RoborockContext = ctx.obj
cache_data = context.cache_data()
if not cache_data:
raise Exception("You need to login first")
client = RoborockApiClient(cache_data.email)
home_data = await client.get_home_data_v3(cache_data.user_data)
cache_data.home_data = home_data
context.update(cache_data)
click.echo(f"Discovered devices {', '.join([device.name for device in home_data.get_all_devices()])}")
async def _load_and_discover(ctx) -> RoborockContext:
"""Discover devices if home data is not available."""
context: RoborockContext = ctx.obj
cache_data = context.cache_data()
if not cache_data.home_data:
await _discover(ctx)
cache_data = context.cache_data()
return context
@click.command()
@click.pass_context
@run_sync()
async def discover(ctx):
await _discover(ctx)
@click.command()
@click.pass_context
@run_sync()
async def list_devices(ctx):
context: RoborockContext = await _load_and_discover(ctx)
cache_data = context.cache_data()
home_data = cache_data.home_data
device_name_id = {device.name: device.duid for device in home_data.devices + home_data.received_devices}
click.echo(json.dumps(device_name_id, indent=4))
@click.command()
@click.option("--device_id", required=True)
@click.pass_context
@run_sync()
async def list_scenes(ctx, device_id):
context: RoborockContext = await _load_and_discover(ctx)
cache_data = context.cache_data()
client = RoborockApiClient(cache_data.email)
scenes = await client.get_scenes(cache_data.user_data, device_id)
output_list = []
for scene in scenes:
output_list.append(scene.as_dict())
click.echo(json.dumps(output_list, indent=4))
@click.command()
@click.option("--scene_id", required=True)
@click.pass_context
@run_sync()
async def execute_scene(ctx, scene_id):
context: RoborockContext = await _load_and_discover(ctx)
cache_data = context.cache_data()
client = RoborockApiClient(cache_data.email)
await client.execute_scene(cache_data.user_data, scene_id)
@click.command()
@click.option("--device_id", required=True)
@click.pass_context
@run_sync()
async def status(ctx, device_id):
context: RoborockContext = await _load_and_discover(ctx)
cache_data = context.cache_data()
home_data = cache_data.home_data
devices = home_data.devices + home_data.received_devices
device = next(device for device in devices if device.duid == device_id)
product_info: dict[str, HomeDataProduct] = {product.id: product for product in home_data.products}
device_data = DeviceData(device, product_info[device.product_id].model)
mqtt_client = RoborockMqttClientV1(cache_data.user_data, device_data)
if not (networking := cache_data.network_info.get(device.duid)):
networking = await mqtt_client.get_networking()
cache_data.network_info[device.duid] = networking
context.update(cache_data)
else:
_LOGGER.debug("Using cached networking info for device %s: %s", device.duid, networking)
local_device_data = DeviceData(device, product_info[device.product_id].model, networking.ip)
local_client = RoborockLocalClientV1(local_device_data)
status = await local_client.get_status()
click.echo(json.dumps(status.as_dict(), indent=4))
@click.command()
@click.option("--device_id", required=True)
@click.option("--cmd", required=True)
@click.option("--params", required=False)
@click.pass_context
@run_sync()
async def command(ctx, cmd, device_id, params):
context: RoborockContext = await _load_and_discover(ctx)
cache_data = context.cache_data()
home_data = cache_data.home_data
devices = home_data.devices + home_data.received_devices
device = next(device for device in devices if device.duid == device_id)
model = next(
(product.model for product in home_data.products if device is not None and product.id == device.product_id),
None,
)
if model is None:
raise RoborockException(f"Could not find model for device {device.name}")
device_info = DeviceData(device=device, model=model)
mqtt_client = RoborockMqttClientV1(cache_data.user_data, device_info)
await mqtt_client.send_command(cmd, json.loads(params) if params is not None else None)
await mqtt_client.async_release()
@click.command()
@click.option("--local_key", required=True)
@click.option("--device_ip", required=True)
@click.option("--file", required=False)
@click.pass_context
@run_sync()
async def parser(_, local_key, device_ip, file):
file_provided = file is not None
if file_provided:
capture = FileCapture(file)
else:
_LOGGER.info("Listen for interface rvi0 since no file was provided")
capture = LiveCapture(interface="rvi0")
buffer = {"data": b""}
def on_package(packet: Packet):
if hasattr(packet, "ip"):
if packet.transport_layer == "TCP" and (packet.ip.dst == device_ip or packet.ip.src == device_ip):
if hasattr(packet, "DATA"):
if hasattr(packet.DATA, "data"):
if packet.ip.dst == device_ip:
try:
f, buffer["data"] = MessageParser.parse(
buffer["data"] + bytes.fromhex(packet.DATA.data),
local_key,
)
print(f"Received request: {f}")
except BaseException as e:
print(e)
pass
elif packet.ip.src == device_ip:
try:
f, buffer["data"] = MessageParser.parse(
buffer["data"] + bytes.fromhex(packet.DATA.data),
local_key,
)
print(f"Received response: {f}")
except BaseException as e:
print(e)
pass
try:
await capture.packets_from_tshark(on_package, close_tshark=not file_provided)
except UnknownInterfaceException:
raise RoborockException(
"You need to run 'rvictl -s XXXXXXXX-XXXXXXXXXXXXXXXX' first, with an iPhone connected to usb port"
)
cli.add_command(login)
cli.add_command(discover)
cli.add_command(list_devices)
cli.add_command(list_scenes)
cli.add_command(execute_scene)
cli.add_command(status)
cli.add_command(command)
cli.add_command(parser)
cli.add_command(session)
def main():
return cli()
if __name__ == "__main__":
main()