Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add type hints to feature set_value #974

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
Jun 12, 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
9 changes: 5 additions & 4 deletions kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
if TYPE_CHECKING:
from .device import Device


_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -140,22 +139,24 @@
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):
if callable(self.attribute_getter):
return self.attribute_getter(container)
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."""
if self.attribute_setter is None:
raise ValueError("Tried to set read-only feature.")
if self.type == Feature.Type.Number: # noqa: SIM102
if not isinstance(value, (int, float)):
raise ValueError("value must be a number")

Check warning on line 152 in kasa/feature.py

View check run for this annotation

Codecov / codecov/patch

kasa/feature.py#L152

Added line #L152 was not covered by tests
if value < self.minimum_value or value > self.maximum_value:
raise ValueError(
f"Value {value} out of range "
f"[{self.minimum_value}, {self.maximum_value}]"
)
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(
f"Unexpected value for {self.name}: {value}"
f" - allowed: {self.choices}"
Expand Down
Loading