8000 Add ADC Value to PIR Enabled Switches by ryenitcher · Pull Request #1263 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Add ADC Value to PIR Enabled Switches #1263

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 6 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 17 additions & 5 deletions kasa/cli/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

import asyncclick as click

from kasa import (
Device,
Feature,
)
from kasa import Device, Feature

from .common import (
echo,
Expand Down Expand Up @@ -133,7 +130,22 @@ async def feature(
echo(f"{feat.name} ({name}): {feat.value}{unit}")
return feat.value

value = ast.literal_eval(value)
try:
# Attempt to parse as python literal.
value = ast.literal_eval(value)
except ValueError:
# The value is probably an unquoted string, so we'll raise an error,
# and tell the user to quote the string.
raise click.exceptions.BadParameter(
f'{repr(value)} for {name} (Perhaps you forgot to "quote" the value?)'
) from SyntaxError
except SyntaxError:
# There are likely miss-matched quotes or odd characters in the input,
# so abort and complain to the user.
raise click.exceptions.BadParameter(
f"{repr(value)} for {name}"
) from SyntaxError

echo(f"Changing {name} from {feat.value} to {value}")
response = await dev.features[name].set_value(value)
await dev.update()
Expand Down
23 changes: 20 additions & 3 deletions kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async def set_value(self, value: int | float | bool | str | Enum | None) -> Any:
elif self.type == Feature.Type.Choice: # noqa: SIM102
if not self.choices or value not in self.choices:
raise ValueError(
f"Unexpected value for {self.name}: {value}"
f"Unexpected value for {self.name}: '{value}'"
f" - allowed: {self.choices}"
)

Expand All @@ -279,7 +279,18 @@ def __repr__(self) -> str:
return f"Unable to read value ({self.id}): {ex}"

if self.type == Feature.Type.Choice:
if not isinstance(choices, list) or value not in choices:
if not isinstance(choices, list):
_LOGGER.error(
"Choices are not properly defined for %s (%s). Type: <%s> Value: %s", # noqa: E501
self.name,
self.id,
type(choices),
choices,
)
return f"{self.name} ({self.id}): improperly defined choice set."
if (value not in choices) and (
isinstance(value, Enum) and value.name not in choices
):
_LOGGER.warning(
"Invalid value for for choice %s (%s): %s not in %s",
self.name,
Expand All @@ -291,7 +302,13 @@ def __repr__(self) -> str:
f"{self.name} ({self.id}): invalid value '{value}' not in {choices}"
)
value = " ".join(
[f"*{choice}*" if choice == value else choice for choice in choices]
[
f"*{choice}*"
if choice == value
or (isinstance(value, Enum) and choice == value.name)
else f"{choice}"
for choice in choices
]
)
if self.precision_hint is not None and isinstance(value, float):
value = round(value, self.precision_hint)
Expand Down
Loading
Loading
0