Skip to content

Commit

Permalink
Add iot brightness feature (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 committed Mar 6, 2024
1 parent 42080bd commit adce92a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions kasa/iot/iotbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..feature import Feature, FeatureType
from ..protocol import BaseProtocol
from .iotdevice import IotDevice, KasaException, requires_update
from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage
Expand Down Expand Up @@ -204,6 +205,22 @@ def __init__(
self.add_module("countdown", Countdown(self, "countdown"))
self.add_module("cloud", Cloud(self, "smartlife.iot.common.cloud"))

async def _initialize_features(self):
await super()._initialize_features()

if bool(self.sys_info["is_dimmable"]): # pragma: no branch
self._add_feature(
Feature(
device=self,
name="Brightness",
attribute_getter="brightness",
attribute_setter="set_brightness",
minimum_value=1,
maximum_value=100,
type=FeatureType.Number,
)
)

@property # type: ignore
@requires_update
def is_color(self) -> bool:
Expand Down
17 changes: 17 additions & 0 deletions kasa/iot/iotdimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..feature import Feature, FeatureType
from ..protocol import BaseProtocol
from .iotdevice import KasaException, requires_update
from .iotplug import IotPlug
Expand Down Expand Up @@ -80,6 +81,22 @@ def __init__(
self.add_module("motion", Motion(self, "smartlife.iot.PIR"))
self.add_module("ambient", AmbientLight(self, "smartlife.iot.LAS"))

async def _initialize_features(self):
await super()._initialize_features()

if "brightness" in self.sys_info: # pragma: no branch
self._add_feature(
Feature(
device=self,
name="Brightness",
attribute_getter="brightness",
attribute_setter="set_brightness",
minimum_value=1,
maximum_value=100,
type=FeatureType.Number,
)
)

@property # type: ignore
@requires_update
def brightness(self) -> int:
Expand Down
3 changes: 3 additions & 0 deletions kasa/iot/iotplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def __init__(
self.add_module("time", Time(self, "time"))
self.add_module("cloud", Cloud(self, "cnCloud"))

async def _initialize_features(self):
await super()._initialize_features()

self._add_feature(
Feature(
device=self,
Expand Down
25 changes: 24 additions & 1 deletion kasa/tests/test_feature_brightness.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest

from kasa.iot import IotDevice
from kasa.smart import SmartDevice
from kasa.tests.conftest import parametrize
from kasa.tests.conftest import dimmable, parametrize

brightness = parametrize("brightness smart", component_filter="brightness")

Expand All @@ -26,3 +27,25 @@ async def test_brightness_component(dev: SmartDevice):

with pytest.raises(ValueError):
await feature.set_value(feature.maximum_value + 10)


@dimmable
async def test_brightness_dimmable(dev: SmartDevice):
"""Test brightness feature."""
assert isinstance(dev, IotDevice)
assert "brightness" in dev.sys_info or bool(dev.sys_info["is_dimmable"])

# Test getting the value
feature = dev.features["brightness"]
assert isinstance(feature.value, int)
assert feature.value > 0 and feature.value <= 100

# Test setting the value
await feature.set_value(10)
assert feature.value == 10

with pytest.raises(ValueError):
await feature.set_value(feature.minimum_value - 10)

with pytest.raises(ValueError):
await feature.set_value(feature.maximum_value + 10)

0 comments on commit adce92a

Please sign in to comment.