2022-04-27 01:10:23 +02:00
|
|
|
"""The QNAP QSW coordinator."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2022-05-09 19:57:27 +02:00
|
|
|
from typing import Any, cast
|
2022-04-27 01:10:23 +02:00
|
|
|
|
|
|
|
from aioqsw.exceptions import QswError
|
|
|
|
from aioqsw.localapi import QnapQswApi
|
|
|
|
import async_timeout
|
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
|
|
|
from .const import DOMAIN, QSW_TIMEOUT_SEC
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-05-09 19:57:27 +02:00
|
|
|
class QswUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
2022-04-27 01:10:23 +02:00
|
|
|
"""Class to manage fetching data from the QNAP QSW device."""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, qsw: QnapQswApi) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
self.qsw = qsw
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=DOMAIN,
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
|
2022-05-10 09:59:10 +02:00
|
|
|
async def _async_update_data(self) -> dict[str, Any]:
|
2022-04-27 01:10:23 +02:00
|
|
|
"""Update data via library."""
|
|
|
|
async with async_timeout.timeout(QSW_TIMEOUT_SEC):
|
|
|
|
try:
|
|
|
|
await self.qsw.update()
|
|
|
|
except QswError as error:
|
|
|
|
raise UpdateFailed(error) from error
|
2022-05-09 19:57:27 +02:00
|
|
|
return cast(dict[str, Any], self.qsw.data())
|