roborock.devices.traits.v1.map_content

Trait for fetching the map content from Roborock devices.

 1"""Trait for fetching the map content from Roborock devices."""
 2
 3import logging
 4from dataclasses import dataclass
 5
 6from vacuum_map_parser_base.map_data import MapData
 7
 8from roborock.data import RoborockBase
 9from roborock.devices.traits.v1 import common
10from roborock.map.map_parser import MapParser, MapParserConfig
11from roborock.roborock_typing import RoborockCommand
12
13_LOGGER = logging.getLogger(__name__)
14
15_TRUNCATE_LENGTH = 20
16
17
18@dataclass
19class MapContent(RoborockBase):
20    """Dataclass representing map content."""
21
22    image_content: bytes | None = None
23    """The rendered image of the map in PNG format."""
24
25    map_data: MapData | None = None
26    """The parsed map data which contains metadata for points on the map."""
27
28    raw_api_response: bytes | None = None
29    """The raw bytes of the map data from the API for caching for future use.
30
31    This should be treated as an opaque blob used only internally by this library
32    to re-parse the map data when needed.
33    """
34
35    def __repr__(self) -> str:
36        """Return a string representation of the MapContent."""
37        img = self.image_content
38        if self.image_content and len(self.image_content) > _TRUNCATE_LENGTH:
39            img = self.image_content[: _TRUNCATE_LENGTH - 3] + b"..."
40        return f"MapContent(image_content={img!r}, map_data={self.map_data!r})"
41
42
43@common.map_rpc_channel
44class MapContentTrait(MapContent, common.V1TraitMixin):
45    """Trait for fetching the map content."""
46
47    command = RoborockCommand.GET_MAP_V1
48
49    def __init__(self, map_parser_config: MapParserConfig | None = None) -> None:
50        """Initialize MapContentTrait."""
51        super().__init__()
52        self._map_parser = MapParser(map_parser_config or MapParserConfig())
53
54    def _parse_response(self, response: common.V1ResponseData) -> MapContent:
55        """Parse the response from the device into a MapContentTrait instance."""
56        if not isinstance(response, bytes):
57            raise ValueError(f"Unexpected MapContentTrait response format: {type(response)}")
58        return self.parse_map_content(response)
59
60    def parse_map_content(self, response: bytes) -> MapContent:
61        """Parse the map content from raw bytes.
62
63        This method is exposed so that cached map data can be parsed without
64        needing to go through the RPC channel.
65
66        Args:
67            response: The raw bytes of the map data from the API.
68
69        Returns:
70            MapContent: The parsed map content.
71
72        Raises:
73            RoborockException: If the map data cannot be parsed.
74        """
75        parsed_data = self._map_parser.parse(response)
76        if parsed_data is None:
77            raise ValueError("Failed to parse map data")
78
79        return MapContent(
80            image_content=parsed_data.image_content,
81            map_data=parsed_data.map_data,
82            raw_api_response=response,
83        )
@dataclass
class MapContent(roborock.data.containers.RoborockBase):
19@dataclass
20class MapContent(RoborockBase):
21    """Dataclass representing map content."""
22
23    image_content: bytes | None = None
24    """The rendered image of the map in PNG format."""
25
26    map_data: MapData | None = None
27    """The parsed map data which contains metadata for points on the map."""
28
29    raw_api_response: bytes | None = None
30    """The raw bytes of the map data from the API for caching for future use.
31
32    This should be treated as an opaque blob used only internally by this library
33    to re-parse the map data when needed.
34    """
35
36    def __repr__(self) -> str:
37        """Return a string representation of the MapContent."""
38        img = self.image_content
39        if self.image_content and len(self.image_content) > _TRUNCATE_LENGTH:
40            img = self.image_content[: _TRUNCATE_LENGTH - 3] + b"..."
41        return f"MapContent(image_content={img!r}, map_data={self.map_data!r})"

Dataclass representing map content.

MapContent( image_content: bytes | None = None, map_data: vacuum_map_parser_base.map_data.MapData | None = None, raw_api_response: bytes | None = None)
image_content: bytes | None = None

The rendered image of the map in PNG format.

map_data: vacuum_map_parser_base.map_data.MapData | None = None

The parsed map data which contains metadata for points on the map.

raw_api_response: bytes | None = None

The raw bytes of the map data from the API for caching for future use.

This should be treated as an opaque blob used only internally by this library to re-parse the map data when needed.