33import asyncio
44import enum
55import logging
6- from collections .abc import Awaitable , Callable
6+ from collections .abc import Callable
7+ from dataclasses import dataclass
78
89import aiohttp
910
1819from roborock .mqtt .roborock_session import create_lazy_mqtt_session
1920from roborock .mqtt .session import MqttSession
2021from roborock .protocol import create_mqtt_params
21- from roborock .web_api import RoborockApiClient
22+ from roborock .web_api import RoborockApiClient , UserWebApiClient
2223
2324from .cache import Cache , NoCache
2425from .channel import Channel
3031
3132__all__ = [
3233 "create_device_manager" ,
33- "create_home_data_api " ,
34+ "UserParams " ,
3435 "DeviceManager" ,
3536]
3637
3738
38- HomeDataApi = Callable [[], Awaitable [HomeData ]]
3939DeviceCreator = Callable [[HomeData , HomeDataDevice , HomeDataProduct ], RoborockDevice ]
4040
4141
@@ -53,7 +53,7 @@ class DeviceManager:
5353
5454 def __init__ (
5555 self ,
56- home_data_api : HomeDataApi ,
56+ web_api : UserWebApiClient ,
5757 device_creator : DeviceCreator ,
5858 mqtt_session : MqttSession ,
5959 cache : Cache ,
@@ -62,7 +62,7 @@ def __init__(
6262
6363 This takes ownership of the MQTT session and will close it when the manager is closed.
6464 """
65- self ._home_data_api = home_data_api
65+ self ._web_api = web_api
6666 self ._cache = cache
6767 self ._device_creator = device_creator
6868 self ._devices : dict [str , RoborockDevice ] = {}
@@ -73,7 +73,7 @@ async def discover_devices(self) -> list[RoborockDevice]:
7373 cache_data = await self ._cache .get ()
7474 if not cache_data .home_data :
7575 _LOGGER .debug ("No cached home data found, fetching from API" )
76- cache_data .home_data = await self ._home_data_api ()
76+ cache_data .home_data = await self ._web_api . get_home_data ()
7777 await self ._cache .set (cache_data )
7878 home_data = cache_data .home_data
7979
@@ -108,45 +108,69 @@ async def close(self) -> None:
108108 await asyncio .gather (* tasks )
109109
110110
111- def create_home_data_api (
112- email : str , user_data : UserData , base_url : str | None = None , session : aiohttp .ClientSession | None = None
113- ) -> HomeDataApi :
114- """Create a home data API wrapper.
111+ @dataclass
112+ class UserParams :
113+ """Parameters for creating a new session with Roborock devices.
115114
116- This function creates a wrapper around the Roborock API client to fetch
117- home data for the user.
115+ These parameters include the username, user data for authentication,
116+ and an optional base URL for the Roborock API. The `user_data` and `base_url`
117+ parameters are obtained from `RoborockApiClient` during the login process.
118118 """
119- # Note: This will auto discover the API base URL. This can be improved
120- # by caching this next to `UserData` if needed to avoid unnecessary API calls.
121- client = RoborockApiClient (username = email , base_url = base_url , session = session )
122119
123- return create_home_data_from_api_client (client , user_data )
120+ username : str
121+ """The username (email) used for logging in."""
122+
123+ user_data : UserData
124+ """This is the user data containing authentication information."""
125+
126+ base_url : str | None = None
127+ """Optional base URL for the Roborock API.
128+
129+ This is used to speed up connection times by avoiding the need to
130+ discover the API base URL each time. If not provided, the API client
131+ will attempt to discover it automatically which may take multiple requests.
132+ """
124133
125134
126- def create_home_data_from_api_client (client : RoborockApiClient , user_data : UserData ) -> HomeDataApi :
135+ def create_web_api_wrapper (
136+ user_params : UserParams ,
137+ * ,
138+ cache : Cache | None = None ,
139+ session : aiohttp .ClientSession | None = None ,
140+ ) -> UserWebApiClient :
127141 """Create a home data API wrapper from an existing API client."""
128142
129- async def home_data_api () -> HomeData :
130- return await client .get_home_data_v3 (user_data )
143+ # Note: This will auto discover the API base URL. This can be improved
144+ # by caching this next to `UserData` if needed to avoid unnecessary API calls.
145+ client = RoborockApiClient (username = user_params .username , base_url = user_params .base_url , session = session )
131146
132- return home_data_api
147+ return UserWebApiClient ( client , user_params . user_data )
133148
134149
135150async def create_device_manager (
136- user_data : UserData ,
137- home_data_api : HomeDataApi ,
151+ user_params : UserParams ,
152+ * ,
138153 cache : Cache | None = None ,
139154 map_parser_config : MapParserConfig | None = None ,
155+ session : aiohttp .ClientSession | None = None ,
140156) -> DeviceManager :
141157 """Convenience function to create and initialize a DeviceManager.
142158
143- The Home Data is fetched using the provided home_data_api callable which
144- is exposed this way to allow for swapping out other implementations to
145- include caching or other optimizations.
159+ Args:
160+ user_params: Parameters for creating the user session.
161+ cache: Optional cache implementation to use for caching device data.
162+ map_parser_config: Optional configuration for parsing maps.
163+ session: Optional aiohttp ClientSession to use for HTTP requests.
164+
165+ Returns:
166+ An initialized DeviceManager with discovered devices.
146167 """
147168 if cache is None :
148169 cache = NoCache ()
149170
171+ web_api = create_web_api_wrapper (user_params , session = session , cache = cache )
172+ user_data = user_params .user_data
173+
150174 mqtt_params = create_mqtt_params (user_data .rriot )
151175 mqtt_session = await create_lazy_mqtt_session (mqtt_params )
152176
@@ -176,6 +200,6 @@ def device_creator(home_data: HomeData, device: HomeDataDevice, product: HomeDat
176200 raise NotImplementedError (f"Device { device .name } has unsupported version { device .pv } " )
177201 return RoborockDevice (device , product , channel , trait )
178202
179- manager = DeviceManager (home_data_api , device_creator , mqtt_session = mqtt_session , cache = cache )
203+ manager = DeviceManager (web_api , device_creator , mqtt_session = mqtt_session , cache = cache )
180204 await manager .discover_devices ()
181205 return manager
0 commit comments