-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator.py
48 lines (37 loc) · 1.49 KB
/
coordinator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Coordinator for Cudy Router integration."""
from datetime import timedelta
import logging
from typing import Any
import async_timeout
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from .router import CudyRouter
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
class CudyRouterDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""Get the latest data from the router."""
config_entry: ConfigEntry
def __init__(
self, hass: HomeAssistant, entry: ConfigEntry, api: CudyRouter
) -> None:
"""Initialize router data."""
self.hass = hass
self.config_entry = entry
self.host: str = entry.data[CONF_HOST]
self.api = api
scan_interval = (entry.options and entry.options.get(CONF_SCAN_INTERVAL)) or 15
super().__init__(
hass,
_LOGGER,
name=f"{DOMAIN} - {self.host}",
update_interval=timedelta(seconds=scan_interval),
)
async def _async_update_data(self) -> dict[str, Any]:
"""Get the latest data from the router."""
async with async_timeout.timeout(30):
try:
return await self.api.get_data(self.hass, self.config_entry.options)
except Exception as err:
raise UpdateFailed from err