Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add unit_getter for feature #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Type(Enum):
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
Expand Down Expand Up @@ -139,6 +139,9 @@ class Category(Enum):
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
Expand Down Expand Up @@ -177,6 +180,10 @@ def __post_init__(self):
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:
Expand Down
4 changes: 2 additions & 2 deletions kasa/smart/modules/temperaturesensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, device: SmartDevice, module: str):
attribute_getter="temperature",
icon="mdi:thermometer",
category=Feature.Category.Primary,
unit_getter="temperature_unit",
)
)
if "current_temp_exception" in device.sys_info:
Expand Down Expand Up @@ -55,7 +56,6 @@ def __init__(self, device: SmartDevice, module: str):
choices=["celsius", "fahrenheit"],
)
)
# TODO: use temperature_unit for feature creation

@property
def temperature(self):
Expand All @@ -68,7 +68,7 @@ def temperature_warning(self) -> bool:
return self._device.sys_info.get("current_temp_exception", 0) != 0

@property
def temperature_unit(self):
def temperature_unit(self) -> Literal["celsius", "fahrenheit"]:
"""Return current temperature unit."""
return self._device.sys_info["temp_unit"]

Expand Down
Loading