Skip to content

Commit dcd938a

Browse files
committed
refactor: wrap room response processing in try-except block and fix test docstring
1 parent a8b96a8 commit dcd938a

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

roborock/devices/traits/v1/rooms.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ async def refresh(self) -> None:
9595
"""Refresh room mappings and backfill unknown room names from the web API."""
9696
response = await self.rpc_channel.send_command(self.command)
9797
if not isinstance(response, list):
98-
raise ValueError(f"Unexpected RoomsTrait response format: {response!r}")
98+
raise RoborockParsingException(
99+
trait_name=type(self).__name__,
100+
command=self.command,
101+
payload=response,
102+
inner_error="Unexpected RoomsTrait response format",
103+
)
99104

100105
segment_map = RoomsConverter.extract_segment_map(response)
101106
# Track all iot ids seen before. Refresh the room list when new ids are found.
@@ -106,7 +111,6 @@ async def refresh(self) -> None:
106111
_LOGGER.debug("Updating rooms: %s", list(updated_rooms))
107112
self._home_data.rooms = updated_rooms
108113
self._discovered_iot_ids.update(new_iot_ids)
109-
110114
try:
111115
rooms = self.converter.convert(response)
112116
except (TypeError, ValueError) as err:
@@ -116,6 +120,7 @@ async def refresh(self) -> None:
116120
payload=response,
117121
inner_error=err,
118122
) from err
123+
119124
rooms = rooms.with_room_names(self._home_data.rooms_name_map)
120125
common.merge_trait_values(self, rooms)
121126

roborock/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class RoborockUnsupportedFeature(RoborockException):
9999
class RoborockParsingException(RoborockException):
100100
"""Class for Roborock exceptions when parsing device responses."""
101101

102-
def __init__(self, trait_name: str, command: Enum | str, payload: Any, inner_error: Exception) -> None:
102+
def __init__(self, trait_name: str, command: Enum | str, payload: Any, inner_error: Exception | str) -> None:
103103
cmd_name = command.name if isinstance(command, Enum) else str(command)
104104
self.message = (
105105
f"Failed to parse {cmd_name} response for {trait_name}. Payload: {payload!r} Error: {inner_error!r}"

tests/devices/traits/v1/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def test_fake_trait_bad_payload() -> None:
4444

4545

4646
async def test_valid_payload() -> None:
47-
"""Test that parsing a bad payload throws a helpful parsing error."""
47+
"""Test that a valid payload parses successfully."""
4848
trait = FakeTrait()
4949
trait._rpc_channel.send_command.return_value = [{"fake_field": 123, "other_field": "abc"}]
5050
await trait.refresh()

0 commit comments

Comments
 (0)