From 651ad96144cc5053399a077f985a067298c1cc9a Mon Sep 17 00:00:00 2001 From: Teemu R Date: Sat, 20 Apr 2024 20:29:07 +0200 Subject: [PATCH] Use brightness module for smartbulb (#853) Moves one more feature out from the smartbulb class --- kasa/smart/modules/brightness.py | 16 ++++++++++++++-- kasa/smart/smartbulb.py | 14 +++----------- 2 files changed, 17 insertions(+), 13 deletions(-) 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(