From ff6ad84e18fd7ad11b207701062716b4e98a72c0 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sat, 20 Apr 2024 19:14:12 +0200 Subject: [PATCH 1/2] Use brightness module for smartbulb Moves one more feature out from the smartbulb class --- kasa/smart/modules/brightness.py | 16 ++++++++++++++-- kasa/smart/smartbulb.py | 16 ++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/kasa/smart/modules/brightness.py b/kasa/smart/modules/brightness.py index a783ec3aa..1f0b4d995 100644 --- a/kasa/smart/modules/brightness.py +++ b/kasa/smart/modules/brightness.py @@ -11,6 +11,10 @@ from ..smartdevice import SmartDevice +BRIGHTNESS_MIN = 1 +BRIGHTNESS_MAX = 100 + + class Brightness(SmartModule): """Implementation of brightness module.""" @@ -25,8 +29,8 @@ def __init__(self, device: SmartDevice, module: str): container=self, attribute_getter="brightness", attribute_setter="set_brightness", - minimum_value=1, - maximum_value=100, + minimum_value=BRIGHTNESS_MIN, + maximum_value=BRIGHTNESS_MAX, type=FeatureType.Number, ) ) @@ -43,4 +47,12 @@ def brightness(self): async def set_brightness(self, brightness: int): """Set the brightness.""" + if not isinstance(brightness, int) or not ( + BRIGHTNESS_MIN <= brightness <= BRIGHTNESS_MAX + ): + raise ValueError( + f"Invalid brightness value: {brightness} " + f"(valid range: {BRIGHTNESS_MIN}-{BRIGHTNESS_MAX}%)" + ) + return await self.call("set_device_info", {"brightness": brightness}) diff --git a/kasa/smart/smartbulb.py b/kasa/smart/smartbulb.py index 0c654e1ff..5aacc77a3 100644 --- a/kasa/smart/smartbulb.py +++ b/kasa/smart/smartbulb.py @@ -6,8 +6,7 @@ from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange from ..exceptions import KasaException -from .modules.colormodule import ColorModule -from .modules.colortemp import ColorTemperatureModule +from .modules import Brightness, ColorModule, ColorTemperatureModule from .smartdevice import SmartDevice AVAILABLE_EFFECTS = { @@ -157,11 +156,6 @@ async def set_color_temp( ColorTemperatureModule, self.modules["ColorTemperatureModule"] ).set_color_temp(temp) - def _raise_for_invalid_brightness(self, value: int): - """Raise error on invalid brightness value.""" - if not isinstance(value, int) or not (1 <= value <= 100): - raise ValueError(f"Invalid brightness value: {value} (valid range: 1-100%)") - async def set_brightness( self, brightness: int, *, transition: int | None = None ) -> dict: @@ -175,11 +169,9 @@ async def set_brightness( if not self.is_dimmable: # pragma: no cover raise KasaException("Bulb is not dimmable.") - self._raise_for_invalid_brightness(brightness) - - return await self.protocol.query( - {"set_device_info": {"brightness": brightness}} - ) + return await cast( + Brightness, self.modules["ColorTemperatureModule"] + ).set_brightness(brightness) async def set_effect( self, From d577e9dbf30bc0ca4af85cd8301994ac3377187e Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sat, 20 Apr 2024 20:09:36 +0200 Subject: [PATCH 2/2] Fix copy-paste oopsie --- kasa/smart/smartbulb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kasa/smart/smartbulb.py b/kasa/smart/smartbulb.py index 5aacc77a3..8da348977 100644 --- a/kasa/smart/smartbulb.py +++ b/kasa/smart/smartbulb.py @@ -169,9 +169,9 @@ async def set_brightness( if not self.is_dimmable: # pragma: no cover raise KasaException("Bulb is not dimmable.") - return await cast( - Brightness, self.modules["ColorTemperatureModule"] - ).set_brightness(brightness) + return await cast(Brightness, self.modules["Brightness"]).set_brightness( + brightness + ) async def set_effect( self,