Skip to content

Commit 8947c6c

Browse files
committed
refactor: Standardize trait data merging to merge_trait_values and remove direct _parse_response methods from traits.
1 parent 0b99b90 commit 8947c6c

File tree

8 files changed

+10
-25
lines changed

8 files changed

+10
-25
lines changed

roborock/devices/traits/v1/common.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919

2020
class V1TraitDataConverter:
21-
"""Converts responses to RoborockBase objects."""
21+
"""Converts responses to RoborockBase objects.
22+
23+
"""
2224

2325
@abstractmethod
2426
def convert(self, response: V1ResponseData) -> RoborockBase:
@@ -69,11 +71,10 @@ async def refresh(self) -> None:
6971
"""Refresh the contents of this trait."""
7072
response = await self.rpc_channel.send_command(self.command)
7173
new_data = self.converter.convert(response)
72-
# Mixin doesn't know its type
73-
merge_object_values(self, new_data) # type: ignore[arg-type]
74+
merge_trait_values(self, new_data) # type: ignore[arg-type]
7475

7576

76-
def merge_object_values(target: RoborockBase, new_object: RoborockBase) -> bool:
77+
def merge_trait_values(target: RoborockBase, new_object: RoborockBase) -> bool:
7778
"""Update the target object with set fields in new_object."""
7879
updated = False
7980
for field in fields(new_object):

roborock/devices/traits/v1/consumeable.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ class ConsumableTrait(Consumable, common.V1TraitMixin):
4343
command = RoborockCommand.GET_CONSUMABLE
4444
converter = common.DefaultConverter(Consumable)
4545

46-
def __init__(self) -> None:
47-
common.V1TraitMixin.__init__(self)
48-
4946
async def reset_consumable(self, consumable: ConsumableAttribute) -> None:
5047
"""Reset a specific consumable attribute on the device."""
5148
await self.rpc_channel.send_command(RoborockCommand.RESET_CONSUMABLE, params=[consumable.value])

roborock/devices/traits/v1/device_features.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DeviceFeaturesTrait(DeviceFeatures, common.V1TraitMixin):
3232
"""Trait for managing supported features on Roborock devices."""
3333

3434
command = RoborockCommand.APP_GET_INIT_STATUS
35+
converter: DeviceTraitsConverter
3536

3637
def __init__(self, product: HomeDataProduct, device_cache: DeviceCache) -> None: # pylint: disable=super-init-not-called
3738
"""Initialize DeviceFeaturesTrait."""
@@ -75,7 +76,7 @@ async def refresh(self) -> None:
7576
"""
7677
cache_data = await self._device_cache.get()
7778
if cache_data.device_features is not None:
78-
common.merge_object_values(self, cache_data.device_features)
79+
common.merge_trait_values(self, cache_data.device_features)
7980
return
8081
# Save cached device features
8182
await super().refresh()

roborock/devices/traits/v1/home.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import asyncio
1919
import base64
2020
import logging
21-
from typing import Self
2221

2322
from roborock.data import CombinedMapInfo, NamedRoomMapping, RoborockBase
2423
from roborock.data.v1.v1_code_mappings import RoborockStateCode
@@ -238,10 +237,6 @@ def home_map_content(self) -> dict[int, MapContent] | None:
238237
"""Returns the map content for all cached maps."""
239238
return self._home_map_content
240239

241-
def _parse_response(self, response: common.V1ResponseData) -> Self:
242-
"""This trait does not parse responses directly."""
243-
raise NotImplementedError("HomeTrait does not support direct command responses")
244-
245240
async def _update_home_cache(
246241
self, home_map_info: dict[int, CombinedMapInfo], home_map_content: dict[int, MapContent]
247242
) -> None:

roborock/devices/traits/v1/map_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(self, map_parser_config: MapParserConfig | None = None) -> None:
9191
super().__init__()
9292
self.converter = MapContentConverter(MapParser(map_parser_config or MapParserConfig()))
9393

94-
def _parse_response(self, response: common.V1ResponseData) -> MapContent:
94+
def _convert(self, response: common.V1ResponseData) -> MapContent:
9595
"""Parse the response from the device into a MapContentTrait instance."""
9696
if not isinstance(response, bytes):
9797
raise ValueError(f"Unexpected MapContentTrait response format: {type(response)}")

roborock/devices/traits/v1/network_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def refresh(self) -> None:
4747
device_cache_data = await self._device_cache.get()
4848
if device_cache_data.network_info:
4949
_LOGGER.debug("Using cached network info for device %s", self._device_uid)
50-
common.merge_object_values(self, device_cache_data.network_info)
50+
common.merge_trait_values(self, device_cache_data.network_info)
5151
return
5252

5353
# Load from device if not in cache

roborock/devices/traits/v1/rooms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async def refresh(self) -> None:
103103
await self._populate_missing_home_data_rooms(segment_map)
104104
rooms = self.converter.convert(response)
105105
rooms = rooms.with_rooom_names(self._iot_id_room_name_map)
106-
common.merge_object_values(self, rooms)
106+
common.merge_trait_values(self, rooms)
107107

108108
@property
109109
def _iot_id_room_name_map(self) -> dict[str, str]:

roborock/devices/traits/v1/status.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from functools import cached_property
2-
from typing import Self
32

43
from roborock import (
54
CleanRoutes,
@@ -92,11 +91,3 @@ def mop_route_name(self) -> str | None:
9291
if self.mop_mode is None:
9392
return None
9493
return self.mop_route_mapping.get(self.mop_mode)
95-
96-
def _parse_response(self, response: common.V1ResponseData) -> Self:
97-
"""Parse the response from the device into a StatusV2-based status object."""
98-
if isinstance(response, list):
99-
response = response[0]
100-
if isinstance(response, dict):
101-
return StatusV2.from_dict(response)
102-
raise ValueError(f"Unexpected status format: {response!r}")

0 commit comments

Comments
 (0)