8000 Add ADC Value to PIR Enabled Switches (#1263) · python-kasa/python-kasa@7f2a1be · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f2a1be

Browse files
authored
Add ADC Value to PIR Enabled Switches (#1263)
1 parent 0aa1242 commit 7f2a1be

File tree

7 files changed

+491
-39
lines changed

7 files changed

+491
-39
lines changed

kasa/cli/feature.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
import asyncclick as click
88

9-
from kasa import (
10-
Device,
11-
Feature,
12-
)
9+
from kasa import Device, Feature
1310

1411
from .common import (
1512
echo,
@@ -133,7 +130,22 @@ async def feature(
133130
echo(f"{feat.name} ({name}): {feat.value}{unit}")
134131
return feat.value
135132

136-
value = ast.literal_eval(value)
133+
try:
134+
# Attempt to parse as python literal.
135+
value = ast.literal_eval(value)
136+
except ValueError:
137+
# The value is probably an unquoted string, so we'll raise an error,
138+
# and tell the user to quote the string.
139+
raise click.exceptions.BadParameter(
140+
f'{repr(value)} for {name} (Perhaps you forgot to "quote" the value?)'
141+
) from SyntaxError
142+
except SyntaxError:
143+
# There are likely miss-matched quotes or odd characters in the input,
144+
# so abort and complain to the user.
145+
raise click.exceptions.BadParameter(
146+
f"{repr(value)} for {name}"
147+
) from SyntaxError
148+
137149
echo(f"Changing {name} from {feat.value} to {value}")
138150
response = await dev.features[name].set_value(value)
139151
await dev.update()

kasa/feature.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ async def set_value(self, value: int | float | bool | str | Enum | None) -> Any:
256256
elif self.type == Feature.Type.Choice: # noqa: SIM102
257257
if not self.choices or value not in self.choices:
258258
raise ValueError(
259-
f"Unexpected value for {self.name}: {value}"
259+
f"Unexpected value for {self.name}: '{value}'"
260260
f" - allowed: {self.choices}"
261261
)
262262

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

281281
if self.type == Feature.Type.Choice:
282-
if not isinstance(choices, list) or value not in choices:
282+
if not isinstance(choices, list):
283+
_LOGGER.error(
284+
"Choices are not properly defined for %s (%s). Type: <%s> Value: %s", # noqa: E501
285+
self.name,
286+
self.id,
287+
type(choices),
288+
choices,
289+
)
290+
return f"{self.name} ({self.id}): improperly defined choice set."
291+
if (value not in choices) and (
292+
isinstance(value, Enum) and value.name not in choices
293+
):
283294
_LOGGER.warning(
284295
"Invalid value for for choice %s (%s): %s not in %s",
285296
self.name,
@@ -291,7 +302,13 @@ def __repr__(self) -> str:
291302
f"{self.name} ({self.id}): invalid value '{value}' not in {choices}"
292303
)
293304
value = " ".join(
294-
[f"*{choice}*" if choice == value else choice for choice in choices]
305+
[
306+
f"*{choice}*"
307+
if choice == value
308+
or (isinstance(value, Enum) and choice == value.name)
309+
else f"{choice}"
310+
for choice in choices
311+
]
295312
)
296313
if self.precision_hint is not None and isinstance(value, float):
297314
value = round(value, self.precision_hint)

0 commit comments

Comments
 (0)
0