"""Support for Meteoclimatic weather service."""

from typing import TYPE_CHECKING

from meteoclimatic import Condition

from homeassistant.components.weather import WeatherEntity
from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import ATTRIBUTION, CONDITION_MAP, DOMAIN, MANUFACTURER, MODEL
from .coordinator import MeteoclimaticConfigEntry, MeteoclimaticUpdateCoordinator


def format_condition(condition):
    """Return condition from dict CONDITION_MAP."""
    if condition in CONDITION_MAP:
        return CONDITION_MAP[condition]
    if isinstance(condition, Condition):
        return condition.value
    return condition


async def async_setup_entry(
    hass: HomeAssistant,
    entry: MeteoclimaticConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the Meteoclimatic weather platform."""
    coordinator = entry.runtime_data

    async_add_entities([MeteoclimaticWeather(coordinator)], False)


class MeteoclimaticWeather(
    CoordinatorEntity[MeteoclimaticUpdateCoordinator], WeatherEntity
):
    """Representation of a weather condition."""

    _attr_attribution = ATTRIBUTION
    _attr_native_pressure_unit = UnitOfPressure.HPA
    _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
    _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR

    def __init__(self, coordinator: MeteoclimaticUpdateCoordinator) -> None:
        """Initialise the weather platform."""
        super().__init__(coordinator)
        self._attr_unique_id = coordinator.data.station.code
        self._attr_name = coordinator.data.station.name
        if TYPE_CHECKING:
            assert coordinator.config_entry.unique_id is not None
        self._attr_device_info = DeviceInfo(
            entry_type=DeviceEntryType.SERVICE,
            identifiers={(DOMAIN, coordinator.config_entry.unique_id)},
            manufacturer=MANUFACTURER,
            model=MODEL,
            name=coordinator.name,
        )

    @property
    def condition(self) -> str | None:
        """Return the current condition."""
        return format_condition(self.coordinator.data.weather.condition)

    @property
    def native_temperature(self) -> float | None:
        """Return the temperature."""
        return self.coordinator.data.weather.temp_current

    @property
    def humidity(self) -> float | None:
        """Return the humidity."""
        return self.coordinator.data.weather.humidity_current

    @property
    def native_pressure(self) -> float | None:
        """Return the pressure."""
        return self.coordinator.data.weather.pressure_current

    @property
    def native_wind_speed(self) -> float | None:
        """Return the wind speed."""
        return self.coordinator.data.weather.wind_current

    @property
    def wind_bearing(self) -> float | None:
        """Return the wind bearing."""
        return self.coordinator.data.weather.wind_bearing
