GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Implement action feature by rytilahti · Pull Request #849 · python-kasa/python-kasa

Implement action feature #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
rytilahti merged 2 commits into master from feat/button_feature
Apr 23, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Implement action feature #849

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
3eeb986
Implement button feature
rytilahti Apr 20, 2024
3483451
Rename Button to Action
rytilahti Apr 23, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

12 changes: 10 additions & 2 deletions 12 kasa/feature.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FeatureType(Enum):
Sensor = auto()
BinarySensor = auto()
Switch = auto()
Button = auto()
Action = auto()
Number = auto()


Expand All @@ -29,7 +29,7 @@ class Feature:
#: User-friendly short description
name: str
#: Name of the property that allows accessing the value
attribute_getter: str | Callable
attribute_getter: str | Callable | None = None
#: Name of the method that allows changing the value
attribute_setter: str | None = None
#: Container storing the data, this overrides 'device' for getters
Expand Down Expand Up @@ -59,6 +59,11 @@ def __post_init__(self):
@property
def value(self):
"""Return the current value."""
if self.type == FeatureType.Action:
return "<Action>"
if self.attribute_getter is None:
raise ValueError("Not an action and no attribute_getter set")

container = self.container if self.container is not None else self.device
if isinstance(self.attribute_getter, Callable):
return self.attribute_getter(container)
Expand All @@ -76,4 +81,7 @@ async def set_value(self, value):
)

container = self.container if self.container is not None else self.device
if self.type == FeatureType.Action:
return await getattr(container, self.attribute_setter)()

return await getattr(container, self.attribute_setter)(value)
22 changes: 20 additions & 2 deletions 22 kasa/smart/modules/alarmmodule.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ def __init__(self, device: SmartDevice, module: str):
device, "Alarm volume", container=self, attribute_getter="alarm_volume"
)
)
self._add_feature(
Feature(
device,
"Test alarm",
container=self,
attribute_setter="play",
type=FeatureType.Action,
)
)
self._add_feature(
Feature(
device,
"Stop alarm",
container=self,
attribute_setter="stop",
type=FeatureType.Action,
)
)

@property
def alarm_sound(self):
Expand Down Expand Up @@ -83,8 +101,8 @@ def source(self) -> str | None:

async def play(self):
"""Play alarm."""
return self.call("play_alarm")
return await self.call("play_alarm")

async def stop(self):
"""Stop alarm."""
return self.call("stop_alarm")
return await self.call("stop_alarm")
22 changes: 20 additions & 2 deletions 22 kasa/tests/test_feature.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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]
Expand Down Expand Up @@ -77,3 +79,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()
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.