mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-24 05:37:59 +00:00
Add type hints to feature set_value (#974)
To prevent untyped call mypy errors in consumers
This commit is contained in:
parent
7f24408c32
commit
4cf395483f
@ -10,7 +10,6 @@ from typing import TYPE_CHECKING, Any, Callable
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .device import Device
|
from .device import Device
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -140,22 +139,24 @@ class Feature:
|
|||||||
raise ValueError("Not an action and no attribute_getter set")
|
raise ValueError("Not an action and no attribute_getter set")
|
||||||
|
|
||||||
container = self.container if self.container is not None else self.device
|
container = self.container if self.container is not None else self.device
|
||||||
if isinstance(self.attribute_getter, Callable):
|
if callable(self.attribute_getter):
|
||||||
return self.attribute_getter(container)
|
return self.attribute_getter(container)
|
||||||
return getattr(container, self.attribute_getter)
|
return getattr(container, self.attribute_getter)
|
||||||
|
|
||||||
async def set_value(self, value):
|
async def set_value(self, value: int | float | bool | str | Enum | None) -> Any:
|
||||||
"""Set the value."""
|
"""Set the value."""
|
||||||
if self.attribute_setter is None:
|
if self.attribute_setter is None:
|
||||||
raise ValueError("Tried to set read-only feature.")
|
raise ValueError("Tried to set read-only feature.")
|
||||||
if self.type == Feature.Type.Number: # noqa: SIM102
|
if self.type == Feature.Type.Number: # noqa: SIM102
|
||||||
|
if not isinstance(value, (int, float)):
|
||||||
|
raise ValueError("value must be a number")
|
||||||
if value < self.minimum_value or value > self.maximum_value:
|
if value < self.minimum_value or value > self.maximum_value:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Value {value} out of range "
|
f"Value {value} out of range "
|
||||||
f"[{self.minimum_value}, {self.maximum_value}]"
|
f"[{self.minimum_value}, {self.maximum_value}]"
|
||||||
)
|
)
|
||||||
elif self.type == Feature.Type.Choice: # noqa: SIM102
|
elif self.type == Feature.Type.Choice: # noqa: SIM102
|
||||||
if value not in self.choices:
|
if not self.choices or value not in self.choices:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Unexpected value for {self.name}: {value}"
|
f"Unexpected value for {self.name}: {value}"
|
||||||
f" - allowed: {self.choices}"
|
f" - allowed: {self.choices}"
|
||||||
|
Loading…
Reference in New Issue
Block a user