mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Extract Tile coordinator in separate file (#134104)
This commit is contained in:
parent
b9c2b3f7e3
commit
52318f5f37
@ -3,11 +3,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from pytile import async_login
|
from pytile import async_login
|
||||||
from pytile.errors import InvalidAuthError, SessionExpiredError, TileError
|
from pytile.errors import InvalidAuthError, TileError
|
||||||
from pytile.tile import Tile
|
from pytile.tile import Tile
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -15,16 +13,15 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
from homeassistant.util.async_ import gather_with_limited_concurrency
|
from homeassistant.util.async_ import gather_with_limited_concurrency
|
||||||
|
|
||||||
from .const import DOMAIN, LOGGER
|
from .const import DOMAIN
|
||||||
|
from .coordinator import TileCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.DEVICE_TRACKER]
|
PLATFORMS = [Platform.DEVICE_TRACKER]
|
||||||
DEVICE_TYPES = ["PHONE", "TILE"]
|
DEVICE_TYPES = ["PHONE", "TILE"]
|
||||||
|
|
||||||
DEFAULT_INIT_TASK_LIMIT = 2
|
DEFAULT_INIT_TASK_LIMIT = 2
|
||||||
DEFAULT_UPDATE_INTERVAL = timedelta(minutes=2)
|
|
||||||
|
|
||||||
CONF_SHOW_INACTIVE = "show_inactive"
|
CONF_SHOW_INACTIVE = "show_inactive"
|
||||||
|
|
||||||
@ -33,7 +30,7 @@ CONF_SHOW_INACTIVE = "show_inactive"
|
|||||||
class TileData:
|
class TileData:
|
||||||
"""Define an object to be stored in `hass.data`."""
|
"""Define an object to be stored in `hass.data`."""
|
||||||
|
|
||||||
coordinators: dict[str, DataUpdateCoordinator[None]]
|
coordinators: dict[str, TileCoordinator]
|
||||||
tiles: dict[str, Tile]
|
tiles: dict[str, Tile]
|
||||||
|
|
||||||
|
|
||||||
@ -56,30 +53,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except TileError as err:
|
except TileError as err:
|
||||||
raise ConfigEntryNotReady("Error during integration setup") from err
|
raise ConfigEntryNotReady("Error during integration setup") from err
|
||||||
|
|
||||||
async def async_update_tile(tile: Tile) -> None:
|
coordinators: dict[str, TileCoordinator] = {}
|
||||||
"""Update the Tile."""
|
|
||||||
try:
|
|
||||||
await 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 client.async_init()
|
|
||||||
except TileError as err:
|
|
||||||
raise UpdateFailed(f"Error while retrieving data: {err}") from err
|
|
||||||
|
|
||||||
coordinators: dict[str, DataUpdateCoordinator[None]] = {}
|
|
||||||
coordinator_init_tasks = []
|
coordinator_init_tasks = []
|
||||||
|
|
||||||
for tile_uuid, tile in tiles.items():
|
for tile_uuid, tile in tiles.items():
|
||||||
coordinator = coordinators[tile_uuid] = DataUpdateCoordinator(
|
coordinator = coordinators[tile_uuid] = TileCoordinator(hass, client, tile)
|
||||||
hass,
|
|
||||||
LOGGER,
|
|
||||||
config_entry=entry,
|
|
||||||
name=tile.name,
|
|
||||||
update_interval=DEFAULT_UPDATE_INTERVAL,
|
|
||||||
update_method=partial(async_update_tile, tile),
|
|
||||||
)
|
|
||||||
coordinator_init_tasks.append(coordinator.async_refresh())
|
coordinator_init_tasks.append(coordinator.async_refresh())
|
||||||
|
|
||||||
await gather_with_limited_concurrency(
|
await gather_with_limited_concurrency(
|
||||||
|
40
homeassistant/components/tile/coordinator.py
Normal file
40
homeassistant/components/tile/coordinator.py
Normal file
@ -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
|
@ -4,8 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pytile.tile import Tile
|
|
||||||
|
|
||||||
from homeassistant.components.device_tracker import AsyncSeeCallback, TrackerEntity
|
from homeassistant.components.device_tracker import AsyncSeeCallback, TrackerEntity
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
@ -13,13 +11,10 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
CoordinatorEntity,
|
|
||||||
DataUpdateCoordinator,
|
|
||||||
)
|
|
||||||
from homeassistant.util.dt import as_utc
|
from homeassistant.util.dt import as_utc
|
||||||
|
|
||||||
from . import TileData
|
from . import TileCoordinator, TileData
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -43,7 +38,7 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
TileDeviceTracker(entry, data.coordinators[tile_uuid], tile)
|
TileDeviceTracker(entry, data.coordinators[tile_uuid])
|
||||||
for tile_uuid, tile in data.tiles.items()
|
for tile_uuid, tile in data.tiles.items()
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -75,23 +70,21 @@ async def async_setup_scanner(
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class TileDeviceTracker(CoordinatorEntity[DataUpdateCoordinator[None]], TrackerEntity):
|
class TileDeviceTracker(CoordinatorEntity[TileCoordinator], TrackerEntity):
|
||||||
"""Representation of a network infrastructure device."""
|
"""Representation of a network infrastructure device."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
_attr_translation_key = "tile"
|
_attr_translation_key = "tile"
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, entry: ConfigEntry, coordinator: TileCoordinator) -> None:
|
||||||
self, entry: ConfigEntry, coordinator: DataUpdateCoordinator[None], tile: Tile
|
|
||||||
) -> None:
|
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._attr_extra_state_attributes = {}
|
self._attr_extra_state_attributes = {}
|
||||||
self._attr_unique_id = f"{entry.data[CONF_USERNAME]}_{tile.uuid}"
|
self._tile = coordinator.tile
|
||||||
|
self._attr_unique_id = f"{entry.data[CONF_USERNAME]}_{self._tile.uuid}"
|
||||||
self._entry = entry
|
self._entry = entry
|
||||||
self._tile = tile
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user