Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions kasa/smart/modules/brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from ..smartdevice import SmartDevice


BRIGHTNESS_MIN = 1
BRIGHTNESS_MAX = 100


class Brightness(SmartModule):
"""Implementation of brightness module."""

Expand All @@ -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,
)
)
Expand All @@ -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})
14 changes: 3 additions & 11 deletions kasa/smart/smartbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:
Expand All @@ -175,10 +169,8 @@ 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["Brightness"]).set_brightness(
brightness
)

async def set_effect(
Expand Down