Skip to content

Commit

Permalink
Implement automatic update of listeners on data update
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Feb 6, 2024
1 parent 19c8914 commit 19bfc35
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
11 changes: 10 additions & 1 deletion custom_components/dwd_weather/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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."""
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/dwd_weather/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/dwd_weather/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 3 additions & 6 deletions custom_components/dwd_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

0 comments on commit 19bfc35

Please sign in to comment.