Require explicit feature type (#1006)

Explicit > implicit. Having this previously would have avoided using a
wrong type for water_alert in the first place.
This commit is contained in:
Teemu R
2024-06-25 18:30:36 +02:00
committed by GitHub
parent 5846bbdbbb
commit 0f5bafaa43
15 changed files with 45 additions and 10 deletions

View File

@@ -129,6 +129,8 @@ class Feature:
id: str
#: User-friendly short description
name: str
#: Type of the feature
type: Feature.Type
#: Name of the property that allows accessing the value
attribute_getter: str | Callable | None = None
#: Name of the method that allows changing the value
@@ -144,8 +146,6 @@ class Feature:
unit_getter: str | None = None
#: Category hint for downstreams
category: Feature.Category = Category.Unset
#: Type of the feature
type: Feature.Type = Type.Sensor
# Display hints offer a way suggest how the value should be shown to users
#: Hint to help rounding the sensor values to given after-comma digits
@@ -191,14 +191,19 @@ class Feature:
else:
self.category = Feature.Category.Info
if self.category == Feature.Category.Config and self.type in [
if self.type in (
Feature.Type.Sensor,
Feature.Type.BinarySensor,
]:
raise ValueError(
f"Invalid type for configurable feature: {self.name} ({self.id}):"
f" {self.type}"
)
):
if self.category == Feature.Category.Config:
raise ValueError(
f"Invalid type for configurable feature: {self.name} ({self.id}):"
f" {self.type}"
)
elif self.attribute_setter is not None:
raise ValueError(
f"Read-only feat defines attribute_setter: {self.name} ({self.id}):"
)
@property
def value(self):