Add iot brightness feature (#808)

This commit is contained in:
Steven B 2024-03-06 16:45:08 +00:00 committed by GitHub
parent 42080bd954
commit adce92a761
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 1 deletions

View File

@ -12,6 +12,7 @@ except ImportError:
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
from ..device_type import DeviceType from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig from ..deviceconfig import DeviceConfig
from ..feature import Feature, FeatureType
from ..protocol import BaseProtocol from ..protocol import BaseProtocol
from .iotdevice import IotDevice, KasaException, requires_update from .iotdevice import IotDevice, KasaException, requires_update
from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage
@ -204,6 +205,22 @@ class IotBulb(IotDevice, Bulb):
self.add_module("countdown", Countdown(self, "countdown")) self.add_module("countdown", Countdown(self, "countdown"))
self.add_module("cloud", Cloud(self, "smartlife.iot.common.cloud")) 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 @property # type: ignore
@requires_update @requires_update
def is_color(self) -> bool: def is_color(self) -> bool:

View File

@ -4,6 +4,7 @@ from typing import Any, Dict, Optional
from ..device_type import DeviceType from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig from ..deviceconfig import DeviceConfig
from ..feature import Feature, FeatureType
from ..protocol import BaseProtocol from ..protocol import BaseProtocol
from .iotdevice import KasaException, requires_update from .iotdevice import KasaException, requires_update
from .iotplug import IotPlug from .iotplug import IotPlug
@ -80,6 +81,22 @@ class IotDimmer(IotPlug):
self.add_module("motion", Motion(self, "smartlife.iot.PIR")) self.add_module("motion", Motion(self, "smartlife.iot.PIR"))
self.add_module("ambient", AmbientLight(self, "smartlife.iot.LAS")) 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 @property # type: ignore
@requires_update @requires_update
def brightness(self) -> int: def brightness(self) -> int:

View File

@ -57,6 +57,9 @@ class IotPlug(IotDevice):
self.add_module("time", Time(self, "time")) self.add_module("time", Time(self, "time"))
self.add_module("cloud", Cloud(self, "cnCloud")) self.add_module("cloud", Cloud(self, "cnCloud"))
async def _initialize_features(self):
await super()._initialize_features()
self._add_feature( self._add_feature(
Feature( Feature(
device=self, device=self,

View File

@ -1,7 +1,8 @@
import pytest import pytest
from kasa.iot import IotDevice
from kasa.smart import SmartDevice 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") brightness = parametrize("brightness smart", component_filter="brightness")
@ -26,3 +27,25 @@ async def test_brightness_component(dev: SmartDevice):
with pytest.raises(ValueError): with pytest.raises(ValueError):
await feature.set_value(feature.maximum_value + 10) 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)