From 4a1891a931ee21498f6789d1505c4ba5647dff80 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sun, 26 Jan 2025 20:13:06 +0100 Subject: [PATCH 1/2] dustbin_mode: add 'off' mode for cleaner downstream implementation --- kasa/smart/modules/dustbin.py | 10 ++++++++++ tests/smart/modules/test_dustbin.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/kasa/smart/modules/dustbin.py b/kasa/smart/modules/dustbin.py index 33aecd8f7..b2b4d1ef4 100644 --- a/kasa/smart/modules/dustbin.py +++ b/kasa/smart/modules/dustbin.py @@ -19,6 +19,8 @@ class Mode(IntEnum): Balanced = 2 Max = 3 + Off = -1_000 + class Dustbin(SmartModule): """Implementation of vacuum dustbin.""" @@ -91,6 +93,8 @@ def _settings(self) -> dict: @property def mode(self) -> str: """Return auto-emptying mode.""" + if self.auto_collection is False: + return Mode.Off.name return Mode(self._settings["dust_collection_mode"]).name async def set_mode(self, mode: str) -> dict: @@ -101,8 +105,14 @@ async def set_mode(self, mode: str) -> dict: "Invalid auto/emptying mode speed %s, available %s", mode, name_to_value ) + if mode == Mode.Off.name: + return await self.set_auto_collection(False) + + # Make a copy just in case, even when we are overriding both settings settings = self._settings.copy() + settings["auto_dust_collection"] = True settings["dust_collection_mode"] = name_to_value[mode] + return await self.call("setDustCollectionInfo", settings) @property diff --git a/tests/smart/modules/test_dustbin.py b/tests/smart/modules/test_dustbin.py index d30d2459b..ad58e1d41 100644 --- a/tests/smart/modules/test_dustbin.py +++ b/tests/smart/modules/test_dustbin.py @@ -60,6 +60,24 @@ async def test_dustbin_mode(dev: SmartDevice, mocker: MockerFixture): await dustbin.set_mode("invalid") +@dustbin +async def test_dustbin_mode_off(dev: SmartDevice, mocker: MockerFixture): + """Test dustbin_mode == Off.""" + dustbin = next(get_parent_and_child_modules(dev, Module.Dustbin)) + call = mocker.spy(dustbin, "call") + + auto_collection = dustbin._device.features["dustbin_mode"] + await auto_collection.set_value(Mode.Off.name) + + params = dustbin._settings.copy() + params["auto_dust_collection"] = False + + call.assert_called_with("setDustCollectionInfo", params) + + await dev.update() + assert dustbin.auto_collection is False + + @dustbin async def test_autocollection(dev: SmartDevice, mocker: MockerFixture): """Test autocollection switch.""" From 65e12b43bea238833f2efa60ce6b1cb1805699ff Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sun, 2 Feb 2025 13:51:53 +0100 Subject: [PATCH 2/2] Improve coverage --- tests/smart/modules/test_dustbin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/smart/modules/test_dustbin.py b/tests/smart/modules/test_dustbin.py index ad58e1d41..ecc68b6b2 100644 --- a/tests/smart/modules/test_dustbin.py +++ b/tests/smart/modules/test_dustbin.py @@ -76,6 +76,7 @@ async def test_dustbin_mode_off(dev: SmartDevice, mocker: MockerFixture): await dev.update() assert dustbin.auto_collection is False + assert dustbin.mode is Mode.Off.name @dustbin