Skip to content

Commit

Permalink
Adds update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jan 12, 2024
1 parent deef387 commit 19c8914
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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.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",
Expand Down
12 changes: 12 additions & 0 deletions custom_components/dwd_weather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
CONF_STATION_ID,
CONF_STATION_NAME,
DOMAIN,
DWDWEATHER_COORDINATOR,
DWDWEATHER_DATA,
)

Expand Down Expand Up @@ -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]}"
Expand Down Expand Up @@ -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()
12 changes: 12 additions & 0 deletions custom_components/dwd_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
CONF_STATION_ID,
CONF_STATION_NAME,
DOMAIN,
DWDWEATHER_COORDINATOR,
DWDWEATHER_DATA,
)

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

0 comments on commit 19c8914

Please sign in to comment.