Implement action feature (#849)

Adds `FeatureType.Action` making it possible to expose features like
"reboot", "test alarm", "pair" etc.

The `attribute_getter` is no longer mandatory, but it will raise an
exception if not defined for other types than actions.
Trying to read returns a static string `<Action>`.
This overloads the `set_value` to call the given callable on any value.

This also fixes the `play` and `stop` coroutines of the alarm module to
await the call.
This commit is contained in:
Teemu R
2024-04-23 19:49:04 +02:00
committed by GitHub
parent b860c32d5f
commit 6e5cae1f47
3 changed files with 50 additions and 6 deletions

View File

@@ -3,11 +3,13 @@ import pytest
from kasa import Feature, FeatureType
class DummyDevice:
pass
@pytest.fixture
def dummy_feature() -> Feature:
# create_autospec for device slows tests way too much, so we use a dummy here
class DummyDevice:
pass
feat = Feature(
device=DummyDevice(), # type: ignore[arg-type]
@@ -79,3 +81,19 @@ async def test_feature_setter_read_only(dummy_feature):
dummy_feature.attribute_setter = None
with pytest.raises(ValueError):
await dummy_feature.set_value("value for read only feature")
async def test_feature_action(mocker):
"""Test that setting value on button calls the setter."""
feat = Feature(
device=DummyDevice(), # type: ignore[arg-type]
name="dummy_feature",
attribute_setter="call_action",
container=None,
icon="mdi:dummy",
type=FeatureType.Action,
)
mock_call_action = mocker.patch.object(feat.device, "call_action", create=True)
assert feat.value == "<Action>"
await feat.set_value(1234)
mock_call_action.assert_called()