From e5d8b13b9cab8ffd096fcb27061daa5e794ea4b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kr=C3=BCger?= Date: Fri, 18 Oct 2024 14:03:24 +0200 Subject: [PATCH] fix: more fixes to energy and power sensors (#12) --- .../house_consumption_energy_average_sensor.py | 8 ++++++-- .../custom_sensors/house_consumption_energy_sensor.py | 7 +++++-- .../hsem/custom_sensors/house_consumption_power_sensor.py | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/custom_components/hsem/custom_sensors/house_consumption_energy_average_sensor.py b/custom_components/hsem/custom_sensors/house_consumption_energy_average_sensor.py index 2fc8a1b..7827f76 100644 --- a/custom_components/hsem/custom_sensors/house_consumption_energy_average_sensor.py +++ b/custom_components/hsem/custom_sensors/house_consumption_energy_average_sensor.py @@ -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( @@ -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) diff --git a/custom_components/hsem/custom_sensors/house_consumption_energy_sensor.py b/custom_components/hsem/custom_sensors/house_consumption_energy_sensor.py index db799db..27087e7 100644 --- a/custom_components/hsem/custom_sensors/house_consumption_energy_sensor.py +++ b/custom_components/hsem/custom_sensors/house_consumption_energy_sensor.py @@ -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: @@ -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) @@ -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) diff --git a/custom_components/hsem/custom_sensors/house_consumption_power_sensor.py b/custom_components/hsem/custom_sensors/house_consumption_power_sensor.py index 4598fe3..b74b046 100644 --- a/custom_components/hsem/custom_sensors/house_consumption_power_sensor.py +++ b/custom_components/hsem/custom_sensors/house_consumption_power_sensor.py @@ -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)