Skip to content

Commit ada5119

Browse files
author
Harry Coureau
committed
fix: keep Q10 archive traits lightweight
1 parent 93c2d7e commit ada5119

5 files changed

Lines changed: 52 additions & 75 deletions

File tree

‎roborock/devices/traits/b01/q10/__init__.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(
102102
self,
103103
channel: B01Q10Channel,
104104
*,
105-
map_parser_config: B01Q10MapParserConfig,
105+
map_parser_config: B01Q10MapParserConfig | None = None,
106106
) -> None:
107107
"""Initialize the B01Props API."""
108108
self._channel = channel
@@ -117,16 +117,13 @@ def __init__(
117117
self.network_info = NetworkInfoTrait()
118118
self.consumable = ConsumableTrait()
119119
self._map_dps = MapDpsTrait()
120-
self.maps = MapsTrait(self.command, map_parser_config=map_parser_config)
120+
self.maps = MapsTrait(self.command)
121121
self.map = MapContentTrait(
122122
self._map_dps,
123123
self.command,
124124
map_parser_config=map_parser_config,
125125
)
126-
self.clean_history = CleanHistoryTrait(
127-
self.command,
128-
map_parser_config=map_parser_config,
129-
)
126+
self.clean_history = CleanHistoryTrait(self.command)
130127
self.vacuum = VacuumTrait(self.command, self.status, self.map)
131128
# Read-model traits updated from the device's DPS push stream.
132129
self._updatable_traits = [
@@ -210,5 +207,5 @@ def create(
210207
"""Create traits for B01 devices."""
211208
return Q10PropertiesApi(
212209
channel,
213-
map_parser_config=map_parser_config or B01Q10MapParserConfig(),
210+
map_parser_config=map_parser_config,
214211
)

‎roborock/devices/traits/b01/q10/clean_history.py‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525
from roborock.data.b01_q10.b01_q10_containers import Q10CleanRecord
2626
from roborock.exceptions import RoborockException
2727
from roborock.map.b01_q10_map_parser import (
28-
B01Q10MapParserConfig,
2928
Q10CleanRecordDetail,
3029
Q10HistoricalTracePacket,
3130
Q10MapPacket,
3231
Q10MapPacketKind,
3332
Q10Point,
3433
)
35-
from roborock.map.b01_q10_render import Q10MapOverlays, render_q10_map
3634

3735
from .command import CommandTrait
3836
from .common import UpdatableTrait
@@ -127,9 +125,6 @@ class CleanHistory(RoborockBase):
127125
"""Decoded clean records, most recent first."""
128126
detail: Q10CleanRecordDetail | None = None
129127
"""Latest received archive. Its wire payload contains no record identifier."""
130-
detail_image_content: bytes | None = None
131-
"""Rendered archive PNG, available separately from diagnostic serialization."""
132-
133128
detail_path: list[Q10Point] = field(default_factory=list)
134129
"""Historical path points from the latest received archive, included in diagnostics."""
135130

@@ -149,12 +144,12 @@ def detail_trace(self) -> Q10HistoricalTracePacket | None:
149144
return self.detail.trace if self.detail else None
150145

151146
def as_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
152-
"""Serialize history metadata and path without map grids or PNG bytes."""
153-
return super().as_dict(exclude=(exclude or set()) | {"detail", "detail_image_content"})
147+
"""Serialize history metadata and path without binary map packets and grids."""
148+
return super().as_dict(exclude=(exclude or set()) | {"detail"})
154149

155150

156151
class CleanHistoryTrait(CleanHistory, UpdatableTrait):
157-
"""Show past cleaning runs with their saved map image and historical path.
152+
"""Browse past cleaning runs and retrieve their parsed maps and historical paths.
158153
159154
Subscribe with ``add_update_listener`` to receive requested or unsolicited
160155
archives. Archive pushes contain no record ID, so the latest detail cannot
@@ -163,12 +158,11 @@ class CleanHistoryTrait(CleanHistory, UpdatableTrait):
163158

164159
_command: CommandTrait
165160

166-
def __init__(self, command: CommandTrait, *, map_parser_config: B01Q10MapParserConfig) -> None:
161+
def __init__(self, command: CommandTrait) -> None:
167162
CleanHistory.__init__(self)
168163
UpdatableTrait.__init__(self, command, _LOGGER)
169164
self._command = command
170165
self._converter = CleanRecordConverter()
171-
self._map_parser_config = map_parser_config
172166

173167
async def refresh(self) -> None:
174168
"""Request the clean-record list from the device.
@@ -206,21 +200,11 @@ def update_from_dps(self, decoded_dps: dict[B01_Q10_DP, Any]) -> None:
206200
self._apply(push)
207201

208202
def update_from_detail(self, detail: Q10CleanRecordDetail) -> None:
209-
"""Store and render a pushed clean-record detail map."""
203+
"""Store a pushed clean-record map and path and notify listeners."""
210204
if detail.map.kind is not Q10MapPacketKind.CLEAN_RECORD_DETAIL:
211205
raise ValueError(f"Expected a Q10 clean-record detail packet, got {detail.map.kind.value}")
212206
self.detail = detail
213207
self.detail_path = list(detail.trace.points) if detail.trace else []
214-
try:
215-
self.detail_image_content = render_q10_map(
216-
detail.map,
217-
detail.trace,
218-
Q10MapOverlays(),
219-
config=self._map_parser_config,
220-
)
221-
except RoborockException:
222-
_LOGGER.debug("Failed to render Q10 clean-record detail", exc_info=True)
223-
self.detail_image_content = None
224208
self._notify_update()
225209

226210
def _apply(self, push: CleanRecordPush) -> None:

‎roborock/devices/traits/b01/q10/maps.py‎

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from roborock.data.b01_q10.b01_q10_containers import Q10MapInfo, dpMultiMap
1010
from roborock.devices.traits.common import DpsDataConverter
1111
from roborock.exceptions import RoborockException
12-
from roborock.map.b01_q10_map_parser import B01Q10MapParserConfig, Q10MapPacket, Q10MapPacketKind
13-
from roborock.map.b01_q10_render import Q10MapOverlays, render_q10_map
12+
from roborock.map.b01_q10_map_parser import Q10MapPacket, Q10MapPacketKind
1413

1514
from .command import CommandTrait
1615
from .common import UpdatableTrait
@@ -28,12 +27,10 @@ class Maps(RoborockBase):
2827
"""Most recently received saved-map preview."""
2928
detail_map_id: str | None = None
3029
"""Map ID supplied by the preview packet itself."""
31-
detail_image_content: bytes | None = None
32-
"""Rendered preview PNG, excluded from diagnostic serialization."""
3330

3431
def as_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
35-
"""Serialize metadata without binary map grids or PNG bytes."""
36-
return super().as_dict(exclude=(exclude or set()) | {"detail_packet", "detail_image_content"})
32+
"""Serialize metadata without binary map packets and grids."""
33+
return super().as_dict(exclude=(exclude or set()) | {"detail_packet"})
3734

3835
@property
3936
def current_map_id(self) -> str | None:
@@ -51,7 +48,7 @@ def map_list(self) -> list[Q10MapInfo]:
5148

5249

5350
class MapsTrait(Maps, UpdatableTrait):
54-
"""Browse saved maps and display their previews independently of the live map.
51+
"""Browse saved maps and retrieve their parsed previews independently of the live map.
5552
5653
Preview pushes identify their map, but carry no request correlation ID.
5754
Consumers receive them through ``add_update_listener``.
@@ -60,17 +57,11 @@ class MapsTrait(Maps, UpdatableTrait):
6057
_CONVERTER = DpsDataConverter.from_dataclass(Maps)
6158
_command: CommandTrait
6259

63-
def __init__(
64-
self,
65-
command: CommandTrait,
66-
*,
67-
map_parser_config: B01Q10MapParserConfig,
68-
) -> None:
60+
def __init__(self, command: CommandTrait) -> None:
6961
"""Initialize the saved-map list trait."""
7062
Maps.__init__(self)
7163
UpdatableTrait.__init__(self, command, _LOGGER)
7264
self._command = command
73-
self._map_parser_config = map_parser_config
7465

7566
async def refresh(self) -> None:
7667
"""Request a new saved-map list from the device."""
@@ -121,19 +112,9 @@ def update_from_dps(self, decoded_dps: dict[B01_Q10_DP, Any]) -> None:
121112
super().update_from_dps(decoded_dps)
122113

123114
def update_from_map_packet(self, packet: Q10MapPacket) -> None:
124-
"""Store and render a pushed saved-map detail packet."""
115+
"""Store a pushed saved-map detail packet and notify listeners."""
125116
if packet.kind is not Q10MapPacketKind.SAVED_MAP_DETAIL:
126117
raise ValueError(f"Expected a Q10 saved-map detail packet, got {packet.kind.value}")
127118
self.detail_map_id = str(packet.map_id)
128119
self.detail_packet = packet
129-
try:
130-
self.detail_image_content = render_q10_map(
131-
packet,
132-
None,
133-
Q10MapOverlays(),
134-
config=self._map_parser_config,
135-
)
136-
except RoborockException:
137-
_LOGGER.debug("Failed to render Q10 saved-map detail", exc_info=True)
138-
self.detail_image_content = None
139120
self._notify_update()

‎tests/devices/traits/b01/q10/test_clean_history.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ def test_received_detail_is_serializable_and_path_is_independent(q10_api: Q10Pro
215215
detail = _detail()
216216
detail.trace = Q10HistoricalTracePacket(points=[Q10Point(x=1, y=2)], heading=0)
217217
history.update_from_detail(detail)
218+
assert history.detail is detail
219+
assert history.detail_packet is detail.map
220+
assert history.detail_trace is detail.trace
218221
detail.trace.points.clear()
219222
assert history.detail_path == [Q10Point(x=1, y=2)]
220223
assert isinstance(history, CleanHistory)
@@ -227,6 +230,18 @@ def test_received_detail_is_serializable_and_path_is_independent(q10_api: Q10Pro
227230
assert history.as_dict(exclude={"records", "detail_path"}) == {}
228231

229232

233+
def test_received_detail_without_trace_clears_previous_path(clean_history: CleanHistoryTrait) -> None:
234+
first = _detail()
235+
first.trace = Q10HistoricalTracePacket(points=[Q10Point(x=1, y=2)], heading=0)
236+
clean_history.update_from_detail(first)
237+
assert clean_history.detail_path == [Q10Point(x=1, y=2)]
238+
second = _detail()
239+
clean_history.update_from_detail(second)
240+
assert clean_history.detail is second
241+
assert clean_history.detail_trace is None
242+
assert clean_history.detail_path == []
243+
244+
230245
def test_unsolicited_and_repeated_details_notify_listeners(clean_history: CleanHistoryTrait) -> None:
231246
updates = []
232247
unsubscribe = clean_history.add_update_listener(lambda: updates.append(clean_history.detail))

‎tests/devices/traits/b01/q10/test_map.py‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,11 @@ async def test_archived_map_pushes_cannot_overwrite_live_map(
805805
assert q10_api.map.robot_position == live_position
806806
assert q10_api.map.robot_heading == live_heading
807807
assert live_updates == []
808-
assert q10_api.clean_history.detail_packet is not None
808+
assert q10_api.clean_history.detail is clean_record
809+
assert q10_api.clean_history.detail_packet is clean_record.map
809810
assert q10_api.clean_history.detail_packet.kind is Q10MapPacketKind.CLEAN_RECORD_DETAIL
810-
assert q10_api.clean_history.detail_image_content is not None
811-
assert q10_api.maps.detail_packet is not None
811+
assert q10_api.maps.detail_packet is saved_map
812812
assert q10_api.maps.detail_packet.kind is Q10MapPacketKind.SAVED_MAP_DETAIL
813-
assert q10_api.maps.detail_image_content is not None
814813
assert clean_record_updates == [None]
815814
assert saved_map_updates == [None]
816815

@@ -826,25 +825,30 @@ def test_archive_owners_reject_wrong_packet_kinds(q10_api: Q10PropertiesApi) ->
826825

827826

828827
@pytest.mark.parametrize("scale", [1, 2, 4])
829-
async def test_all_q10_map_views_render_at_requested_scale(scale: int, fake_channel: FakeB01Q10Channel) -> None:
828+
async def test_live_q10_map_renders_at_requested_scale(scale: int, fake_channel: FakeB01Q10Channel) -> None:
830829
api = create(fake_channel, map_parser_config=B01Q10MapParserConfig(map_scale=scale))
831830
payload = FIXTURE.read_bytes()
832831
fake_channel.messages_to_stream = [
833832
parse_map_packet(payload),
834-
parse_clean_record_detail(b"\x03\x01" + payload[2:]),
835-
parse_map_packet(b"\x04\x01" + payload[2:]),
836833
]
837834
await api.start()
838835
try:
839-
await _wait_for(lambda: api.maps.detail_image_content is not None)
840-
for content in [api.map.image_content, api.clean_history.detail_image_content, api.maps.detail_image_content]:
841-
assert content is not None
842-
with Image.open(io.BytesIO(content)) as image:
843-
assert image.size == (8 * scale, 6 * scale)
836+
await _wait_for(lambda: api.map.image_content is not None)
837+
assert api.map.image_content is not None
838+
with Image.open(io.BytesIO(api.map.image_content)) as image:
839+
assert image.size == (8 * scale, 6 * scale)
844840
finally:
845841
await api.close()
846842

847843

844+
def test_q10_properties_constructor_uses_default_live_map_config(fake_channel: FakeB01Q10Channel) -> None:
845+
api = Q10PropertiesApi(fake_channel)
846+
api.map.update_from_map_packet(parse_map_packet(FIXTURE.read_bytes()))
847+
assert api.map.image_content is not None
848+
with Image.open(io.BytesIO(api.map.image_content)) as image:
849+
assert image.size == (32, 24)
850+
851+
848852
@pytest.mark.parametrize("selected,expected", [(None, "12345"), ("67890", "67890")])
849853
async def test_saved_map_detail_refresh_publishes_selection(
850854
q10_api: Q10PropertiesApi,
@@ -885,7 +889,6 @@ async def test_saved_map_details_publish_packet_map_id_and_notify(
885889
await _wait_for(lambda: bool(updates))
886890
assert updates == [str(packet.map_id)]
887891
assert q10_api.maps.detail_packet is packet
888-
assert q10_api.maps.detail_image_content is not None
889892
message_queue.put_nowait(replace(packet, map_id=999))
890893
await _wait_for(lambda: len(updates) == 2)
891894
assert updates == [str(packet.map_id), "999"]
@@ -903,7 +906,7 @@ def test_saved_map_diagnostics_exclude_binary_content(q10_api: Q10PropertiesApi)
903906
q10_api.maps.update_from_map_packet(packet)
904907
data = q10_api.as_dict()["maps"]
905908
assert data == {"detailMapId": str(packet.map_id)}
906-
assert q10_api.maps.detail_image_content is not None
909+
assert q10_api.maps.detail_packet is packet
907910
assert q10_api.maps.as_dict(exclude={"detail_map_id"}) == {}
908911
json.dumps(data)
909912

@@ -946,7 +949,7 @@ def predicate() -> bool:
946949

947950

948951
@pytest.mark.parametrize("drawables,expected_base", [([], True), ([Drawable.CHARGER], False)])
949-
async def test_all_q10_map_views_honor_drawable_selection(
952+
async def test_live_q10_map_honors_drawable_selection(
950953
drawables: list[Drawable], expected_base: bool, fake_channel: FakeB01Q10Channel
951954
) -> None:
952955
api = create(fake_channel, map_parser_config=B01Q10MapParserConfig(drawables=drawables))
@@ -964,15 +967,12 @@ async def test_all_q10_map_views_honor_drawable_selection(
964967
)
965968
fake_channel.messages_to_stream = [
966969
packet,
967-
Q10CleanRecordDetail(map=replace(packet, kind=Q10MapPacketKind.CLEAN_RECORD_DETAIL)),
968-
replace(packet, kind=Q10MapPacketKind.SAVED_MAP_DETAIL),
969970
]
970971
await api.start()
971972
try:
972-
await _wait_for(lambda: api.maps.detail_image_content is not None)
973-
for content in [api.map.image_content, api.clean_history.detail_image_content, api.maps.detail_image_content]:
974-
assert content is not None
975-
with Image.open(io.BytesIO(content)) as image:
976-
assert (image.tobytes() == base_pixels) is expected_base
973+
await _wait_for(lambda: api.map.image_content is not None)
974+
assert api.map.image_content is not None
975+
with Image.open(io.BytesIO(api.map.image_content)) as image:
976+
assert (image.tobytes() == base_pixels) is expected_base
977977
finally:
978978
await api.close()

0 commit comments

Comments
 (0)