"""Home Assistant hardware firmware update coordinator."""

from datetime import timedelta
import logging
from typing import override

from aiohttp import ClientSession
from ha_silabs_firmware_client import (
    FirmwareManifest,
    FirmwareUpdateClient,
    ManifestMissing,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

_LOGGER = logging.getLogger(__name__)


FIRMWARE_REFRESH_INTERVAL = timedelta(hours=8)


class FirmwareUpdateCoordinator(DataUpdateCoordinator[FirmwareManifest]):
    """Coordinator to manage firmware updates."""

    def __init__(
        self,
        hass: HomeAssistant,
        config_entry: ConfigEntry,
        session: ClientSession,
        url: str,
    ) -> None:
        """Initialize the firmware update coordinator."""
        super().__init__(
            hass,
            _LOGGER,
            name="firmware update coordinator",
            update_interval=FIRMWARE_REFRESH_INTERVAL,
            config_entry=config_entry,
        )
        self.session = session

        self.client = FirmwareUpdateClient(url, session)

    @override
    async def _async_update_data(self) -> FirmwareManifest:
        try:
            return await self.client.async_update_data()
        except ManifestMissing as err:
            raise UpdateFailed(
                "GitHub release assets haven't been uploaded yet"
            ) from err
