From 69231dbc1ed78f193f5cd669a9259bc1c113690a Mon Sep 17 00:00:00 2001 From: St4n <2866240+St4n@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:12:03 +0200 Subject: [PATCH 1/2] Adapt to 2023.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adapted from patch done by kamaradclimber on rte ecowatt : HA/core removes the async keyword on a method called "async" 🤦 This patch fixes it at the expense of not restoring "extra" attributes (such as "generation time", "period start", "period end" and "level code") which is very sad. --- custom_components/signal_ecogaz/__init__.py | 22 ++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/custom_components/signal_ecogaz/__init__.py b/custom_components/signal_ecogaz/__init__.py index e1f4ae7..9cea057 100644 --- a/custom_components/signal_ecogaz/__init__.py +++ b/custom_components/signal_ecogaz/__init__.py @@ -124,13 +124,21 @@ def restored(self): async def async_added_to_hass(self): await super().async_added_to_hass() _LOGGER.debug("starting to restore sensor from previous data") - if (last_stored_state := await self._async_get_restored_data()) is not None: - old_state = last_stored_state.state.as_dict() - _LOGGER.debug(f"restored state: {old_state}") - self._state = old_state["state"] - for key, value in old_state["attributes"].items(): - self._attr_extra_state_attributes[key] = value - self.coordinator.last_update_success = True + + if (extra_stored_data := await self.async_get_last_sensor_data()) is not None: + if ( + extra_stored_data.native_value != "unknown" + or self.restore_even_if_unknown() + ): + _LOGGER.debug(f"Restoring state for {self.unique_id}") + self._attr_native_value = extra_stored_data.native_value + self._attr_native_unit_of_measurement = extra_stored_data.native_unit_of_measurement + # sadly it seems as of 2023.6 we don't have any way to get back the additional attributes + self.coordinator.last_update_success = True + else: + # by not restoring state, we allow the Coordinator to fetch data again and fill + # data as soon as possible + _LOGGER.debug(f"Stored state was 'unknown', starting from scratch") # signal restoration happened self._restored = True From 451a1d4a1953919cf5b3b427866d4aff093e9230 Mon Sep 17 00:00:00 2001 From: St4n <2866240+St4n@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:30:01 +0200 Subject: [PATCH 2/2] Update __init__.py Wrong ident --- custom_components/signal_ecogaz/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/signal_ecogaz/__init__.py b/custom_components/signal_ecogaz/__init__.py index 9cea057..7239db1 100644 --- a/custom_components/signal_ecogaz/__init__.py +++ b/custom_components/signal_ecogaz/__init__.py @@ -135,7 +135,7 @@ async def async_added_to_hass(self): self._attr_native_unit_of_measurement = extra_stored_data.native_unit_of_measurement # sadly it seems as of 2023.6 we don't have any way to get back the additional attributes self.coordinator.last_update_success = True - else: + else: # by not restoring state, we allow the Coordinator to fetch data again and fill # data as soon as possible _LOGGER.debug(f"Stored state was 'unknown', starting from scratch")