"""DataUpdateCoordinator for PerfectDraft.""" from __future__ import annotations from datetime import timedelta import logging from typing import Any from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, UpdateFailed, ) from .api import PerfectDraftApiClient from .const import ( CONF_MACHINE_ID, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL, DOMAIN, ) from .exceptions import ( AuthenticationError, PerfectDraftApiError, PerfectDraftConnectionError, ) _LOGGER = logging.getLogger(__name__) class PerfectDraftDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Single coordinator that polls the PerfectDraft API for all entities.""" config_entry: ConfigEntry def __init__( self, hass: HomeAssistant, client: PerfectDraftApiClient, config_entry: ConfigEntry, ) -> None: self.client = client interval = config_entry.options.get( CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL ) super().__init__( hass, _LOGGER, name=DOMAIN, update_interval=timedelta(seconds=interval), config_entry=config_entry, ) def update_interval_from_options(self) -> None: """Re-read the polling interval from config entry options.""" interval = self.config_entry.options.get( CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL ) self.update_interval = timedelta(seconds=interval) _LOGGER.debug("Polling interval updated to %s seconds", interval) async def _async_update_data(self) -> dict[str, Any]: machine_id = self.config_entry.data.get(CONF_MACHINE_ID) try: if not machine_id: profile = await self.client.get_user_profile() machines = profile.get("perfectdraftMachines", []) if not machines: raise UpdateFailed( "No PerfectDraft machine found on this account" ) machine_id = str(machines[0].get("id", "")) details = await self.client.get_machine_details(machine_id) except AuthenticationError as err: raise ConfigEntryAuthFailed( "Authentication failed — please re-authenticate" ) from err except (PerfectDraftApiError, PerfectDraftConnectionError) as err: raise UpdateFailed(str(err)) from err details["_machine_id"] = machine_id # The active keg lives behind a separate serialization group. Treat it # as best-effort so a failure here doesn't take the whole device down. try: active = await self.client.get_machine_active_keg(machine_id) details["kegActive"] = active.get("kegActive") except (PerfectDraftApiError, PerfectDraftConnectionError) as err: _LOGGER.debug("Active keg lookup failed: %s", err) return details