Skip to content

Commit

Permalink
refactor: make better use of HASS sensor base class
Browse files Browse the repository at this point in the history
- Don't re-implement sensor property methods unless it's really
  necessary. Instead, implement the values that the existing property
  methods depend on.
- Fix automatic temperature conversions not working
  • Loading branch information
jason0x43 committed Dec 29, 2023
1 parent d6bd105 commit e78b7da
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 87 deletions.
7 changes: 7 additions & 0 deletions custom_components/hubitat/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def get_attr(self, attr: str) -> float | int | str | None:
return self._device.attributes[attr].value
return None

@callback
def get_attr_unit(self, attr: str) -> str | None:
"""Get the unit of an attribute."""
if attr in self._device.attributes:
return self._device.attributes[attr].unit
return None

@callback
def get_float_attr(self, attr: str) -> float | None:
"""Get the current value of an attribute."""
Expand Down
9 changes: 5 additions & 4 deletions custom_components/hubitat/hubitatmaker/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ def values(self) -> list[str] | None:
return None
return self._properties["values"]

def update_value(self, value: str | float) -> None:
def update_value(self, value: str | float, unit: str | None = None) -> None:
self._properties["currentValue"] = value
self._properties["unit"] = unit

@property
def unit(self) -> str:
return self._properties["unit"]
def unit(self) -> str | None:
return self._properties.get("unit")

def __iter__(self):
for key in "name", "type", "value", "unit":
Expand Down Expand Up @@ -98,7 +99,7 @@ def update_attr(
self, attr_name: str, value: str | int, value_unit: str | None
) -> None:
attr = self.attributes[attr_name]
attr.update_value(value)
attr.update_value(value, value_unit)
self._last_update = time()

def update_state(self, properties: dict[str, Any]) -> None:
Expand Down
Loading

0 comments on commit e78b7da

Please sign in to comment.