4242from pyshark .packet .packet import Packet # type: ignore
4343
4444from roborock import SHORT_MODEL_TO_ENUM , RoborockCommand
45- from roborock .data import CombinedMapInfo , DeviceData , HomeData , NetworkInfo , RoborockBase , UserData
45+ from roborock .data import DeviceData , RoborockBase , UserData
4646from roborock .device_features import DeviceFeatures
4747from roborock .devices .cache import Cache , CacheData
4848from roborock .devices .device import RoborockDevice
@@ -116,10 +116,8 @@ class ConnectionCache(RoborockBase):
116116
117117 user_data : UserData
118118 email : str
119- home_data : HomeData | None = None
120- network_info : dict [str , NetworkInfo ] | None = None
121- home_map_info : dict [int , CombinedMapInfo ] | None = None
122- trait_data : dict [str , Any ] | None = None
119+ # TODO: Used new APIs for cache file storage
120+ cache_data : CacheData | None = None
123121
124122
125123class DeviceConnectionManager :
@@ -134,10 +132,10 @@ def __init__(self, context: "RoborockContext", loop: asyncio.AbstractEventLoop |
134132 async def ensure_device_manager (self ) -> DeviceManager :
135133 """Ensure device manager is initialized."""
136134 if self .device_manager is None :
137- cache_data = self .context .cache_data ()
135+ connection_cache = self .context .connection_cache ()
138136 user_params = UserParams (
139- username = cache_data .email ,
140- user_data = cache_data .user_data ,
137+ username = connection_cache .email ,
138+ user_data = connection_cache .user_data ,
141139 )
142140 self .device_manager = await create_device_manager (user_params , cache = self .context )
143141 # Cache devices for quick lookup
@@ -164,7 +162,8 @@ class RoborockContext(Cache):
164162 """Context that handles both CLI and session modes internally."""
165163
166164 roborock_file = Path ("~/.roborock" ).expanduser ()
167- _cache_data : ConnectionCache | None = None
165+ roborock_cache_file = Path ("~/.roborock.cache" ).expanduser ()
166+ _connection_cache : ConnectionCache | None = None
168167
169168 def __init__ (self ):
170169 self .reload ()
@@ -177,22 +176,22 @@ def reload(self):
177176 with open (self .roborock_file ) as f :
178177 data = json .load (f )
179178 if data :
180- self ._cache_data = ConnectionCache .from_dict (data )
179+ self ._connection_cache = ConnectionCache .from_dict (data )
181180
182- def update (self , cache_data : ConnectionCache ):
183- data = json .dumps (cache_data .as_dict (), default = vars , indent = 4 )
181+ def update (self , connection_cache : ConnectionCache ):
182+ data = json .dumps (connection_cache .as_dict (), default = vars , indent = 4 )
184183 with open (self .roborock_file , "w" ) as f :
185184 f .write (data )
186185 self .reload ()
187186
188187 def validate (self ):
189- if self ._cache_data is None :
188+ if self ._connection_cache is None :
190189 raise RoborockException ("You must login first" )
191190
192- def cache_data (self ) -> ConnectionCache :
191+ def connection_cache (self ) -> ConnectionCache :
193192 """Get the cache data."""
194193 self .validate ()
195- return cast (ConnectionCache , self ._cache_data )
194+ return cast (ConnectionCache , self ._connection_cache )
196195
197196 def start_session_mode (self ):
198197 """Start session mode with a background event loop."""
@@ -229,19 +228,21 @@ async def get_device_manager(self) -> DeviceConnectionManager:
229228
230229 async def refresh_devices (self ) -> ConnectionCache :
231230 """Refresh device data from server (always fetches fresh data)."""
232- cache_data = self .cache_data ()
233- client = RoborockApiClient (cache_data .email )
234- home_data = await client .get_home_data_v3 (cache_data .user_data )
235- cache_data .home_data = home_data
236- self .update (cache_data )
237- return cache_data
231+ connection_cache = self .connection_cache ()
232+ client = RoborockApiClient (connection_cache .email )
233+ home_data = await client .get_home_data_v3 (connection_cache .user_data )
234+ if connection_cache .cache_data is None :
235+ connection_cache .cache_data = CacheData ()
236+ connection_cache .cache_data .home_data = home_data
237+ self .update (connection_cache )
238+ return connection_cache
238239
239240 async def get_devices (self ) -> ConnectionCache :
240241 """Get device data (uses cache if available, fetches if needed)."""
241- cache_data = self .cache_data ()
242- if not cache_data .home_data :
243- cache_data = await self .refresh_devices ()
244- return cache_data
242+ connection_cache = self .connection_cache ()
243+ if ( connection_cache . cache_data is None ) or ( connection_cache . cache_data .home_data is None ) :
244+ connection_cache = await self .refresh_devices ()
245+ return connection_cache
245246
246247 async def cleanup (self ):
247248 """Clean up resources (mainly for session mode)."""
@@ -266,22 +267,16 @@ def finish_session(self) -> None:
266267 async def get (self ) -> CacheData :
267268 """Get cached value."""
268269 _LOGGER .debug ("Getting cache data" )
269- connection_cache = self .cache_data ()
270- return CacheData (
271- home_data = connection_cache .home_data ,
272- network_info = connection_cache .network_info or {},
273- home_map_info = connection_cache .home_map_info ,
274- trait_data = connection_cache .trait_data or {},
275- )
270+ connection_cache = self .connection_cache ()
271+ if connection_cache .cache_data is not None :
272+ return connection_cache .cache_data
273+ return CacheData ()
276274
277275 async def set (self , value : CacheData ) -> None :
278276 """Set value in the cache."""
279277 _LOGGER .debug ("Setting cache data" )
280- connection_cache = self .cache_data ()
281- connection_cache .home_data = value .home_data
282- connection_cache .network_info = value .network_info
283- connection_cache .home_map_info = value .home_map_info
284- connection_cache .trait_data = value .trait_data
278+ connection_cache = self .connection_cache ()
279+ connection_cache .cache_data = value
285280 self .update (connection_cache )
286281
287282
@@ -367,9 +362,9 @@ async def discover(ctx):
367362@async_command
368363async def list_devices (ctx ):
369364 context : RoborockContext = ctx .obj
370- cache_data = await context .get_devices ()
365+ connection_cache = await context .get_devices ()
371366
372- home_data = cache_data .home_data
367+ home_data = connection_cache . cache_data .home_data
373368
374369 device_name_id = {device .name : device .duid for device in home_data .get_all_devices ()}
375370 click .echo (json .dumps (device_name_id , indent = 4 ))
0 commit comments