Skip to content

Commit b07b2a3

Browse files
committed
fix: Update trait refresh method to return None
1 parent 44680e3 commit b07b2a3

File tree

6 files changed

+25
-41
lines changed

6 files changed

+25
-41
lines changed

roborock/devices/traits/v1/clean_summary.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CleanSummaryTrait(CleanSummaryWithDetail, common.V1TraitMixin):
1414

1515
command = RoborockCommand.GET_CLEAN_SUMMARY
1616

17-
async def refresh(self) -> Self:
17+
async def refresh(self) -> None:
1818
"""Refresh the clean summary data and last clean record.
1919
2020
Assumes that the clean summary has already been fetched.
@@ -23,10 +23,9 @@ async def refresh(self) -> Self:
2323
if not self.records:
2424
_LOGGER.debug("No clean records available in clean summary.")
2525
self.last_clean_record = None
26-
return self
26+
return
2727
last_record_id = self.records[-1]
2828
self.last_clean_record = await self.get_clean_record(last_record_id)
29-
return self
3029

3130
@classmethod
3231
def _parse_type_response(cls, response: common.V1ResponseData) -> Self:
@@ -61,23 +60,15 @@ def _parse_clean_record_response(cls, response: common.V1ResponseData) -> CleanR
6160
if isinstance(response[-1], dict):
6261
records = [CleanRecord.from_dict(rec) for rec in response]
6362
final_record = records[-1]
64-
try:
65-
# This code is semi-presumptuous - so it is put in a try finally to be safe.
66-
final_record.begin = records[0].begin
67-
final_record.begin_datetime = records[0].begin_datetime
68-
final_record.start_type = records[0].start_type
69-
for rec in records[0:-1]:
70-
final_record.duration = (final_record.duration or 0) + (rec.duration or 0)
71-
final_record.area = (final_record.area or 0) + (rec.area or 0)
72-
final_record.avoid_count = (final_record.avoid_count or 0) + (rec.avoid_count or 0)
73-
final_record.wash_count = (final_record.wash_count or 0) + (rec.wash_count or 0)
74-
final_record.square_meter_area = (final_record.square_meter_area or 0) + (
75-
rec.square_meter_area or 0
76-
)
77-
return final_record
78-
except Exception:
79-
# Return final record when an exception occurred
80-
return final_record
63+
# Aggregate basic stats from prior partial records (exclude last which is full)
64+
final_record.begin = records[0].begin
65+
final_record.start_type = records[0].start_type
66+
for rec in records[0:-1]:
67+
final_record.duration = (final_record.duration or 0) + (rec.duration or 0)
68+
final_record.area = (final_record.area or 0) + (rec.area or 0)
69+
final_record.avoid_count = (final_record.avoid_count or 0) + (rec.avoid_count or 0)
70+
final_record.wash_count = (final_record.wash_count or 0) + (rec.wash_count or 0)
71+
return final_record
8172
# There are still a few unknown variables in this.
8273
begin, end, duration, area = unpack_list(response, 4)
8374
return CleanRecord(begin=begin, end=end, duration=duration, area=area)

roborock/devices/traits/v1/common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@ def rpc_channel(self) -> V1RpcChannel:
7676
raise ValueError("Device trait in invalid state")
7777
return self._rpc_channel
7878

79-
async def refresh(self) -> Self:
79+
async def refresh(self) -> None:
8080
"""Refresh the contents of this trait."""
8181
response = await self.rpc_channel.send_command(self.command)
8282
new_data = self._parse_response(response)
8383
if not isinstance(new_data, RoborockBase):
8484
raise ValueError(f"Internal error, unexpected response type: {new_data!r}")
8585
_LOGGER.debug("Refreshed %s: %s", self.__class__.__name__, new_data)
8686
self._update_trait_values(new_data)
87-
return self
8887

8988
def _update_trait_values(self, new_data: RoborockBase) -> None:
9089
"""Update the values of this trait from another instance."""

roborock/devices/traits/v1/device_features.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from dataclasses import fields
2-
from typing import Self
32

43
from roborock.data import AppInitStatus, RoborockProductNickname
54
from roborock.device_features import DeviceFeatures
@@ -13,7 +12,7 @@ class DeviceFeaturesTrait(DeviceFeatures, common.V1TraitMixin):
1312

1413
command = RoborockCommand.APP_GET_INIT_STATUS
1514

16-
def __init__(self, product_nickname: RoborockProductNickname, cache: Cache) -> None:
15+
def __init__(self, product_nickname: RoborockProductNickname, cache: Cache) -> None: # pylint: disable=super-init-not-called
1716
"""Initialize MapContentTrait."""
1817
self._nickname = product_nickname
1918
self._cache = cache
@@ -22,7 +21,7 @@ def __init__(self, product_nickname: RoborockProductNickname, cache: Cache) -> N
2221
for field in fields(self):
2322
setattr(self, field.name, False)
2423

25-
async def refresh(self) -> Self:
24+
async def refresh(self) -> None:
2625
"""Refresh the contents of this trait.
2726
2827
This will use cached device features if available since they do not
@@ -32,12 +31,11 @@ async def refresh(self) -> Self:
3231
cache_data = await self._cache.get()
3332
if cache_data.device_features is not None:
3433
self._update_trait_values(cache_data.device_features)
35-
return self
34+
return
3635
# Save cached device features
37-
device_features = await super().refresh()
38-
cache_data.device_features = device_features
36+
await super().refresh()
37+
cache_data.device_features = self
3938
await self._cache.set(cache_data)
40-
return device_features
4139

4240
def _parse_response(self, response: common.V1ResponseData) -> DeviceFeatures:
4341
"""Parse the response from the device into a MapContentTrait instance."""

roborock/devices/traits/v1/home.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def _build_home_map_info(self) -> tuple[dict[int, CombinedMapInfo], dict[i
158158
home_map_info[map_info.map_flag] = combined_map_info
159159
return home_map_info, home_map_content
160160

161-
async def refresh(self) -> Self:
161+
async def refresh(self) -> None:
162162
"""Refresh current map's underlying map and room data, updating cache as needed.
163163
164164
This will only refresh the current map's data and will not populate non
@@ -171,7 +171,7 @@ async def refresh(self) -> Self:
171171
# then we'll fall through below to refresh the current map only.
172172
try:
173173
await self.discover_home()
174-
return self
174+
return
175175
except RoborockDeviceBusy:
176176
_LOGGER.debug("Cannot refresh home data while device is busy cleaning")
177177

@@ -189,7 +189,6 @@ async def refresh(self) -> Self:
189189
await self._update_current_map(
190190
map_flag, combined_map_info, new_map_content, update_cache=self._discovery_completed
191191
)
192-
return self
193192

194193
@property
195194
def home_map_info(self) -> dict[int, CombinedMapInfo] | None:

roborock/devices/traits/v1/network_info.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import logging
6-
from typing import Self
76

87
from roborock.data import NetworkInfo
98
from roborock.devices.cache import Cache
@@ -25,20 +24,20 @@ class NetworkInfoTrait(NetworkInfo, common.V1TraitMixin):
2524

2625
command = RoborockCommand.GET_NETWORK_INFO
2726

28-
def __init__(self, device_uid: str, cache: Cache) -> None:
27+
def __init__(self, device_uid: str, cache: Cache) -> None: # pylint: disable=super-init-not-called
2928
"""Initialize the trait."""
3029
self._device_uid = device_uid
3130
self._cache = cache
3231
self.ip = ""
3332

34-
async def refresh(self) -> Self:
33+
async def refresh(self) -> None:
3534
"""Refresh the network info from the cache."""
3635

3736
cache_data = await self._cache.get()
3837
if cache_data.network_info and (network_info := cache_data.network_info.get(self._device_uid)):
3938
_LOGGER.debug("Using cached network info for device %s", self._device_uid)
4039
self._update_trait_values(network_info)
41-
return self
40+
return
4241

4342
# Load from device if not in cache
4443
_LOGGER.debug("No cached network info for device %s, fetching from device", self._device_uid)
@@ -48,8 +47,6 @@ async def refresh(self) -> Self:
4847
cache_data.network_info[self._device_uid] = self
4948
await self._cache.set(cache_data)
5049

51-
return self
52-
5350
def _parse_response(self, response: common.V1ResponseData) -> NetworkInfo:
5451
"""Parse the response from the device into a NetworkInfo."""
5552
if not isinstance(response, dict):

tests/devices/traits/v1/test_rooms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ async def test_refresh_rooms_trait(
4848
assert not rooms_trait.rooms
4949

5050
# Load the room mapping information
51-
refreshed_trait = await rooms_trait.refresh()
51+
await rooms_trait.refresh()
5252

5353
# Verify the room mappings are now populated
54-
assert refreshed_trait.rooms
55-
rooms = refreshed_trait.rooms
54+
assert rooms_trait.rooms
55+
rooms = rooms_trait.rooms
5656
assert len(rooms) == 3
5757

5858
assert rooms[0].segment_id == 16

0 commit comments

Comments
 (0)