"""Интеграция: устройства из Buryans Hub как device_tracker.""" import logging import aiohttp from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN, SCAN_INTERVAL _LOGGER = logging.getLogger(__name__) PLATFORMS = ["device_tracker"] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: url = entry.data["url"].rstrip("/") async def _fetch(): try: timeout = aiohttp.ClientTimeout(total=20) async with aiohttp.ClientSession(timeout=timeout) as session: async with session.get(f"{url}/data/devices") as resp: if resp.status != 200: raise UpdateFailed(f"HTTP {resp.status}") data = await resp.json() except UpdateFailed: raise except Exception as e: # noqa: BLE001 raise UpdateFailed(str(e)) from e items = data.get("items") or [] payload = (items[0].get("payload") if items else {}) or {} devices = payload.get("devices") or [] return {d.get("mac") or d.get("ip"): d for d in devices if d.get("mac") or d.get("ip")} coordinator = DataUpdateCoordinator( hass, _LOGGER, name=DOMAIN, update_method=_fetch, update_interval=SCAN_INTERVAL ) await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data.get(DOMAIN, {}).pop(entry.entry_id, None) return unload_ok