"""Support for deCONZ climate devices."""
from __future__ import annotations

from collections.abc import ValuesView
from typing import Any

from pydeconz.sensor import (
    THERMOSTAT_FAN_MODE_AUTO,
    THERMOSTAT_FAN_MODE_HIGH,
    THERMOSTAT_FAN_MODE_LOW,
    THERMOSTAT_FAN_MODE_MEDIUM,
    THERMOSTAT_FAN_MODE_OFF,
    THERMOSTAT_FAN_MODE_ON,
    THERMOSTAT_FAN_MODE_SMART,
    THERMOSTAT_MODE_AUTO,
    THERMOSTAT_MODE_COOL,
    THERMOSTAT_MODE_HEAT,
    THERMOSTAT_MODE_OFF,
    THERMOSTAT_PRESET_AUTO,
    THERMOSTAT_PRESET_BOOST,
    THERMOSTAT_PRESET_COMFORT,
    THERMOSTAT_PRESET_COMPLEX,
    THERMOSTAT_PRESET_ECO,
    THERMOSTAT_PRESET_HOLIDAY,
    THERMOSTAT_PRESET_MANUAL,
    Thermostat,
)

from homeassistant.components.climate import DOMAIN, ClimateEntity
from homeassistant.components.climate.const import (
    FAN_AUTO,
    FAN_HIGH,
    FAN_LOW,
    FAN_MEDIUM,
    FAN_OFF,
    FAN_ON,
    HVAC_MODE_AUTO,
    HVAC_MODE_COOL,
    HVAC_MODE_HEAT,
    HVAC_MODE_OFF,
    PRESET_BOOST,
    PRESET_COMFORT,
    PRESET_ECO,
    SUPPORT_FAN_MODE,
    SUPPORT_PRESET_MODE,
    SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import ATTR_LOCKED, ATTR_OFFSET, ATTR_VALVE
from .deconz_device import DeconzDevice
from .gateway import DeconzGateway, get_gateway_from_config_entry

DECONZ_FAN_SMART = "smart"

FAN_MODE_TO_DECONZ = {
    DECONZ_FAN_SMART: THERMOSTAT_FAN_MODE_SMART,
    FAN_AUTO: THERMOSTAT_FAN_MODE_AUTO,
    FAN_HIGH: THERMOSTAT_FAN_MODE_HIGH,
    FAN_MEDIUM: THERMOSTAT_FAN_MODE_MEDIUM,
    FAN_LOW: THERMOSTAT_FAN_MODE_LOW,
    FAN_ON: THERMOSTAT_FAN_MODE_ON,
    FAN_OFF: THERMOSTAT_FAN_MODE_OFF,
}

DECONZ_TO_FAN_MODE = {value: key for key, value in FAN_MODE_TO_DECONZ.items()}

HVAC_MODE_TO_DECONZ = {
    HVAC_MODE_AUTO: THERMOSTAT_MODE_AUTO,
    HVAC_MODE_COOL: THERMOSTAT_MODE_COOL,
    HVAC_MODE_HEAT: THERMOSTAT_MODE_HEAT,
    HVAC_MODE_OFF: THERMOSTAT_MODE_OFF,
}

DECONZ_PRESET_AUTO = "auto"
DECONZ_PRESET_COMPLEX = "complex"
DECONZ_PRESET_HOLIDAY = "holiday"
DECONZ_PRESET_MANUAL = "manual"

PRESET_MODE_TO_DECONZ = {
    DECONZ_PRESET_AUTO: THERMOSTAT_PRESET_AUTO,
    PRESET_BOOST: THERMOSTAT_PRESET_BOOST,
    PRESET_COMFORT: THERMOSTAT_PRESET_COMFORT,
    DECONZ_PRESET_COMPLEX: THERMOSTAT_PRESET_COMPLEX,
    PRESET_ECO: THERMOSTAT_PRESET_ECO,
    DECONZ_PRESET_HOLIDAY: THERMOSTAT_PRESET_HOLIDAY,
    DECONZ_PRESET_MANUAL: THERMOSTAT_PRESET_MANUAL,
}

DECONZ_TO_PRESET_MODE = {value: key for key, value in PRESET_MODE_TO_DECONZ.items()}


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    async_add_entities: AddEntitiesCallback,
) -> None:
    """Set up the deCONZ climate devices.

    Thermostats are based on the same device class as sensors in deCONZ.
    """
    gateway = get_gateway_from_config_entry(hass, config_entry)
    gateway.entities[DOMAIN] = set()

    @callback
    def async_add_climate(
        sensors: list[Thermostat]
        | ValuesView[Thermostat] = gateway.api.sensors.values(),
    ) -> None:
        """Add climate devices from deCONZ."""
        entities: list[DeconzThermostat] = []