mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Implement choice feature type (#880)
Implement the choice feature type allowing to provide a list of choices that can be set. Co-authored-by: sdb9696
This commit is contained in:
@@ -92,6 +92,13 @@ class Feature:
|
||||
#: If set, this property will be used to set *minimum_value* and *maximum_value*.
|
||||
range_getter: str | None = None
|
||||
|
||||
# Choice-specific attributes
|
||||
#: List of choices as enum
|
||||
choices: list[str] | None = None
|
||||
#: Attribute name of the choices getter property.
|
||||
#: If set, this property will be used to set *choices*.
|
||||
choices_getter: str | None = None
|
||||
|
||||
#: Identifier
|
||||
id: str | None = None
|
||||
|
||||
@@ -108,6 +115,10 @@ class Feature:
|
||||
container, self.range_getter
|
||||
)
|
||||
|
||||
# Populate choices, if choices_getter is given
|
||||
if self.choices_getter is not None:
|
||||
self.choices = getattr(container, self.choices_getter)
|
||||
|
||||
# Set the category, if unset
|
||||
if self.category is Feature.Category.Unset:
|
||||
if self.attribute_setter:
|
||||
@@ -147,6 +158,12 @@ class Feature:
|
||||
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:
|
||||
raise ValueError(
|
||||
f"Unexpected value for {self.name}: {value}"
|
||||
f" - allowed: {self.choices}"
|
||||
)
|
||||
|
||||
container = self.container if self.container is not None else self.device
|
||||
if self.type == Feature.Type.Action:
|
||||
|
Reference in New Issue
Block a user