forked from python-kasa/python-kasa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add brightness module (python-kasa#806)
Add module for controlling the brightness.
- Loading branch information
Showing
5 changed files
with
73 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Implementation of brightness module.""" | ||
from typing import TYPE_CHECKING, Dict | ||
|
||
from ...feature import Feature, FeatureType | ||
from ..smartmodule import SmartModule | ||
|
||
if TYPE_CHECKING: | ||
from ..smartdevice import SmartDevice | ||
|
||
|
||
class Brightness(SmartModule): | ||
"""Implementation of brightness module.""" | ||
|
||
REQUIRED_COMPONENT = "brightness" | ||
|
||
def __init__(self, device: "SmartDevice", module: str): | ||
super().__init__(device, module) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
"Brightness", | ||
container=self, | ||
attribute_getter="brightness", | ||
attribute_setter="set_brightness", | ||
minimum_value=1, | ||
maximum_value=100, | ||
type=FeatureType.Number, | ||
) | ||
) | ||
|
||
def query(self) -> Dict: | ||
"""Query to execute during the update cycle.""" | ||
# Brightness is contained in the main device info response. | ||
return {} | ||
|
||
@property | ||
def brightness(self): | ||
"""Return current brightness.""" | ||
return self.data["brightness"] | ||
|
||
async def set_brightness(self, brightness: int): | ||
"""Set the brightness.""" | ||
return await self.call("set_device_info", {"brightness": brightness}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import pytest | ||
|
||
from kasa.smart import SmartDevice | ||
from kasa.tests.conftest import parametrize | ||
|
||
from .conftest import ( | ||
brightness, | ||
) | ||
brightness = parametrize("brightness smart", component_filter="brightness") | ||
|
||
|
||
@brightness | ||
async def test_brightness_component(dev: SmartDevice): | ||
"""Placeholder to test framwework component filter.""" | ||
"""Test brightness feature.""" | ||
assert isinstance(dev, SmartDevice) | ||
assert "brightness" in dev._components | ||
|
||
# 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) |