2024-02-15 15:25:08 +00:00
|
|
|
"""Generic interface for defining device features."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
import logging
|
2024-02-15 15:25:08 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from enum import Enum, auto
|
2024-04-17 13:39:24 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, Callable
|
2024-02-15 15:25:08 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .device import Device
|
|
|
|
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2024-02-15 15:25:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Feature:
|
|
|
|
"""Feature defines a generic interface for device features."""
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
class Type(Enum):
|
|
|
|
"""Type to help decide how to present the feature."""
|
|
|
|
|
|
|
|
#: Sensor is an informative read-only value
|
|
|
|
Sensor = auto()
|
|
|
|
#: BinarySensor is a read-only boolean
|
|
|
|
BinarySensor = auto()
|
|
|
|
#: Switch is a boolean setting
|
|
|
|
Switch = auto()
|
|
|
|
#: Action triggers some action on device
|
|
|
|
Action = auto()
|
|
|
|
#: Number defines a numeric setting
|
|
|
|
#: See :ref:`range_getter`, :ref:`minimum_value`, and :ref:`maximum_value`
|
|
|
|
Number = auto()
|
|
|
|
#: Choice defines a setting with pre-defined values
|
|
|
|
Choice = auto()
|
|
|
|
Unknown = -1
|
|
|
|
|
|
|
|
# TODO: unsure if this is a great idea..
|
|
|
|
Sensor = Type.Sensor
|
|
|
|
BinarySensor = Type.BinarySensor
|
|
|
|
Switch = Type.Switch
|
|
|
|
Action = Type.Action
|
|
|
|
Number = Type.Number
|
|
|
|
Choice = Type.Choice
|
|
|
|
|
2024-04-23 17:20:12 +00:00
|
|
|
class Category(Enum):
|
2024-04-24 16:38:52 +00:00
|
|
|
"""Category hint to allow feature grouping."""
|
2024-04-23 17:20:12 +00:00
|
|
|
|
|
|
|
#: Primary features control the device state directly.
|
2024-04-24 16:38:52 +00:00
|
|
|
#: Examples include turning the device on/off, or adjusting its brightness.
|
2024-04-23 17:20:12 +00:00
|
|
|
Primary = auto()
|
|
|
|
#: Config features change device behavior without immediate state changes.
|
|
|
|
Config = auto()
|
|
|
|
#: Informative/sensor features deliver some potentially interesting information.
|
|
|
|
Info = auto()
|
|
|
|
#: Debug features deliver more verbose information then informative features.
|
|
|
|
#: You may want to hide these per default to avoid cluttering your UI.
|
|
|
|
Debug = auto()
|
|
|
|
#: The default category if none is specified.
|
|
|
|
Unset = -1
|
|
|
|
|
2024-02-15 15:25:08 +00:00
|
|
|
#: Device instance required for getting and setting values
|
2024-04-17 13:39:24 +00:00
|
|
|
device: Device
|
2024-02-15 15:25:08 +00:00
|
|
|
#: User-friendly short description
|
|
|
|
name: str
|
|
|
|
#: Name of the property that allows accessing the value
|
2024-04-23 17:49:04 +00:00
|
|
|
attribute_getter: str | Callable | None = None
|
2024-02-15 15:25:08 +00:00
|
|
|
#: Name of the method that allows changing the value
|
2024-04-17 13:39:24 +00:00
|
|
|
attribute_setter: str | None = None
|
2024-02-15 15:25:08 +00:00
|
|
|
#: Container storing the data, this overrides 'device' for getters
|
|
|
|
container: Any = None
|
|
|
|
#: Icon suggestion
|
2024-04-17 13:39:24 +00:00
|
|
|
icon: str | None = None
|
2024-04-22 09:25:30 +00:00
|
|
|
#: Unit, if applicable
|
|
|
|
unit: str | None = None
|
2024-04-23 17:20:12 +00:00
|
|
|
#: Category hint for downstreams
|
|
|
|
category: Feature.Category = Category.Unset
|
2024-02-15 15:25:08 +00:00
|
|
|
#: Type of the feature
|
2024-04-24 16:38:52 +00:00
|
|
|
type: Feature.Type = Type.Sensor
|
2024-02-15 15:25:08 +00:00
|
|
|
|
2024-04-29 11:31:42 +00:00
|
|
|
# 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
|
|
|
|
precision_hint: int | None = None
|
|
|
|
|
2024-02-24 01:16:43 +00:00
|
|
|
# Number-specific attributes
|
|
|
|
#: Minimum value
|
|
|
|
minimum_value: int = 0
|
|
|
|
#: Maximum value
|
|
|
|
maximum_value: int = 2**16 # Arbitrary max
|
2024-03-15 16:36:07 +00:00
|
|
|
#: Attribute containing the name of the range getter property.
|
|
|
|
#: If set, this property will be used to set *minimum_value* and *maximum_value*.
|
2024-04-17 13:39:24 +00:00
|
|
|
range_getter: str | None = None
|
2024-03-15 16:36:07 +00:00
|
|
|
|
2024-04-23 17:20:12 +00:00
|
|
|
#: Identifier
|
|
|
|
id: str | None = None
|
|
|
|
|
2024-03-15 16:36:07 +00:00
|
|
|
def __post_init__(self):
|
|
|
|
"""Handle late-binding of members."""
|
2024-04-23 17:20:12 +00:00
|
|
|
# Set id, if unset
|
|
|
|
if self.id is None:
|
|
|
|
self.id = self.name.lower().replace(" ", "_")
|
|
|
|
|
|
|
|
# Populate minimum & maximum values, if range_getter is given
|
2024-03-15 16:36:07 +00:00
|
|
|
container = self.container if self.container is not None else self.device
|
|
|
|
if self.range_getter is not None:
|
|
|
|
self.minimum_value, self.maximum_value = getattr(
|
|
|
|
container, self.range_getter
|
|
|
|
)
|
2024-02-24 01:16:43 +00:00
|
|
|
|
2024-04-23 17:20:12 +00:00
|
|
|
# Set the category, if unset
|
|
|
|
if self.category is Feature.Category.Unset:
|
|
|
|
if self.attribute_setter:
|
|
|
|
self.category = Feature.Category.Config
|
|
|
|
else:
|
|
|
|
self.category = Feature.Category.Info
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
if self.category == Feature.Category.Config and self.type in [
|
|
|
|
Feature.Type.Sensor,
|
|
|
|
Feature.Type.BinarySensor,
|
|
|
|
]:
|
|
|
|
raise ValueError(
|
|
|
|
f"Invalid type for configurable feature: {self.name} ({self.id}):"
|
|
|
|
f" {self.type}"
|
|
|
|
)
|
|
|
|
|
2024-02-15 15:25:08 +00:00
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
"""Return the current value."""
|
2024-04-24 16:38:52 +00:00
|
|
|
if self.type == Feature.Type.Action:
|
2024-04-23 17:49:04 +00:00
|
|
|
return "<Action>"
|
|
|
|
if self.attribute_getter is None:
|
|
|
|
raise ValueError("Not an action and no attribute_getter set")
|
|
|
|
|
2024-02-15 15:25:08 +00:00
|
|
|
container = self.container if self.container is not None else self.device
|
|
|
|
if isinstance(self.attribute_getter, Callable):
|
|
|
|
return self.attribute_getter(container)
|
|
|
|
return getattr(container, self.attribute_getter)
|
|
|
|
|
|
|
|
async def set_value(self, value):
|
|
|
|
"""Set the value."""
|
|
|
|
if self.attribute_setter is None:
|
|
|
|
raise ValueError("Tried to set read-only feature.")
|
2024-04-24 16:38:52 +00:00
|
|
|
if self.type == Feature.Type.Number: # noqa: SIM102
|
2024-02-24 01:16:43 +00:00
|
|
|
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}]"
|
|
|
|
)
|
|
|
|
|
2024-02-19 20:11:11 +00:00
|
|
|
container = self.container if self.container is not None else self.device
|
2024-04-24 16:38:52 +00:00
|
|
|
if self.type == Feature.Type.Action:
|
2024-04-23 17:49:04 +00:00
|
|
|
return await getattr(container, self.attribute_setter)()
|
|
|
|
|
2024-02-19 20:11:11 +00:00
|
|
|
return await getattr(container, self.attribute_setter)(value)
|
2024-04-23 17:20:12 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2024-04-29 11:31:42 +00:00
|
|
|
value = self.value
|
|
|
|
if self.precision_hint is not None and value is not None:
|
|
|
|
value = round(self.value, self.precision_hint)
|
|
|
|
s = f"{self.name} ({self.id}): {value}"
|
2024-04-23 17:20:12 +00:00
|
|
|
if self.unit is not None:
|
|
|
|
s += f" {self.unit}"
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
if self.type == Feature.Type.Number:
|
2024-04-23 17:20:12 +00:00
|
|
|
s += f" (range: {self.minimum_value}-{self.maximum_value})"
|
|
|
|
|
|
|
|
return s
|