Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathexample.py
More file actions
Latest commit
85 lines (65 loc) · 2.76 KB
/
Copy pathexample.py
File metadata and controls
85 lines (65 loc) · 2.76 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
"""Example script demonstrating how to connect to Roborock devices and print their status."""
importasyncio
importdataclasses
importjson
importpathlib
fromtypingimportAny
fromroborock.devices.device_managerimportUserParams, create_device_manager
fromroborock.devices.file_cacheimportFileCache, load_value, store_value
fromroborock.web_apiimportRoborockApiClient
# We typically store the login credentials/information separately from other cached data.
USER_PARAMS_PATH=pathlib.Path.home() /".cache"/"roborock-user-params.pkl"
# Device connection information is cached to speed up future connections.
CACHE_PATH=pathlib.Path.home() /".cache"/"roborock-cache-data.pkl"
asyncdeflogin_flow() ->UserParams:
"""Perform the login flow to obtain UserData from the web API."""
username=input("Email: ")
web_api=RoborockApiClient(username=username)
print("Requesting login code sent to email...")
awaitweb_api.request_code()
code=input("Code: ")
user_data=awaitweb_api.code_login(code)
# We store the base_url to avoid future discovery calls.
base_url=awaitweb_api.base_url
returnUserParams(
username=username,
user_data=user_data,
base_url=base_url,
)
asyncdefget_or_create_session() ->UserParams:
"""Initialize the session by logging in if necessary."""
user_params=awaitload_value(USER_PARAMS_PATH)
ifuser_paramsisNone:
print("No cached login data found, please login.")
user_params=awaitlogin_flow()
print("Login successful, caching login data...")
awaitstore_value(USER_PARAMS_PATH, user_params)
print(f"Cached login data to {USER_PARAMS_PATH}.")
returnuser_params
defremove_none_values(data: dict[str, Any]) ->dict[str, Any]:
return {k: vfork, vindata.items() ifvisnotNone}
asyncdefmain():
user_params=awaitget_or_create_session()
cache=FileCache(CACHE_PATH)
# Create a device manager that can discover devices.
device_manager=awaitcreate_device_manager(user_params, cache=cache)
devices=awaitdevice_manager.get_devices()
# Get all vacuum devices that support the v1 PropertiesApi
device_results= []
fordeviceindevices:
ifnotdevice.v1_properties:
continue
# Refresh the current device status
status_trait=device.v1_properties.status
awaitstatus_trait.refresh()
# Print the device status as JSON
device_results.append(
{
"device": device.name,
"status": remove_none_values(dataclasses.asdict(status_trait)),
}
)
print(json.dumps(device_results, indent=2))
awaitcache.flush()
if__name__=="__main__":
asyncio.run(main())