"""Sensor entities for Tami4Edge."""

import logging
from typing import override

from homeassistant.components.sensor import (
    SensorDeviceClass,
    SensorEntity,
    SensorEntityDescription,
    SensorStateClass,
)
from homeassistant.const import UnitOfVolume
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .coordinator import Tami4ConfigEntry, Tami4EdgeCoordinator
from .entity import Tami4EdgeBaseEntity

_LOGGER = logging.getLogger(__name__)

ENTITY_DESCRIPTIONS = [
    SensorEntityDescription(
        key="uv_upcoming_replacement",
        translation_key="uv_upcoming_replacement",
        device_class=SensorDeviceClass.DATE,
    ),
    SensorEntityDescription(
        key="uv_installed",
        translation_key="uv_installed",
    ),
    SensorEntityDescription(
        key="filter_upcoming_replacement",
        translation_key="filter_upcoming_replacement",
        device_class=SensorDeviceClass.DATE,
    ),
    SensorEntityDescription(
        key="filter_installed",
        translation_key="filter_installed",
    ),
    SensorEntityDescription(
        key="filter_litters_passed",
        translation_key="filter_litters_passed",
        state_class=SensorStateClass.TOTAL,
        device_class=SensorDeviceClass.WATER,
        native_unit_of_measurement=UnitOfVolume.LITERS,
    ),
]


async def async_setup_entry(
    hass: HomeAssistant,
    entry: Tami4ConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Perform the setup for Tami4Edge."""
    coordinator = entry.runtime_data

    async_add_entities(
        Tami4EdgeSensorEntity(
            coordinator=coordinator,
            entity_description=entity_description,
        )
        for entity_description in ENTITY_DESCRIPTIONS
    )


class Tami4EdgeSensorEntity(
    Tami4EdgeBaseEntity,
    CoordinatorEntity[Tami4EdgeCoordinator],
    SensorEntity,
):
    """Representation of the entity."""

    def __init__(
        self,
        coordinator: Tami4EdgeCoordinator,
        entity_description: SensorEntityDescription,
    ) -> None:
        """Initialize the Tami4Edge sensor entity."""
        Tami4EdgeBaseEntity.__init__(self, coordinator.api, entity_description)
        CoordinatorEntity.__init__(self, coordinator)
        self._update_attr()

    def _update_attr(self) -> None:
        self._attr_native_value = getattr(
            self.coordinator.data, self.entity_description.key
        )

    @callback
    @override
    def _handle_coordinator_update(self) -> None:
        """Handle updated data from the coordinator."""
        self._update_attr()
        self.async_write_ha_state()
