8000 Implement vacuum dustbin module (dust_bucket) (#1423) · ryenitcher/python-kasa@3c98efb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c98efb

Browse files
authored
Implement vacuum dustbin module (dust_bucket) (python-kasa#1423)
Initial implementation for dustbin auto-emptying. New features: - `dustbin_empty` action to empty the dustbin immediately - `dustbin_autocollection_enabled` to toggle the auto collection - `dustbin_mode` to choose how often the auto collection is performed
1 parent 68f50aa commit 3c98efb

File tree

8 files changed

+227
-3
lines changed

8 files changed

+227
-3
lines changed

devtools/helpers/smartrequests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ def get_component_requests(component_id, ver_code):
455455
SmartRequest.get_raw_request("getMapData"),
456456
],
457457
"auto_change_map": [SmartRequest.get_raw_request("getAutoChangeMap")],
458-
"dust_bucket": [SmartRequest.get_raw_request("getAutoDustCollection")],
458+
"dust_bucket": [
459+
SmartRequest.get_raw_request("getAutoDustCollection"),
460+
SmartRequest.get_raw_request("getDustCollectionInfo"),
461+
],
459462
"mop": [SmartRequest.get_raw_request("getMopState")],
460463
"do_not_disturb": [SmartRequest.get_raw_request("getDoNotDisturb")],
461464
"charge_pose_clean": [],

kasa/module.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class Module(ABC):
163163

164164
# Vacuum modules
165165
Clean: Final[ModuleName[smart.Clean]] = ModuleName("Clean")
166+
Dustbin: Final[ModuleName[smart.Dustbin]] = ModuleName("Dustbin")
166167

167168
def __init__(self, device: Device, module: str) -> None:
168169
self._device = device

kasa/smart/modules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .colortemperature import ColorTemperature
1414
from .contactsensor import ContactSensor
1515
from .devicemodule import DeviceModule
16+
from .dustbin import Dustbin
1617
from .energy import Energy
1718
from .fan import Fan
1819
from .firmware import Firmware
@@ -72,4 +73,5 @@
7273
"OverheatProtection",
7374
"HomeKit",
7475
"Matter",
76+
"Dustbin",
7577
]

kasa/smart/modules/dustbin.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""Implementation of vacuum dustbin."""
2+
3+
from __future__ import annotations
4+
5+
import logging
6+
from enum import IntEnum
7+
8+
from ...feature import Feature
9+
from ..smartmodule import SmartModule
10+
11+
_LOGGER = logging.getLogger(__name__)
12+
13+
14+
class Mode(IntEnum):
15+
"""Dust collection modes."""
16+
17+
Smart = 0
18+
Light = 1
19+
Balanced = 2
20+
Max = 3
21+
22+
23+
class Dustbin(SmartModule):
24+
"""Implementation of vacuum dustbin."""
25+
26+
REQUIRED_COMPONENT = "dust_bucket"
27+
28+
def _initialize_features(self) -> None:
29+
"""Initialize features."""
30+
self._add_feature(
31+
Feature(
32+
self._device,
33+
id="dustbin_empty",
34+
name="Empty dustbin",
35+
container=self,
36+
attribute_setter="start_emptying",
37+
category=Feature.Category.Primary,
38+
type=Feature.Action,
39+
)
40+
)
41+
42+
self._add_feature(
43+
Feature(
44+
self._device,
45+
id="dustbin_autocollection_enabled",
46+
name="Automatic emptying enabled",
47+
container=self,
48+
attribute_getter="auto_collection",
49+
attribute_setter="set_auto_collection",
50+
category=Feature.Category.Config,
51+
type=Feature.Switch,
52+
)
53+
)
54+
55+
self._add_feature(
56+
Feature(
57+
self._device,
58+
id="dustbin_mode",
59+
name="Automatic emptying mode",
60+
container=self,
61+
attribute_getter="mode",
62+
attribute_setter="set_mode",
63+
icon="mdi:fan",
64+
choices_getter=lambda: list(Mode.__members__),
65+
category=Feature.Category.Config,
66+
type=Feature.Type.Choice,
67+
)
68+
)
69+
70+
def query(self) -> dict:
71+
"""Query to execute during the update cycle."""
72+
return {
73+
"getAutoDustCollection": {},
74+
"getDustCollectionInfo": {},
75+
}
76+
77+
async def start_emptying(self) -> dict:
78+
"""Start emptying the bin."""
79+
return await self.call(
80+
"setSwitchDustCollection",
81+
{
82+
"switch_dust_collection": True,
83+
},
84+
)
85+
86+
@property
87+
def _settings(self) -> dict:
88+
"""Return auto-empty settings."""
89+
return self.data["getDustCollectionInfo"]
90+
91+
@property
92+
def mode(self) -> str:
93+
"""Return auto-emptying mode."""
94+
return Mode(self._settings["dust_collection_mode"]).name
95+
96+
async def set_mode(self, mode: str) -> dict:
97+
"""Set auto-emptying mode."""
98+
name_to_value = {x.name: x.value for x in Mode}
99+
if mode not in name_to_value:
100+
raise ValueError(
101+
"Invalid auto/emptying mode speed %s, available %s", mode, name_to_value
102+
)
103+
104+
settings = self._settings.copy()
105+
settings["dust_collection_mode"] = name_to_value[mode]
106+
return await self.call("setDustCollectionInfo", settings)
107+
108+
@property
109+
def auto_collection(self) -> dict:
110+
"""Return auto-emptying config."""
111+
return self._settings["auto_dust_collection"]
112+
113+
async def set_auto_collection(self, on: bool) -> dict:
114+
"""Toggle auto-emptying."""
115+
settings = self._settings.copy()
116+
settings["auto_dust_collection"] = on
117+
return await self.call("setDustCollectionInfo", settings)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ markers = [
112112
]
113113
asyncio_mode = "auto"
114114
asyncio_default_fixture_loop_scope = "function"
115-
timeout = 10
115+
#timeout = 10
116116
# dist=loadgroup enables grouping of tests into single worker.
117117
# required as caplog doesn't play nicely with multiple workers.
118118
addopts = "--disable-socket --allow-unix-socket --dist=loadgroup"

tests/fakeprotocol_smart.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,12 @@ async def _send_request(self, request_dict: dict):
640640
elif method[:3] == "set":
641641
target_method = f"get{method[3:]}"
642642
# Some vacuum commands do not have a getter
643-
if method in ["setRobotPause", "setSwitchClean", "setSwitchCharge"]:
643+
if method in [
644+
"setRobotPause",
645+
"setSwitchClean",
646+
"setSwitchCharge",
647+
"setSwitchDustCollection",
648+
]:
644649
return {"error_code": 0}
645650

646651
info[target_method].update(params)

tests/fixtures/smart/RV20 Max Plus(EU)_1.0_1.0.7.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@
202202
"getMopState": {
203203
"mop_state": false
204204
},
205+
"getDustCollectionInfo": {
206+
"auto_dust_collection": true,
207+
"dust_collection_mode": 0
208+
},
205209
"getVacStatus": {
206210
"err_status": [
207211
0

tests/smart/modules/test_dustbin.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
from pytest_mock import MockerFixture
5+
6+
from kasa import Module
7+
from kasa.smart import SmartDevice
8+
from kasa.smart.modules.dustbin import Mode
9+
10+
from ...device_fixtures import get_parent_and_child_modules, parametrize
11+
12+
dustbin = parametrize(
13+
"has dustbin", component_filter="dust_bucket", protocol_filter={"SMART"}
14+
)
15+
16+
17+
@dustbin
18+
@pytest.mark.parametrize(
19+
("feature", "prop_name", "type"),
20+
[
21+
("dustbin_autocollection_enabled", "auto_collection", bool),
22+
("dustbin_mode", "mode", str),
23+
],
24+
)
25+
async def test_features(dev: SmartDevice, feature: str, prop_name: str, type: type):
26+
"""Test that features are registered and work as expected."""
27+
dustbin = next(get_parent_and_child_modules(dev, Module.Dustbin))
28+
assert dustbin is not None
29+
30+
prop = getattr(dustbin, prop_name)
31+
assert isinstance(prop, type)
32+
33+
feat = dustbin._device.features[feature]
34+
assert feat.value == prop
35+
assert isinstance(feat.value, type)
36+
37+
38+
@dustbin
39+
async def test_dustbin_mode(dev: SmartDevice, mocker: MockerFixture):
40+
"""Test dust mode."""
41+
dustbin = next(get_parent_and_child_modules(dev, Module.Dustbin))
42+
call = mocker.spy(dustbin, "call")
43+
44+
mode_feature = dustbin._device.features["dustbin_mode"]
45+
assert dustbin.mode == mode_feature.value
46+
47+
new_mode = Mode.Max
48+
await dustbin.set_mode(new_mode.name)
49+
50+
params = dustbin._settings.copy()
51+
params["dust_collection_mode"] = new_mode.value
52+
53+
call.assert_called_with("setDustCollectionInfo", params)
54+
55+
await dev.update()
56+
57+
assert dustbin.mode == new_mode.name
58+
59+
with pytest.raises(ValueError, match="Invalid auto/emptying mode speed"):
60+
await dustbin.set_mode("invalid")
61+
62+
63+
@dustbin
64+
async def test_autocollection(dev: SmartDevice, mocker: MockerFixture):
65+
"""Test autocollection switch."""
66+
dustbin = next(get_parent_and_child_modules(dev, Module.Dustbin))
67+
call = mocker.spy(dustbin, "call")
68+
69+
auto_collection = dustbin._device.features["dustbin_autocollection_enabled"]
70+
assert dustbin.auto_collection == auto_collection.value
71+
72+
await auto_collection.set_value(True)
73+
74+
params = dustbin._settings.copy()
75+
params["auto_dust_collection"] = True
76+
77+
call.assert_called_with("setDustCollectionInfo", params)
78+
79+
await dev.update()
80+
81+
assert dustbin.auto_collection is True
82+
83+
84+
@dustbin
85+
async def test_empty_dustbin(dev: SmartDevice, mocker: MockerFixture):
86+
"""Test the empty dustbin feature."""
87+
dustbin = next(get_parent_and_child_modules(dev, Module.Dustbin))
88+
call = mocker.spy(dustbin, "call")
89+
90+
await dustbin.start_emptying()
91+
92+
call.assert_called_with("setSwitchDustCollection", {"switch_dust_collection": True})

0 commit comments

Comments
 (0)
0