From 19bfc35ca456269a28fd5653367f423b8f01f960 Mon Sep 17 00:00:00 2001 From: FL550 Date: Tue, 6 Feb 2024 12:15:16 +0100 Subject: [PATCH] Implement automatic update of listeners on data update --- custom_components/dwd_weather/connector.py | 11 ++++++++++- custom_components/dwd_weather/const.py | 2 +- custom_components/dwd_weather/manifest.json | 2 +- custom_components/dwd_weather/weather.py | 9 +++------ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/custom_components/dwd_weather/connector.py b/custom_components/dwd_weather/connector.py index 8c10502..5a38962 100644 --- a/custom_components/dwd_weather/connector.py +++ b/custom_components/dwd_weather/connector.py @@ -65,6 +65,7 @@ def __init__(self, hass, config_entry: ConfigEntry): self.forecast = None self.latest_update = None self.infos = {} + self.entities = [] # Holds the current data from DWD self.dwd_weather = dwdforecast.Weather(self._config[CONF_STATION_ID]) @@ -74,9 +75,14 @@ def __init__(self, hass, config_entry: ConfigEntry): int(self.dwd_weather.station["elev"]), ) + def register_entity(self, entity): + self.entities.append(entity) + async def async_update(self): """Async wrapper for update method.""" - return await self._hass.async_add_executor_job(self._update) + if await self._hass.async_add_executor_job(self._update): + for entity in self.entities: + await entity.async_update_listeners(("daily", "hourly")) def _update(self): """Get the latest data from DWD.""" @@ -132,6 +138,9 @@ def _update(self): self.infos[ATTR_STATION_ID] = self._config[CONF_STATION_ID] self.infos[ATTR_STATION_NAME] = self._config[CONF_STATION_NAME] _LOGGER.debug("Forecast data {}".format(self.dwd_weather.forecast_data)) + return True + else: + return False def get_forecast(self, WeatherEntityFeature_FORECAST) -> list[Forecast] | None: if WeatherEntityFeature_FORECAST == WeatherEntityFeature.FORECAST_HOURLY: diff --git a/custom_components/dwd_weather/const.py b/custom_components/dwd_weather/const.py index 4655b47..27b02ca 100644 --- a/custom_components/dwd_weather/const.py +++ b/custom_components/dwd_weather/const.py @@ -13,7 +13,7 @@ Platform.SENSOR, Platform.WEATHER, ] -INTEGRATION_VERSION = "2.1.0" +INTEGRATION_VERSION = "2.1.2" MIN_REQUIRED_HA_VERSION = "2023.10.1" ATTR_LATEST_UPDATE = "latest_update_utc" diff --git a/custom_components/dwd_weather/manifest.json b/custom_components/dwd_weather/manifest.json index f04fcb3..f153245 100644 --- a/custom_components/dwd_weather/manifest.json +++ b/custom_components/dwd_weather/manifest.json @@ -1,6 +1,6 @@ { "domain": "dwd_weather", - "version": "2.1.1", + "version": "2.1.2", "name": "Deutscher Wetterdienst (DWD)", "documentation": "https://github.com/FL550/dwd_weather", "issue_tracker": "https://github.com/FL550/dwd_weather/issues", diff --git a/custom_components/dwd_weather/weather.py b/custom_components/dwd_weather/weather.py index e8afa61..73a764d 100644 --- a/custom_components/dwd_weather/weather.py +++ b/custom_components/dwd_weather/weather.py @@ -44,10 +44,11 @@ class DWDWeather(DWDWeatherEntity, WeatherEntity): def __init__(self, entry_data, hass_data): """Initialise the platform with a data instance and site.""" - dwd_data: DWDWeatherData = hass_data[DWDWEATHER_DATA] + self._dwd_data: DWDWeatherData = hass_data[DWDWEATHER_DATA] self._coordinator = hass_data[DWDWEATHER_COORDINATOR] + self._dwd_data.register_entity(self) - unique_id = f"{dwd_data._config[CONF_STATION_ID]}_{dwd_data._config[CONF_STATION_NAME]}_Weather" + unique_id = f"{self._dwd_data._config[CONF_STATION_ID]}_{self._dwd_data._config[CONF_STATION_NAME]}_Weather" _LOGGER.debug("Setting up weather with id {}".format(unique_id)) super().__init__(hass_data, unique_id) @@ -151,7 +152,3 @@ async def async_added_to_hass(self) -> None: self.async_on_remove( self._coordinator.async_add_listener(self.async_write_ha_state) ) - - async def async_update(self) -> None: - """Get the latest data and updates the states.""" - await self._coordinator.async_request_refresh()