-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data coordinator to Stookwijzer (#131574)
- Loading branch information
Showing
6 changed files
with
77 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Class representing a Stookwijzer update coordinator.""" | ||
|
||
from datetime import timedelta | ||
|
||
from stookwijzer import Stookwijzer | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .const import DOMAIN, LOGGER | ||
|
||
SCAN_INTERVAL = timedelta(minutes=60) | ||
|
||
type StookwijzerConfigEntry = ConfigEntry[StookwijzerCoordinator] | ||
|
||
|
||
class StookwijzerCoordinator(DataUpdateCoordinator[None]): | ||
"""Stookwijzer update coordinator.""" | ||
|
||
def __init__(self, hass: HomeAssistant, entry: StookwijzerConfigEntry) -> None: | ||
"""Initialize the coordinator.""" | ||
super().__init__( | ||
hass, | ||
LOGGER, | ||
name=DOMAIN, | ||
update_interval=SCAN_INTERVAL, | ||
) | ||
self.client = Stookwijzer( | ||
async_get_clientsession(hass), | ||
entry.data[CONF_LATITUDE], | ||
entry.data[CONF_LONGITUDE], | ||
) | ||
|
||
async def _async_update_data(self) -> None: | ||
"""Fetch data from API endpoint.""" | ||
await self.client.async_update() | ||
if self.client.advice is None: | ||
raise UpdateFailed( | ||
translation_domain=DOMAIN, | ||
translation_key="no_data_received", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters