From 8259d28b12a2387ca529a588a4f2668ff979540f Mon Sep 17 00:00:00 2001 From: "Teemu R." Date: Sun, 2 Feb 2025 14:00:49 +0100 Subject: [PATCH] dustbin_mode: add 'off' mode for cleaner downstream impl (#1488) Adds a new artificial "Off" mode for dustbin_mode, which will allow avoiding the need to expose both a toggle and a select in homeassistant. This changes the behavior of the existing mode selection, as it is not anymore possible to change the mode without activating the auto collection. * Mode is Off, if auto collection has been disabled * When setting mode to "Off", this will disable the auto collection * When setting mode to anything else than "Off", the auto collection will be automatically enabled. --- kasa/smart/modules/dustbin.py | 10 ++++++++++ tests/smart/modules/test_dustbin.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/kasa/smart/modules/dustbin.py b/kasa/smart/modules/dustbin.py index 33aecd8f..b2b4d1ef 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 @@ class Dustbin(SmartModule): @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 @@ class Dustbin(SmartModule): "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 d30d2459..ecc68b6b 100644 --- a/tests/smart/modules/test_dustbin.py +++ b/tests/smart/modules/test_dustbin.py @@ -60,6 +60,25 @@ 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 + assert dustbin.mode is Mode.Off.name + + @dustbin async def test_autocollection(dev: SmartDevice, mocker: MockerFixture): """Test autocollection switch."""