-
-
Notifications
You must be signed in to change notification settings - Fork 29k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataUpdateCoordinator to pyLoad integration (#120237)
* Add DataUpdateCoordinator * Update tests * changes * changes * test coverage * some changes * Update homeassistant/components/pyload/sensor.py * use dataclass * fix ConfigEntry * fix configtype * fix some issues * remove logger * remove unnecessary else * revert fixture changes --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
- Loading branch information
Showing
6 changed files
with
101 additions
and
257 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,78 @@ | ||
"""Update coordinator for pyLoad Integration.""" | ||
|
||
from dataclasses import dataclass | ||
from datetime import timedelta | ||
import logging | ||
|
||
from pyloadapi import CannotConnect, InvalidAuth, ParserError, PyLoadAPI | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryError | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .const import DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
SCAN_INTERVAL = timedelta(seconds=20) | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class pyLoadData: | ||
"""Data from pyLoad.""" | ||
|
||
pause: bool | ||
active: int | ||
queue: int | ||
total: int | ||
speed: float | ||
download: bool | ||
reconnect: bool | ||
captcha: bool | ||
free_space: int | ||
|
||
|
||
class PyLoadCoordinator(DataUpdateCoordinator[pyLoadData]): | ||
"""pyLoad coordinator.""" | ||
|
||
config_entry: ConfigEntry | ||
|
||
def __init__(self, hass: HomeAssistant, pyload: PyLoadAPI) -> None: | ||
"""Initialize pyLoad coordinator.""" | ||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name=DOMAIN, | ||
update_interval=SCAN_INTERVAL, | ||
) | ||
self.pyload = pyload | ||
self.version: str | None = None | ||
|
||
async def _async_update_data(self) -> pyLoadData: | ||
"""Fetch data from API endpoint.""" | ||
try: | ||
if not self.version: | ||
self.version = await self.pyload.version() | ||
return pyLoadData( | ||
**await self.pyload.get_status(), | ||
free_space=await self.pyload.free_space(), | ||
) | ||
|
||
except InvalidAuth as e: | ||
try: | ||
await self.pyload.login() | ||
except InvalidAuth as exc: | ||
raise ConfigEntryError( | ||
f"Authentication failed for {self.pyload.username}, check your login credentials", | ||
) from exc | ||
|
||
raise UpdateFailed( | ||
"Unable to retrieve data due to cookie expiration but re-authentication was successful." | ||
) from e | ||
except CannotConnect as e: | ||
raise UpdateFailed( | ||
"Unable to connect and retrieve data from pyLoad API" | ||
) from e | ||
except ParserError as e: | ||
raise UpdateFailed("Unable to parse data from pyLoad API") from e |
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
Oops, something went wrong.