FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow callable coroutines for feature setters by sdb9696 · Pull Request #1272 · python-kasa/python-kasa · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 12 additions & 7 deletions kasa/feature.py
This file contains hidden or 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
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 @@ class Category(Enum):
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 @@ async def set_value(self, value: int | float | bool | str | Enum | None) -> Any:
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
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

Back | FazBrowse Home | New Git URL