Use brightness module for smartbulb (#853)

Moves one more feature out from the smartbulb class
This commit is contained in:
Teemu R 2024-04-20 20:29:07 +02:00 committed by GitHub
parent 29b28966e0
commit 651ad96144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 13 deletions

View File

@ -11,6 +11,10 @@ if TYPE_CHECKING:
from ..smartdevice import SmartDevice from ..smartdevice import SmartDevice
BRIGHTNESS_MIN = 1
BRIGHTNESS_MAX = 100
class Brightness(SmartModule): class Brightness(SmartModule):
"""Implementation of brightness module.""" """Implementation of brightness module."""
@ -25,8 +29,8 @@ class Brightness(SmartModule):
container=self, container=self,
attribute_getter="brightness", attribute_getter="brightness",
attribute_setter="set_brightness", attribute_setter="set_brightness",
minimum_value=1, minimum_value=BRIGHTNESS_MIN,
maximum_value=100, maximum_value=BRIGHTNESS_MAX,
type=FeatureType.Number, type=FeatureType.Number,
) )
) )
@ -43,4 +47,12 @@ class Brightness(SmartModule):
async def set_brightness(self, brightness: int): async def set_brightness(self, brightness: int):
"""Set the brightness.""" """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}) return await self.call("set_device_info", {"brightness": brightness})

View File

@ -6,8 +6,7 @@ from typing import cast
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
from ..exceptions import KasaException from ..exceptions import KasaException
from .modules.colormodule import ColorModule from .modules import Brightness, ColorModule, ColorTemperatureModule
from .modules.colortemp import ColorTemperatureModule
from .smartdevice import SmartDevice from .smartdevice import SmartDevice
AVAILABLE_EFFECTS = { AVAILABLE_EFFECTS = {
@ -157,11 +156,6 @@ class SmartBulb(SmartDevice, Bulb):
ColorTemperatureModule, self.modules["ColorTemperatureModule"] ColorTemperatureModule, self.modules["ColorTemperatureModule"]
).set_color_temp(temp) ).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( async def set_brightness(
self, brightness: int, *, transition: int | None = None self, brightness: int, *, transition: int | None = None
) -> dict: ) -> dict:
@ -175,10 +169,8 @@ class SmartBulb(SmartDevice, Bulb):
if not self.is_dimmable: # pragma: no cover if not self.is_dimmable: # pragma: no cover
raise KasaException("Bulb is not dimmable.") raise KasaException("Bulb is not dimmable.")
self._raise_for_invalid_brightness(brightness) return await cast(Brightness, self.modules["Brightness"]).set_brightness(
brightness
return await self.protocol.query(
{"set_device_info": {"brightness": brightness}}
) )
async def set_effect( async def set_effect(