diff --git a/custom_components/dwd_weather/manifest.json b/custom_components/dwd_weather/manifest.json index bb03531..f04fcb3 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.0", + "version": "2.1.1", "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/sensor.py b/custom_components/dwd_weather/sensor.py index 0ba7589..1071764 100644 --- a/custom_components/dwd_weather/sensor.py +++ b/custom_components/dwd_weather/sensor.py @@ -38,6 +38,7 @@ CONF_STATION_ID, CONF_STATION_NAME, DOMAIN, + DWDWEATHER_COORDINATOR, DWDWEATHER_DATA, ) @@ -270,6 +271,7 @@ class DWDWeatherForecastSensor(DWDWeatherEntity, SensorEntity): def __init__(self, entry_data, hass_data, sensor_type): """Initialize the sensor.""" dwd_data: DWDWeatherData = hass_data[DWDWEATHER_DATA] + self._coordinator = hass_data[DWDWEATHER_COORDINATOR] self._type = sensor_type # name = f"{dwd_data._config[CONF_STATION_NAME]}: {SENSOR_TYPES[self._type][0]}" @@ -422,3 +424,13 @@ def entity_registry_enabled_default(self) -> bool: def available(self): """Return if state is available.""" return self._connector.latest_update is not None + + async def async_added_to_hass(self) -> None: + """Connect to dispatcher listening for entity data notifications.""" + 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() diff --git a/custom_components/dwd_weather/weather.py b/custom_components/dwd_weather/weather.py index 5b69a1e..e8afa61 100644 --- a/custom_components/dwd_weather/weather.py +++ b/custom_components/dwd_weather/weather.py @@ -22,6 +22,7 @@ CONF_STATION_ID, CONF_STATION_NAME, DOMAIN, + DWDWEATHER_COORDINATOR, DWDWEATHER_DATA, ) @@ -44,6 +45,7 @@ def __init__(self, entry_data, hass_data): """Initialise the platform with a data instance and site.""" dwd_data: DWDWeatherData = hass_data[DWDWEATHER_DATA] + self._coordinator = hass_data[DWDWEATHER_COORDINATOR] unique_id = f"{dwd_data._config[CONF_STATION_ID]}_{dwd_data._config[CONF_STATION_NAME]}_Weather" _LOGGER.debug("Setting up weather with id {}".format(unique_id)) @@ -143,3 +145,13 @@ def attribution(self): def extra_state_attributes(self): """Return data validity infos.""" return self._connector.infos + + async def async_added_to_hass(self) -> None: + """Connect to dispatcher listening for entity data notifications.""" + 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()