"""Coordinator for the motionEye integration."""

import logging
from typing import Any, override

from motioneye_client.client import MotionEyeClient, MotionEyeClientError

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

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN

_LOGGER = logging.getLogger(__name__)


type MotionEyeConfigEntry = ConfigEntry[MotionEyeUpdateCoordinator]


class MotionEyeUpdateCoordinator(DataUpdateCoordinator[dict[str, Any] | None]):
    """Coordinator for motionEye data."""

    config_entry: MotionEyeConfigEntry

    def __init__(
        self, hass: HomeAssistant, entry: MotionEyeConfigEntry, client: MotionEyeClient
    ) -> None:
        """Initialize the coordinator."""
        super().__init__(
            hass,
            _LOGGER,
            name=DOMAIN,
            config_entry=entry,
            update_interval=DEFAULT_SCAN_INTERVAL,
        )
        self.client = client

    @override
    async def _async_update_data(self) -> dict[str, Any] | None:
        try:
            return await self.client.async_get_cameras()
        except MotionEyeClientError as exc:
            raise UpdateFailed("Error communicating with API") from exc
