Skip to content

Commit 2c351ba

Browse files
committed
fix: camelize nested child keys in map trait serialization
1 parent c2e4b93 commit 2c351ba

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,25 @@ def _render(self) -> None:
161161

162162
def as_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
163163
"""Return the trait data as a dictionary, excluding large binary data."""
164+
from enum import Enum
165+
166+
from roborock.data.containers import _camelize
167+
168+
def _to_camel_dict(obj: Any) -> Any:
169+
return dataclasses.asdict(
170+
obj,
171+
dict_factory=lambda _fields: {
172+
_camelize(key): value.value if isinstance(value, Enum) else value
173+
for (key, value) in _fields
174+
if value is not None
175+
},
176+
)
177+
164178
exclude_set = exclude or set()
165179
data = {
166-
"rooms": [dataclasses.asdict(room) for room in self.rooms],
167-
"path": [dataclasses.asdict(point) for point in self.path],
168-
"robotPosition": dataclasses.asdict(self.robot_position) if self.robot_position is not None else None,
180+
"rooms": [_to_camel_dict(room) for room in self.rooms],
181+
"path": [_to_camel_dict(point) for point in self.path],
182+
"robotPosition": _to_camel_dict(self.robot_position) if self.robot_position is not None else None,
169183
"robotHeading": self.robot_heading,
170184
}
171185
for key in exclude_set:

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from roborock.exceptions import RoborockException
2525
from roborock.map.b01_q10_map_parser import (
2626
Q10Point,
27+
Q10Room,
2728
Q10TracePacket,
2829
parse_map_packet,
2930
parse_trace_packet,
@@ -410,3 +411,26 @@ async def test_combined_status_and_overlay_update_renders_once(render_map: Mock)
410411
assert len(render_map.call_args.args[2].zones) == 1
411412
assert render_map.call_args.kwargs["robot_at_dock"] is True
412413
assert trait.image_content == b"combined map"
414+
415+
416+
def test_map_content_trait_as_dict_camelizes_child_keys() -> None:
417+
"""MapContentTrait.as_dict() camelizes nested child keys (e.g. rawName, pixelValue)."""
418+
from roborock.map.b01_q10_map_parser import Q10MapPacket
419+
420+
trait = _map_trait()
421+
trait._map_packet = Q10MapPacket(
422+
map_id=1,
423+
width=100,
424+
height=100,
425+
grid=b"",
426+
rooms=[Q10Room(id=1, raw_name="living_room", pixel_value=12, pixel_count=400)],
427+
)
428+
trait._trace_packet = Q10TracePacket(
429+
points=[Q10Point(x=100, y=200), Q10Point(x=150, y=250)],
430+
sequence=0,
431+
)
432+
433+
data = trait.as_dict()
434+
assert data["rooms"] == [{"id": 1, "rawName": "living_room", "pixelValue": 12, "pixelCount": 400}]
435+
assert data["path"] == [{"x": 100, "y": 200}, {"x": 150, "y": 250}]
436+
assert data["robotPosition"] == {"x": 150, "y": 250}

0 commit comments

Comments
 (0)