8000 Allow callable coroutines for feature setters by sdb9696 · Pull Request #1272 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Allow callable coroutines for feature setters #1272

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.

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
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from dataclasses import dataclass
from enum import Enum, auto
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any, Callable, Coroutine

if TYPE_CHECKING:
from .device import Device
Expand Down Expand Up @@ -136,10 +136,10 @@
name: str
#: Type of the feature
type: Feature.Type
#: Name of the property that allows accessing the value
#: Callable or name of the property that allows accessing the value
attribute_getter: str | Callable | None = None
#: Name of the method that allows changing the value
attribute_setter: str | None = None
#: Callable coroutine or name of the method that allows changing the value
attribute_setter: str | Callable[..., Coroutine[Any, Any, Any]] | None = None
#: Container storing the data, this overrides 'device' for getters
container: Any = None
#: Icon suggestion
Expand Down Expand Up @@ -258,11 +258,16 @@
f" - allowed: {self.choices}"
)

container = self.container if self.container is not None else self.device
if callable(self.attribute_setter):
attribute_setter = self.attribute_setter

Check warning on line 262 in kasa/feature.py

View check run for this annotation

Codecov / codecov/patch

kasa/feature.py#L262

Added line #L262 was not covered by tests
else:
container = self.container if self.container is not None else self.device
attribute_setter = getattr(container, self.attribute_setter)

if self.type == Feature.Type.Action:
return await getattr(container, self.attribute_setter)()
return await attribute_setter()

return await getattr(container, self.attribute_setter)(value)
return await attribute_setter(value)

def __repr__(self) -> str:
try:
Expand Down
Loading
0