Add unit_getter for feature (#993)

Allow defining getter for unit, necessary to set the correct unit based
on device responses.
This commit is contained in:
Teemu R
2024-06-21 14:51:56 +02:00
committed by GitHub
parent 472008e818
commit ac1e81dc17
2 changed files with 10 additions and 3 deletions

View File

@@ -99,7 +99,7 @@ class Feature:
Choice = auto()
Unknown = -1
# TODO: unsure if this is a great idea..
# Aliases for easy access
Sensor = Type.Sensor
BinarySensor = Type.BinarySensor
Switch = Type.Switch
@@ -139,6 +139,9 @@ class Feature:
icon: str | None = None
#: Unit, if applicable
unit: str | None = None
#: Attribute containing the name of the unit getter property.
#: If set, this property will be used to set *unit*.
unit_getter: str | None = None
#: Category hint for downstreams
category: Feature.Category = Category.Unset
#: Type of the feature
@@ -177,6 +180,10 @@ class Feature:
if self.choices_getter is not None:
self.choices = getattr(container, self.choices_getter)
# Populate unit, if unit_getter is given
if self.unit_getter is not None:
self.unit = getattr(container, self.unit_getter)
# Set the category, if unset
if self.category is Feature.Category.Unset:
if self.attribute_setter: