|
| 1 | +"""Trait for managing maps and room mappings on Roborock devices. |
| 2 | +
|
| 3 | +New datatypes are introduced here to manage the additional information associated |
| 4 | +with maps and rooms, such as map names and room names. These override the |
| 5 | +base container datatypes to add additional fields. |
| 6 | +""" |
| 7 | +import logging |
| 8 | +from typing import Self |
| 9 | + |
| 10 | +from roborock.containers import MultiMapsList, MultiMapsListMapInfo |
| 11 | +from roborock.devices.traits.v1 import common |
| 12 | +from roborock.roborock_typing import RoborockCommand |
| 13 | + |
| 14 | +from .status import StatusTrait |
| 15 | + |
| 16 | +_LOGGER = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +@common.mqtt_rpc_channel |
| 20 | +class MapsTrait(MultiMapsList, common.V1TraitMixin): |
| 21 | + """Trait for managing the maps of Roborock devices. |
| 22 | +
|
| 23 | + A device may have multiple maps, each identified by a unique map_flag. |
| 24 | + Each map can have multiple rooms associated with it, in a `RoomMapping`. |
| 25 | + """ |
| 26 | + |
| 27 | + command = RoborockCommand.GET_MULTI_MAPS_LIST |
| 28 | + |
| 29 | + def __init__(self, status_trait: StatusTrait) -> None: |
| 30 | + """Initialize the MapsTrait. |
| 31 | +
|
| 32 | + We keep track of the StatusTrait to ensure we have the latest |
| 33 | + status information when dealing with maps. |
| 34 | + """ |
| 35 | + super().__init__() |
| 36 | + self._status_trait = status_trait |
| 37 | + |
| 38 | + @property |
| 39 | + def current_map(self) -> int | None: |
| 40 | + """Returns the currently active map (map_flag), if available.""" |
| 41 | + return self._status_trait.current_map |
| 42 | + |
| 43 | + @property |
| 44 | + def current_map_info(self) -> MultiMapsListMapInfo | None: |
| 45 | + """Returns the currently active map info, if available.""" |
| 46 | + if (current_map := self.current_map) is None or self.map_info is None: |
| 47 | + return None |
| 48 | + for map_info in self.map_info: |
| 49 | + if map_info.map_flag == current_map: |
| 50 | + return map_info |
| 51 | + return None |
| 52 | + |
| 53 | + async def set_current_map(self, map_flag: int) -> None: |
| 54 | + """Update the current map of the device by it's map_flag id.""" |
| 55 | + await self.rpc_channel.send_command(RoborockCommand.LOAD_MULTI_MAP, params=[map_flag]) |
| 56 | + # Refresh our status to make sure it reflects the new map |
| 57 | + await self._status_trait.refresh() |
| 58 | + |
| 59 | + def _parse_response(self, response: common.V1ResponseData) -> Self: |
| 60 | + """Parse the response from the device into a MapsTrait instance. |
| 61 | +
|
| 62 | + This overrides the base implementation to handle the specific |
| 63 | + response format for the multi maps list. This is needed because we have |
| 64 | + a custom constructor that requires the StatusTrait. |
| 65 | + """ |
| 66 | + if not isinstance(response, list): |
| 67 | + raise ValueError(f"Unexpected MapsTrait response format: {response!r}") |
| 68 | + response = response[0] |
| 69 | + if not isinstance(response, dict): |
| 70 | + raise ValueError(f"Unexpected MapsTrait response format: {response!r}") |
| 71 | + return MultiMapsList.from_dict(response) |
0 commit comments