Skip to content

Commit

Permalink
fix: more fixes to energy and power sensors (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
woopstar authored Oct 18, 2024
1 parent 1ddedcb commit e5d8b13
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def extra_state_attributes(self):
"max_age_days": self._max_age.days,
}

async def async_update(self):
async def _handle_update(self, event):

# Find energy sensor from unique id
self._hsem_energy_sensor_entity = await async_resolve_entity_id_from_unique_id(
Expand Down Expand Up @@ -128,7 +128,11 @@ async def async_added_to_hass(self):
try:
self._state = round(convert_to_float(old_state.state), 2)
self._last_updated = old_state.attributes.get("last_updated", None)
# self._samples = old_state.attributes.get("samples")
#self._samples = old_state.attributes.get("samples")
except (ValueError, TypeError):
_LOGGER.warning(f"Invalid old state value for {self.name}")
self._state = 0.0

async def async_update(self, event=None):
"""Manually trigger the sensor update."""
await self._handle_update(event=None)
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def extra_state_attributes(self):
}

async def async_added_to_hass(self):
await super().async_added_to_hass()

old_state = await self.async_get_last_state()
if old_state is not None:
Expand All @@ -77,7 +76,7 @@ async def async_added_to_hass(self):
self._last_reset_date = datetime.now().date()
self._last_updated = None

async def async_update(self):
async def _handle_update(self, event):
now = datetime.now()

# Tjek om vi skal nulstille (hvis dagen er ændret)
Expand Down Expand Up @@ -135,3 +134,7 @@ async def async_update(self):

# Update Home Assistant state
self.async_write_ha_state()

async def async_update(self, event=None):
"""Manually trigger the sensor update."""
await self._handle_update(event=None)
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ async def _handle_update(self, event):
# Trigger an update in Home Assistant
self.async_write_ha_state()

async def async_update(self):
async def async_update(self, event=None):
"""Manually trigger the sensor update."""
await self._handle_update(event=None)

0 comments on commit e5d8b13

Please sign in to comment.