diff --git a/kasa/smart/modules/brightness.py b/kasa/smart/modules/brightness.py index a783ec3a..1f0b4d99 100644 --- a/kasa/smart/modules/brightness.py +++ b/kasa/smart/modules/brightness.py @@ -11,6 +11,10 @@ if TYPE_CHECKING: from ..smartdevice import SmartDevice +BRIGHTNESS_MIN = 1 +BRIGHTNESS_MAX = 100 + + class Brightness(SmartModule): """Implementation of brightness module.""" @@ -25,8 +29,8 @@ class Brightness(SmartModule): 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 @@ class Brightness(SmartModule): 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 0c654e1f..8da34897 100644 --- a/kasa/smart/smartbulb.py +++ b/kasa/smart/smartbulb.py @@ -6,8 +6,7 @@ from typing import cast 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 @@ class SmartBulb(SmartDevice, Bulb): 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,10 +169,8 @@ class SmartBulb(SmartDevice, Bulb): 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["Brightness"]).set_brightness( + brightness ) async def set_effect(