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:
Teemu R
2024-04-30 08:56:09 +02:00
committed by GitHub
parent d3544b4989
commit 300d823895
7 changed files with 98 additions and 18 deletions

View File

@@ -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: