Support for on_off_gradually v2+ (#793)

Previously, only v1 of on_off_gradually is supported, and the newer versions are not backwards compatible.
This PR adds support for the newer versions of the component, and implements `number` type for `Feature` to expose the transition time selection.
This also adds a new `supported_version` property to the main module API.
This commit is contained in:
Teemu R
2024-02-24 02:16:43 +01:00
committed by GitHub
parent a73e2a9ede
commit cbf82c9498
4 changed files with 159 additions and 16 deletions

View File

@@ -14,6 +14,7 @@ class FeatureType(Enum):
BinarySensor = auto()
Switch = auto()
Button = auto()
Number = auto()
@dataclass
@@ -35,6 +36,12 @@ class Feature:
#: Type of the feature
type: FeatureType = FeatureType.Sensor
# Number-specific attributes
#: Minimum value
minimum_value: int = 0
#: Maximum value
maximum_value: int = 2**16 # Arbitrary max
@property
def value(self):
"""Return the current value."""
@@ -47,5 +54,12 @@ class Feature:
"""Set the value."""
if self.attribute_setter is None:
raise ValueError("Tried to set read-only feature.")
if self.type == FeatureType.Number: # noqa: SIM102
if value < self.minimum_value or value > self.maximum_value:
raise ValueError(
f"Value {value} out of range "
f"[{self.minimum_value}, {self.maximum_value}]"
)
container = self.container if self.container is not None else self.device
return await getattr(container, self.attribute_setter)(value)