Skip to content

Commit 1e521b0

Browse files
committed
Refactored code to use a single WashInfo command class
1 parent 7d08f56 commit 1e521b0

File tree

7 files changed

+71
-35
lines changed

7 files changed

+71
-35
lines changed

deebot_client/capabilities.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@
4343
WorkMode,
4444
WorkModeEvent,
4545
)
46+
from deebot_client.events.wash_info import WashInfoEvent
4647

4748
if TYPE_CHECKING:
4849
from collections.abc import Callable
4950

5051
from _typeshed import DataclassInstance
5152

5253
from deebot_client.command import Command, SetCommand
54+
from deebot_client.commands.json.wash_info import SetWashInfo
5355
from deebot_client.events.efficiency_mode import EfficiencyMode, EfficiencyModeEvent
54-
from deebot_client.events.wash_info import WashInfoEvent, WashMode
56+
from deebot_client.events.wash_info import WashMode
5557
from deebot_client.models import CleanAction, CleanMode
5658

5759

@@ -122,6 +124,14 @@ class CapabilityCleanAction:
122124
area: Callable[[CleanMode, str, int], Command]
123125

124126

127+
@dataclass(frozen=True, kw_only=True)
128+
class CapabilityWashInfo(CapabilityEvent[WashInfoEvent]):
129+
"""Capabilities for wash handling."""
130+
131+
set: Callable[[WashMode | None, int | None], SetWashInfo]
132+
wash_modes: tuple[WashMode, ...]
133+
134+
125135
@dataclass(frozen=True, kw_only=True)
126136
class CapabilityClean:
127137
"""Capabilities for clean."""
@@ -132,8 +142,7 @@ class CapabilityClean:
132142
log: CapabilityEvent[CleanLogEvent] | None = None
133143
preference: CapabilitySetEnable[CleanPreferenceEvent] | None = None
134144
work_mode: CapabilitySetTypes[WorkModeEvent, WorkMode] | None = None
135-
wash_info_mode: CapabilitySetTypes[WashInfoEvent, WashMode] | None = None
136-
wash_info_hot_wash_amount: CapabilitySet[WashInfoEvent, int] | None = None
145+
wash_info: CapabilityWashInfo | None = None
137146

138147

139148
@dataclass(frozen=True)

deebot_client/commands/json/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from .true_detect import GetTrueDetect, SetTrueDetect
4141
from .voice_assistant_state import GetVoiceAssistantState, SetVoiceAssistantState
4242
from .volume import GetVolume, SetVolume
43-
from .wash_info import GetWashInfo, SetWashInfoHotWashAmount, SetWashInfoMode
43+
from .wash_info import GetWashInfo, SetWashInfo
4444
from .water_info import GetWaterInfo, SetWaterInfo
4545
from .work_mode import GetWorkMode, SetWorkMode
4646

@@ -99,8 +99,7 @@
9999
"GetVolume",
100100
"SetVolume",
101101
"GetWashInfo",
102-
"SetWashInfoHotWashAmount",
103-
"SetWashInfoMode",
102+
"SetWashInfo",
104103
"GetWaterInfo",
105104
"SetWaterInfo",
106105
"GetWorkMode",
@@ -188,8 +187,7 @@
188187
SetVolume,
189188

190189
GetWashInfo,
191-
SetWashInfoHotWashAmount,
192-
SetWashInfoMode,
190+
SetWashInfo,
193191

194192
GetWaterInfo,
195193
SetWaterInfo,
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
"""WashInfo command module."""
2+
from __future__ import annotations
23

34
from types import MappingProxyType
45
from typing import Any
56

67
from deebot_client.command import InitParam
7-
from deebot_client.event_bus import EventBus
8-
from deebot_client.events import WashInfoEvent, WashMode
9-
from deebot_client.message import HandlingResult
8+
from deebot_client.events import WashMode
9+
from deebot_client.messages.json.wash_info import OnWashInfo
1010

1111
from .common import JsonGetCommand, JsonSetCommand
1212

1313

14-
class GetWashInfo(OnWaterInfo, JsonGetCommand):
14+
class GetWashInfo(OnWashInfo, JsonGetCommand):
1515
"""Get wash info command."""
1616

1717
name = "getWashInfo"
1818

1919

20-
2120
class SetWashInfo(JsonSetCommand):
2221
"""Set wash info command."""
2322

@@ -30,14 +29,17 @@ class SetWashInfo(JsonSetCommand):
3029
}
3130
)
3231

33-
def __init__(self, mode: WashMode | str| None = None, hot_wash_amount: int | None) -> None:
34-
args = {}
32+
def __init__(
33+
self, mode: WashMode | str | None = None, hot_wash_amount: int | None = None
34+
) -> None:
35+
args: dict[str, Any] = {}
36+
3537
if isinstance(mode, str):
3638
mode = WashMode.get(mode)
37-
39+
3840
if mode is not None:
3941
args["mode"] = mode
40-
42+
4143
if hot_wash_amount is not None:
42-
agrs["hot_wash_amount"] = hot_wash_amount
44+
args["hot_wash_amount"] = hot_wash_amount
4345
super().__init__(args)

deebot_client/events/wash_info.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Wash info event module."""
2+
from __future__ import annotations
3+
24
from dataclasses import dataclass
35

46
from deebot_client.util import DisplayNameIntEnum
@@ -17,6 +19,6 @@ class WashMode(DisplayNameIntEnum):
1719
class WashInfoEvent(Event):
1820
"""Wash info event representation."""
1921

20-
mode: WashMode
22+
mode: WashMode | None
2123
interval: int | None
2224
hot_wash_amount: int | None

deebot_client/hardware/deebot/p1jij8.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
CapabilitySettings,
1616
CapabilitySetTypes,
1717
CapabilityStats,
18+
CapabilityWashInfo,
1819
)
1920
from deebot_client.commands.json.advanced_mode import GetAdvancedMode, SetAdvancedMode
2021
from deebot_client.commands.json.battery import GetBattery
@@ -53,8 +54,7 @@
5354
from deebot_client.commands.json.volume import GetVolume, SetVolume
5455
from deebot_client.commands.json.wash_info import (
5556
GetWashInfo,
56-
SetWashInfoHotWashAmount,
57-
SetWashInfoMode,
57+
SetWashInfo,
5858
)
5959
from deebot_client.commands.json.water_info import GetWaterInfo, SetWaterInfo
6060
from deebot_client.commands.json.work_mode import GetWorkMode, SetWorkMode
@@ -130,20 +130,15 @@
130130
WorkMode.VACUUM_AND_MOP,
131131
),
132132
),
133-
wash_info_mode=CapabilitySetTypes(
133+
wash_info=CapabilityWashInfo(
134134
event=WashInfoEvent,
135135
get=[GetWashInfo()],
136-
set=SetWashInfoMode,
137-
types=(
136+
set=SetWashInfo,
137+
wash_modes=(
138138
WashMode.STANDARD,
139139
WashMode.HOT,
140140
),
141141
),
142-
wash_info_hot_wash_amount=CapabilitySet(
143-
event=WashInfoEvent,
144-
get=[GetWashInfo()],
145-
set=SetWashInfoHotWashAmount,
146-
),
147142
),
148143
custom=CapabilityCustomCommand(
149144
event=CustomCommandEvent, get=[], set=CustomCommand

deebot_client/messages/json/wash_info.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""WashInfo messages."""
2-
from typing import Any
2+
from __future__ import annotations
3+
4+
from typing import TYPE_CHECKING, Any
35

4-
from deebot_client.event_bus import EventBus
56
from deebot_client.events import WashInfoEvent
67
from deebot_client.events.wash_info import WashMode
78
from deebot_client.message import HandlingResult, MessageBodyDataDict
89

10+
if TYPE_CHECKING:
11+
from deebot_client.event_bus import EventBus
12+
913

1014
class OnWashInfo(MessageBodyDataDict):
1115
"""On battery message."""
@@ -20,11 +24,15 @@ def _handle_body_data_dict(
2024
2125
:return: A message response
2226
"""
27+
mode = data.get("mode")
28+
if isinstance(mode, int):
29+
mode = WashMode(mode)
30+
2331
event_bus.notify(
2432
WashInfoEvent(
25-
mode=WashMode(int(data["mode"])),
26-
hot_wash_amount=data["hot_wash_amount"],
27-
interval=data["interval"],
33+
mode=mode,
34+
hot_wash_amount=data.get("hot_wash_amount"),
35+
interval=data.get("interval"),
2836
)
2937
)
3038
return HandlingResult.success()

tests/commands/json/test_wash_info.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
from __future__ import annotations
2+
13
from typing import Any
24

35
import pytest
46

57
from deebot_client.commands.json import (
68
GetWashInfo,
79
)
10+
from deebot_client.commands.json.wash_info import SetWashInfo
811
from deebot_client.events import WashInfoEvent, WashMode
912
from tests.helpers import (
1013
get_request_json,
1114
get_success_body,
1215
verify_DisplayNameEnum_unique,
1316
)
1417

15-
from . import assert_command
18+
from . import assert_command, assert_set_command
1619

1720

1821
def test_WashInfo_unique() -> None:
@@ -35,3 +38,22 @@ def test_WashInfo_unique() -> None:
3538
async def test_GetWashInfo(json: dict[str, Any], expected: WashInfoEvent) -> None:
3639
json = get_request_json(get_success_body(json))
3740
await assert_command(GetWashInfo(), json, expected)
41+
42+
43+
@pytest.mark.parametrize(("value"), [WashMode.HOT, "hot"])
44+
async def test_SetWashInfo_mode(value: WashMode | str) -> None:
45+
command = SetWashInfo(mode=value)
46+
args = {"mode": WashMode.HOT}
47+
await assert_set_command(command, args, WashInfoEvent(WashMode.HOT, None, None))
48+
49+
50+
def test_SetWashInfo_mode_inexisting_value() -> None:
51+
with pytest.raises(ValueError, match="'INEXSTING' is not a valid WashMode member"):
52+
SetWashInfo(mode="inexsting")
53+
54+
55+
@pytest.mark.parametrize(("value"), [1])
56+
async def test_SetWashInfo_hot_wash_amount(value: int) -> None:
57+
command = SetWashInfo(hot_wash_amount=value)
58+
args = {"hot_wash_amount": value}
59+
await assert_set_command(command, args, WashInfoEvent(None, None, value))

0 commit comments

Comments
 (0)