From 0c40939624fea4fef2cc482dec45852f2da68e66 Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:53:49 +0000 Subject: [PATCH] Allow callable coroutines for feature setters (#1272) --- kasa/feature.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/kasa/feature.py b/kasa/feature.py index e61cba07..bbf47375 100644 --- a/kasa/feature.py +++ b/kasa/feature.py @@ -71,7 +71,7 @@ import logging 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 @@ -136,10 +136,10 @@ class Feature: 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 @@ -258,11 +258,16 @@ class Feature: f" - allowed: {self.choices}" ) - container = self.container if self.container is not None else self.device - if self.type == Feature.Type.Action: - return await getattr(container, self.attribute_setter)() + 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) - return await getattr(container, self.attribute_setter)(value) + if self.type == Feature.Type.Action: + return await attribute_setter() + + return await attribute_setter(value) def __repr__(self) -> str: try: