Skip to content

Commit 965ea95

Browse files
committed
refactor: type simulator consumables state using Consumable dataclass
1 parent 59c4169 commit 965ea95

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

roborock/testing/v1_simulator.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import logging
99
import time
1010
from collections.abc import Callable
11+
from dataclasses import asdict
1112
from typing import Any
1213
from unittest.mock import Mock
1314

1415
from roborock.data import HomeDataDevice, HomeDataProduct
1516
from roborock.data.v1 import RoborockStateCode
17+
from roborock.data.v1.v1_containers import Consumable
1618
from roborock.devices.cache import DeviceCache, InMemoryCache
1719
from roborock.devices.rpc.v1_channel import V1Channel
1820
from roborock.protocols.v1_protocol import SecurityData
@@ -107,16 +109,16 @@ def __init__(
107109
self.dss = dss
108110
self.dock_type = dock_type
109111

110-
self.consumables = {
111-
"main_brush_work_time": 74382,
112-
"side_brush_work_time": 74383,
113-
"filter_work_time": 74384,
114-
"filter_element_work_time": 0,
115-
"sensor_dirty_time": 74385,
116-
"strainer_work_times": 65,
117-
"dust_collection_work_times": 25,
118-
"cleaning_brush_work_times": 66,
119-
}
112+
self.consumables = Consumable(
113+
main_brush_work_time=74382,
114+
side_brush_work_time=74383,
115+
filter_work_time=74384,
116+
filter_element_work_time=0,
117+
sensor_dirty_time=74385,
118+
strainer_work_times=65,
119+
dust_collection_work_times=25,
120+
cleaning_brush_work_times=66,
121+
)
120122

121123
self.dnd_timer = {
122124
"start_hour": 22,
@@ -153,7 +155,7 @@ def __init__(
153155
# Set up default handlers dictionary
154156
self.default_handlers: dict[str, Callable[[Any], Any]] = {
155157
"get_status": lambda params: [self.get_status_dict()],
156-
"get_consumable": lambda params: [self.consumables],
158+
"get_consumable": lambda params: [{k: v for k, v in asdict(self.consumables).items() if v is not None}],
157159
"get_dnd_timer": lambda params: self.dnd_timer,
158160
"get_clean_summary": lambda params: self.clean_summary,
159161
"get_clean_record": lambda params: self.last_clean_record,
@@ -282,8 +284,8 @@ def _handle_set_water_box_custom_mode(self, params: Any) -> str:
282284
def _handle_reset_consumable(self, params: Any) -> str:
283285
if isinstance(params, list) and len(params) > 0:
284286
consumable_name = params[0]
285-
if consumable_name in self.consumables:
286-
self.consumables[consumable_name] = 0
287+
if hasattr(self.consumables, consumable_name):
288+
setattr(self.consumables, consumable_name, 0)
287289
return "ok"
288290

289291
def _handle_app_get_init_status(self, params: Any) -> list[dict[str, Any]]:

tests/testing/test_v1_simulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def test_trait_consumable_reset():
5050
await device.v1_properties.consumables.reset_consumable(ConsumableAttribute.FILTER_WORK_TIME)
5151

5252
# The simulator state should be updated
53-
assert fake_device.consumables["filter_work_time"] == 0
53+
assert fake_device.consumables.filter_work_time == 0
5454
# The trait auto-refreshes after reset, so the client should reflect the change
5555
assert device.v1_properties.consumables.filter_work_time == 0
5656

0 commit comments

Comments
 (0)