Skip to content

Commit dfd93fc

Browse files
authored
fix: remove v1 image parser monkey patch (#949)
Reverts the V1 image parser monkey-patch introduced in #902. Fixes home-assistant/core#181447
1 parent 2ffc17b commit dfd93fc

2 files changed

Lines changed: 2 additions & 99 deletions

File tree

roborock/map/map_parser.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io
44
import logging
5-
import threading
65
from dataclasses import dataclass, field
76

87
from vacuum_map_parser_base.config.color import Color, ColorsPalette, SupportedColor
@@ -11,13 +10,10 @@
1110
from vacuum_map_parser_base.config.size import Size, Sizes
1211
from vacuum_map_parser_base.image_generator import ImageGenerator
1312
from vacuum_map_parser_base.map_data import MapData
14-
from vacuum_map_parser_roborock.image_parser import RoborockImageParser
1513
from vacuum_map_parser_roborock.map_data_parser import RoborockMapDataParser
1614

1715
from roborock.exceptions import RoborockException
1816

19-
from .room_colors import adjacency_aware_room_colors
20-
2117
_LOGGER = logging.getLogger(__name__)
2218

2319
DEFAULT_DRAWABLES = {
@@ -103,68 +99,16 @@ def parse(self, map_bytes: bytes) -> ParsedMapData | None:
10399
return ParsedMapData(image_content=img_byte_arr.getvalue(), map_data=parsed_map)
104100

105101

106-
class _AdjacencyAwareRoborockImageParser(RoborockImageParser):
107-
"""Apply the shared adjacency color policy to V1 room cells."""
108-
109-
def __init__(
110-
self,
111-
palette: ColorsPalette,
112-
image_config: ImageConfig,
113-
*,
114-
recolor_rooms: bool = True,
115-
) -> None:
116-
super().__init__(palette, image_config)
117-
self._room_palette = palette
118-
self._base_room_colors = palette.cached_room_colors.copy()
119-
self._recolor_rooms = recolor_rooms
120-
self._palette_lock = threading.Lock()
121-
122-
def parse(
123-
self,
124-
raw_data: bytes,
125-
width: int,
126-
height: int,
127-
carpet_map: set[int] | None,
128-
removed_map: set[int] | None = None,
129-
):
130-
"""Assign non-conflicting room colors before the V1 image pass."""
131-
with self._palette_lock:
132-
# cached_room_colors is a read-only property, so reset its dict in place.
133-
cached_room_colors = self._room_palette.cached_room_colors
134-
cached_room_colors.clear()
135-
cached_room_colors.update(self._base_room_colors)
136-
137-
if self._recolor_rooms:
138-
139-
def room_id(value: int) -> int | None:
140-
if value in (self.MAP_OUTSIDE, self.MAP_WALL, self.MAP_INSIDE, self.MAP_SCAN):
141-
return None
142-
return self._get_room_number(value) if value & 0x07 == 0x07 else None
143-
144-
room_colors = adjacency_aware_room_colors(raw_data, width, self._room_palette, room_id)
145-
for number, color in room_colors.items():
146-
# ColorsPalette caches both forms for get_room_color(str | int).
147-
cached_room_colors[number] = color
148-
cached_room_colors[str(number)] = color
149-
return super().parse(raw_data, width, height, carpet_map, removed_map)
150-
151-
152102
def _create_map_data_parser(config: MapParserConfig) -> RoborockMapDataParser:
153103
"""Create a RoborockMapDataParser based on the config entry."""
154104
palette, sizes, image_config = _create_rendering_components(config)
155-
parser = RoborockMapDataParser(
105+
return RoborockMapDataParser(
156106
palette,
157107
sizes,
158108
config.drawables,
159109
image_config,
160110
[],
161111
)
162-
parser._image_parser = _AdjacencyAwareRoborockImageParser(
163-
palette,
164-
image_config,
165-
recolor_rooms=config.show_rooms,
166-
)
167-
return parser
168112

169113

170114
def _create_image_generator(

tests/map/test_map_parser.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
from pathlib import Path
44

55
import pytest
6-
from vacuum_map_parser_base.config.color import ColorsPalette
7-
from vacuum_map_parser_base.config.image_config import ImageConfig
86

97
from roborock.exceptions import RoborockException
10-
from roborock.map.map_parser import (
11-
MapParser,
12-
MapParserConfig,
13-
_AdjacencyAwareRoborockImageParser,
14-
)
8+
from roborock.map.map_parser import MapParser, MapParserConfig
159

1610
MAP_DATA_FILE = Path(__file__).parent / "raw_map_data"
1711
DEFAULT_MAP_CONFIG = MapParserConfig()
@@ -25,39 +19,4 @@ def test_invalid_map_content(map_content: bytes):
2519
parser.parse(map_content)
2620

2721

28-
def test_v1_parser_gives_adjacent_rooms_distinct_palette_colors() -> None:
29-
"""Repeated palette entries do not merge neighboring V1 rooms."""
30-
palette = ColorsPalette()
31-
original_room_12 = palette.get_room_color(12)
32-
image_parser = _AdjacencyAwareRoborockImageParser(palette, ImageConfig())
33-
raw_data = bytes([(2 << 3) | 7, (12 << 3) | 7])
34-
35-
image, _rooms = image_parser.parse(raw_data, 2, 1, None)
36-
37-
assert image is not None
38-
assert image.getpixel((0, 0)) != image.getpixel((1, 0))
39-
assert palette.get_room_color(12) == palette.get_room_color("12")
40-
41-
isolated_image, _rooms = image_parser.parse(bytes([(12 << 3) | 7]), 1, 1, None)
42-
43-
assert isolated_image is not None
44-
assert isolated_image.getpixel((0, 0))[: len(original_room_12)] == original_room_12
45-
46-
47-
def test_v1_parser_keeps_adjacent_rooms_hidden_when_rooms_disabled() -> None:
48-
"""Adjacency conflict handling cannot override intentional transparency."""
49-
hidden_rooms = {str(room_id): (0, 0, 0, 0) for room_id in range(1, 32)}
50-
parser = _AdjacencyAwareRoborockImageParser(
51-
ColorsPalette({}, hidden_rooms),
52-
ImageConfig(),
53-
recolor_rooms=False,
54-
)
55-
raw_data = bytes([(2 << 3) | 7, (12 << 3) | 7])
56-
57-
image, _rooms = parser.parse(raw_data, 2, 1, None)
58-
59-
assert image is not None
60-
assert [image.getpixel((x, 0)) for x in range(2)] == [(0, 0, 0, 0)] * 2
61-
62-
6322
# We can add additional tests here in the future that actually parse valid map data

0 commit comments

Comments
 (0)