forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Tile coordinator in separate file (home-assistant#134104)
- Loading branch information
Showing
3 changed files
with
53 additions
and
42 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,40 @@ | ||
"""Update coordinator for Tile.""" | ||
|
||
from datetime import timedelta | ||
|
||
from pytile.api import API | ||
from pytile.errors import InvalidAuthError, SessionExpiredError, TileError | ||
from pytile.tile import Tile | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryAuthFailed | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .const import LOGGER | ||
|
||
|
||
class TileCoordinator(DataUpdateCoordinator[None]): | ||
"""Define an object to coordinate Tile data retrieval.""" | ||
|
||
def __init__(self, hass: HomeAssistant, client: API, tile: Tile) -> None: | ||
"""Initialize.""" | ||
super().__init__( | ||
hass, | ||
LOGGER, | ||
name=tile.name, | ||
update_interval=timedelta(minutes=2), | ||
) | ||
self.tile = tile | ||
self.client = client | ||
|
||
async def _async_update_data(self) -> None: | ||
"""Update data via library.""" | ||
try: | ||
await self.tile.async_update() | ||
except InvalidAuthError as err: | ||
raise ConfigEntryAuthFailed("Invalid credentials") from err | ||
except SessionExpiredError: | ||
LOGGER.debug("Tile session expired; creating a new one") | ||
await self.client.async_init() | ||
except TileError as err: | ||
raise UpdateFailed(f"Error while retrieving data: {err}") from err |
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