"""Helper and coordinator for refoss."""

from datetime import timedelta
from typing import override

from refoss_ha.controller.device import BaseDevice
from refoss_ha.exceptions import DeviceTimeoutError

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

from .const import DOMAIN, LOGGER, MAX_ERRORS


class RefossDataUpdateCoordinator(DataUpdateCoordinator[None]):
    """Manages polling for state changes from the device."""

    config_entry: ConfigEntry

    def __init__(
        self, hass: HomeAssistant, config_entry: ConfigEntry, device: BaseDevice
    ) -> None:
        """Initialize the data update coordinator."""
        super().__init__(
            hass,
            LOGGER,
            config_entry=config_entry,
            name=f"{DOMAIN}-{device.device_info.dev_name}",
            update_interval=timedelta(seconds=15),
        )
        self.device = device
        self._error_count = 0

    @override
    async def _async_update_data(self) -> None:
        """Update the state of the device."""
        try:
            await self.device.async_handle_update()
            self.last_update_success = True
            self._error_count = 0
        except DeviceTimeoutError:
            LOGGER.debug(
                "Update device %s status timeout,ip: %s",
                self.device.dev_name,
                self.device.inner_ip,
            )
            self._error_count += 1

            if self._error_count >= MAX_ERRORS:
                self.last_update_success = False
