8000 Add colortemp module · python-kasa/python-kasa@ca26d15 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca26d15

Browse files
committed
Add colortemp module
1 parent a4b2e2c commit ca26d15

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

kasa/feature.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ class Feature:
4141
minimum_value: int = 0
4242
#: Maximum value
4343
maximum_value: int = 2**16 # Arbitrary max
44+
#: Attribute containing the name of the range getter property.
45+
#: If set, this property will be used to set *minimum_value* and *maximum_value*.
46+
range_getter: Optional[str] = None
47+
48+
def __post_init__(self):
49+
"""Handle late-binding of members."""
50+
container = self.container if self.container is not None else self.device
51+
if self.range_getter is not None:
52+
self.minimum_value, self.maximum_value = getattr(
53+
container, self.range_getter
54+
)
4455

4556
@property
4657
def value(self):

kasa/smart/modules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .brightness import Brightness
66
from .childdevicemodule import ChildDeviceModule
77
from .cloudmodule import CloudModule
8+
from .colortemp import ColorTemperatureModule
89
from .devicemodule import DeviceModule
910
from .energymodule import EnergyModule
1011
from .firmware import Firmware
@@ -31,4 +32,5 @@
3132
"Firmware",
3233
"CloudModule",
3334
"LightTransitionModule",
35+
"ColorTemperatureModule",
3436
]

kasa/smart/modules/colortemp.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Implementation of color temp module."""
2+
from typing import TYPE_CHECKING, Dict
3+
4+
from ...feature import Feature
5+
from ..smartmodule import SmartModule
6+
from ...bulb import ColorTempRange
7+
8+
if TYPE_CHECKING:
9+
from ..smartdevice import SmartDevice
10+
11+
12+
class ColorTemperatureModule(SmartModule):
13+
"""Implementation of color temp module."""
14+
15+
REQUIRED_COMPONENT = "color_temperature"
16+
17+
def __init__(self, device: "SmartDevice", module: str):
18+
super().__init__(device, module)
19+
self._add_feature(
20+
Feature(
21+
device,
22+
"Color temperature",
23+
container=self,
24+
attribute_getter="color_temp",
25+
attribute_setter="set_color_temp",
26+
range_getter="valid_temperature_range",
27+
)
28+
)
29+
30+
def query(self) -> Dict:
31+
"""Query to execute during the update cycle."""
32+
# Color temp is contained in the main device info response.
33+
return {}
34+
35+
@property
36+
def valid_temperature_range(self) -> ColorTempRange:
37+
"""Return valid color-temp range."""
38+
return ColorTempRange(*self.data.get("color_temp_range"))
39+
40+
@property
41+
def color_temp(self):
42+
"""Return current color temperature."""
43+
return self.data["color_temp"]
44+
45+
async def set_color_temp(self, temp: int):
46+
"""Set the color temperature."""
47+
valid_temperature_range = self.valid_temperature_range
48+
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
49+
raise ValueError(
50+
"Temperature should be between {} and {}, was {}".format(
51+
*valid_temperature_range, temp
52+
)
53+
)
54+
55+
return await self.call("set_device_info", {"color_temp": temp})

kasa/tests/test_feature_colortemp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from kasa.smart import SmartDevice
4+
from kasa.tests.conftest import parametrize
5+
6+
brightness = parametrize("colortemp smart", component_filter="color_temperature")
7+
8+
9+
@brightness
10+
async def test_colortemp_component(dev: SmartDevice):
11+
"""Test brightness feature."""
12+
assert isinstance(dev, SmartDevice)
13+
assert "color_temperature" in dev._components
14+
15+
# Test getting the value
16+
feature = dev.features["color_temperature"]
17+
assert isinstance(feature.value, int)
18+
assert isinstance(feature.minimum_value, int)
19+
assert isinstance(feature.maximum_value, int)
20+
21+
# Test setting the value
22+
# We need to take the min here, as L9xx reports a range [9000, 9000].
23+
new_value = min(feature.minimum_value + 1, feature.maximum_value)
24+
await feature.set_value(new_value)
25+
assert feature.value == new_value
26+
27+
with pytest.raises(ValueError):
28+
await feature.set_value(feature.minimum_value - 10)
29+
30+
with pytest.raises(ValueError):
31+
await feature.set_value(feature.maximum_value + 10)

0 commit comments

Comments
 (0)
0