1313import logging
1414from typing import Self
1515
16+ from roborock .code_mappings import RoborockStateCode
1617from roborock .containers import CombinedMapInfo , RoborockBase
1718from roborock .devices .cache import Cache
1819from roborock .devices .traits .v1 import common
19- from roborock .exceptions import RoborockException
20+ from roborock .exceptions import RoborockDeviceBusy , RoborockException
2021from roborock .roborock_typing import RoborockCommand
2122
2223from .maps import MapsTrait
2324from .rooms import RoomsTrait
25+ from .status import StatusTrait
2426
2527_LOGGER = logging .getLogger (__name__ )
2628
@@ -32,7 +34,13 @@ class HomeTrait(RoborockBase, common.V1TraitMixin):
3234
3335 command = RoborockCommand .GET_MAP_V1 # This is not used
3436
35- def __init__ (self , maps_trait : MapsTrait , rooms_trait : RoomsTrait , cache : Cache ) -> None :
37+ def __init__ (
38+ self ,
39+ status_trait : StatusTrait ,
40+ maps_trait : MapsTrait ,
41+ rooms_trait : RoomsTrait ,
42+ cache : Cache ,
43+ ) -> None :
3644 """Initialize the HomeTrait.
3745
3846 We keep track of the MapsTrait and RoomsTrait to provide a comprehensive
@@ -49,6 +57,7 @@ def __init__(self, maps_trait: MapsTrait, rooms_trait: RoomsTrait, cache: Cache)
4957 accuracy.
5058 """
5159 super ().__init__ ()
60+ self ._status_trait = status_trait
5261 self ._maps_trait = maps_trait
5362 self ._rooms_trait = rooms_trait
5463 self ._cache = cache
@@ -58,21 +67,29 @@ async def discover_home(self) -> None:
5867 """Iterate through all maps to discover rooms and cache them.
5968
6069 This will be a no-op if the home cache is already populated.
70+
71+ This cannot be called while the device is cleaning, as that would interrupt the
72+ cleaning process. This will raise `RoborockDeviceBusy` if the device is
73+ currently cleaning.
74+
75+ After discovery, the home cache will be populated and can be accessed via the `home_cache` property.
6176 """
6277 cache_data = await self ._cache .get ()
6378 if cache_data .home_cache :
6479 _LOGGER .debug ("Home cache already populated, skipping discovery" )
6580 self ._home_cache = cache_data .home_cache
6681 return
6782
83+ if self ._status_trait .state == RoborockStateCode .cleaning :
84+ raise RoborockDeviceBusy ("Cannot perform home discovery while the device is cleaning" )
85+
6886 await self ._maps_trait .refresh ()
6987 if self ._maps_trait .current_map_info is None :
7088 raise RoborockException ("Cannot perform home discovery without current map info" )
7189
7290 home_cache = await self ._build_home_cache ()
7391 _LOGGER .debug ("Home discovery complete, caching data for %d maps" , len (home_cache ))
7492 await self ._update_home_cache (home_cache )
75- self ._home_cache = home_cache
7693
7794 async def _refresh_map_data (self , map_info ) -> CombinedMapInfo :
7895 """Collect room data for a specific map and return CombinedMapInfo."""
@@ -129,8 +146,7 @@ async def refresh(self) -> Self:
129146 if current_map_data :
130147 map_data = await self ._refresh_map_data (current_map_info )
131148 if map_data != current_map_data :
132- self ._home_cache [map_flag ] = map_data
133- await self ._update_home_cache (self ._home_cache )
149+ await self ._update_home_cache ({** self ._home_cache , map_flag : map_data })
134150
135151 return self
136152
@@ -155,3 +171,4 @@ async def _update_home_cache(self, home_cache: dict[int, CombinedMapInfo]) -> No
155171 cache_data = await self ._cache .get ()
156172 cache_data .home_cache = home_cache
157173 await self ._cache .set (cache_data )
174+ self ._home_cache = home_cache
0 commit comments