<commit_before>"""Support for Climate devices of (EMEA/EU-based) Honeywell TCC systems."""
from datetime import datetime
import logging
from typing import Any, Dict, Optional, List

import requests.exceptions
import evohomeclient2

from homeassistant.components.climate import ClimateDevice
from homeassistant.components.climate.const import (
    CURRENT_HVAC_HEAT, CURRENT_HVAC_IDLE, CURRENT_HVAC_OFF,
    HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_OFF,
    PRESET_AWAY, PRESET_ECO, PRESET_HOME, PRESET_NONE,
    SUPPORT_TARGET_TEMPERATURE, SUPPORT_PRESET_MODE)
from homeassistant.const import PRECISION_TENTHS
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util.dt import parse_datetime

from . import CONF_LOCATION_IDX, _handle_exception, EvoDevice
from .const import (
    DOMAIN, EVO_RESET, EVO_AUTO, EVO_AUTOECO, EVO_AWAY, EVO_CUSTOM, EVO_DAYOFF,
    EVO_HEATOFF, EVO_FOLLOW, EVO_TEMPOVER, EVO_PERMOVER)

_LOGGER = logging.getLogger(__name__)

PRESET_RESET = 'Reset'  # reset all child zones to EVO_FOLLOW
PRESET_CUSTOM = 'Custom'

HA_HVAC_TO_TCS = {
    HVAC_MODE_OFF: EVO_HEATOFF,
    HVAC_MODE_HEAT: EVO_AUTO,
}

TCS_PRESET_TO_HA = {
    EVO_AWAY: PRESET_AWAY,
    EVO_CUSTOM: PRESET_CUSTOM,
    EVO_AUTOECO: PRESET_ECO,
    EVO_DAYOFF: PRESET_HOME,
    EVO_RESET: PRESET_RESET,
}  # EVO_AUTO: None,

HA_PRESET_TO_TCS = {v: k for k, v in TCS_PRESET_TO_HA.items()}

EVO_PRESET_TO_HA = {
    EVO_FOLLOW: PRESET_NONE,
    EVO_TEMPOVER: 'temporary',
    EVO_PERMOVER: 'permanent',
}
HA_PRESET_TO_EVO = {v: k for k, v in EVO_PRESET_TO_HA.items()}


def setup_platform(hass: HomeAssistantType, hass_config: ConfigType,
                   add_entities, discovery_info=None) -> None:
    """Create the evohome Controller, and its Zones, if any."""
    broker = hass.data[DOMAIN]['broker']
    loc_idx = broker.params[CONF_LOCATION_IDX]

    _LOGGER.debug(
        "Found Location/Controller, id=%s [%s], name=%s (location_idx=%s)",
        broker.tcs.systemId, broker.tcs.modelType, broker.tcs.location.name,
        loc_idx)

    # special case of RoundThermostat (is single zone)
    if broker.config['zones'][0]['modelType'] == 'RoundModulation':
        zone = list(broker.tcs.zones.values())[0]
        _LOGGER.debug(
            "Found %s, id=%s [%s], name=%s",
            zone.zoneType, zone.zoneId, zone.modelType, zone.name)

        add_entities([EvoThermostat(broker, zone)], update_before_add=True)
        return

    controller = EvoController(broker, broker.tcs)

    zones = []
    for zone in broker.tcs.zones.values():
        _LOGGER.debug(
            "Found %s, id=%s [%s], name=%s",
            zone.zoneType, zone.zoneId, zone.modelType, zone.name)
        zones.append(EvoZone(broker, zone))

    add_entities([controller] + zones, update_before_add=True)


class EvoClimateDevice(EvoDevice, ClimateDevice):
    """Base for a Honeywell evohome Climate device."""

    def __init__(self, evo_broker, evo_device) -> None:
        """Initialize the evohome Climate device."""
        super().__init__(evo_broker, evo_device)

        self._preset_modes = None

    def _set_temperature(self, temperature: float,
                         until: Optional[datetime] = None) -> None:
        """Set a new target temperature for the Zone.

        until == None means indefinitely (i.e. PermanentOverride)
        """
        try:
            self._evo_device.set_temperature(temperature, until)
        except (requests.exceptions.RequestException,
                evohomeclient2.AuthenticationError) as err:
            _handle_exception(err)

    def _set_zone_mode(self, op_mode: str) -> None:
        """Set a Zone to one of its native EVO_* operating modes.

        Zones inherit their _effective_ operating mode from the Controller.

        Usually, Zones are in 'FollowSchedule' mode, where their setpoints are
        a function of their own schedule and the Controller's operating mode,
        e.g. 'AutoWithEco' mode means their setpoint is (by default) 3C less
        than scheduled.

        However, Zones can _override_ these setpoints, either indefinitely,
        'PermanentOverride' mode, or for a period of time, 'TemporaryOverride',
        after which they will revert back to 'FollowSchedule'.

        Finally, some of the Controller's operating modes are _forced_ upon the
        Zones, regardless of any override mode, e.g. 'HeatingOff', Zones to
        (by default) 5C, and 'Away', Zones to (by default) 12C.
        """
        if op_mode == EVO_FOLLOW:
            try:
                self._evo_device.cancel_temp_override()
            except (requests.exceptions.RequestException,
                    evohomeclient2.AuthenticationError) as err:
                _handle_exception(err)
            return

        temperature = self._evo_device.setpointStatus['targetHeatTemperature']
        until = None  # EVO_PERMOVER

        if op_mode == EVO_TEMPOVER and self._schedule['DailySchedules']:
            self._update_schedule()
            if self._schedule['DailySchedules']:
                until = parse_datetime(self.setpoints['next']['from'])

        self._set_temperature(temperature, until=until)

    def _set_tcs_mode(self, op_mode: str) -> None:
        """Set the Controller to any of its native EVO_* operating modes."""
        try:
            self._evo_tcs._set_status(op_mode)  # noqa: E501; pylint: disable=protected-access
        except (requests.exceptions.RequestException,
                evohomeclient2.AuthenticationError) as err:
            _handle_exception(err)

    @property
    def hvac_modes(self) -> List[str]:
        """Return the list of available hvac operation modes."""
        return list(HA_HVAC_TO_TCS)

    @property
    def preset_modes(self) -> Optional[List[str]]:
        """Return a list of available preset modes."""
        return self._preset_modes


class EvoZone(EvoClimateDevice):
    """Base for a Honeywell evohome Zone."""

    def __init__(self, evo_broker, evo_device) -> None:
        """Initialize the evohome Zone."""
        super().__init__(evo_broker, evo_device)

        self._name = evo_device.name
        self._icon = 'mdi:radiator'

        self._precision = \
            self._evo_device.setpointCapabilities['valueResolution']
        self._state_attributes = [
            'zoneId', 'activeFaults', 'setpointStatus', 'temperatureStatus',
            'setpoints']

        self._supported_features = SUPPORT_PRESET_MODE | \
            SUPPORT_TARGET_TEMPERATURE
        self._preset_modes = list(HA_PRESET_TO_EVO)

    @property
    def hvac_mode(self) -> str:
        """Return the current operating mode of the evohome Zone."""
        if self._evo_tcs.systemModeStatus['mode'] in [EVO_AWAY, EVO_HEATOFF]:
            return HVAC_MODE_AUTO
        is_off = self.target_temperature <= self.min_temp
        return HVAC_MODE_OFF if is_off else HVAC_MODE_HEAT

    @property
    def hvac_action(self) -> Optional[str]:
        """Return the current running hvac operation if supported."""
        if self._evo_tcs.systemModeStatus['mode'] == EVO_HEATOFF:
            return CURRENT_HVAC_OFF
        if self.target_temperature <= self.min_temp:
            return CURRENT_HVAC_OFF
        if self.target_temperature <= self.current_temperature:
            return CURRENT_HVAC_HEAT
        return CURRENT_HVAC_IDLE

    @property
    def current_temperature(self) -> Optional[float]:
        """Return the current temperature of the evohome Zone."""
        return (self._evo_device.temperatureStatus['temperature']
                if self._evo_device.temperatureStatus['isAvailable'] else None)

    @property
    def target_temperature(self) -> float:
        """Return the target temperature of the evohome Zone."""
        if self._evo_tcs.systemModeStatus['mode'] == EVO_HEATOFF:
            return self._evo_device.setpointCapabilities['minHeatSetpoint']
        return self._evo_device.setpointStatus['targetHeatTemperature']

    @property
    def preset_mode(self) -> Optional[str]:
        """Return the current preset mode, e.g., home, away, temp."""
        if self._evo_tcs.systemModeStatus['mode'] in [EVO_AWAY, EVO_HEATOFF]:
            return TCS_PRESET_TO_HA.get(self._evo_tcs.systemModeStatus['mode'])
        return EVO_PRESET_TO_HA.get(
            self._evo_device.setpointStatus['setpointMode'], 'follow')

    @property
    def min_temp(self) -> float:
        """Return the minimum target temperature of a evohome Zone.

        The default is 5, but is user-configurable within 5-35 (in Celsius).
        """<commit_msg>[climate] Correct evohome hvac_action (#25407)<commit_after>"""Support for Climate devices of (EMEA/EU-based) Honeywell TCC systems."""
from datetime import datetime
import logging
from typing import Any, Dict, Optional, List

import requests.exceptions
import evohomeclient2

from homeassistant.components.climate import ClimateDevice
from homeassistant.components.climate.const import (
    CURRENT_HVAC_HEAT, CURRENT_HVAC_IDLE, CURRENT_HVAC_OFF,
    HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_OFF,
    PRESET_AWAY, PRESET_ECO, PRESET_HOME, PRESET_NONE,
    SUPPORT_TARGET_TEMPERATURE, SUPPORT_PRESET_MODE)
from homeassistant.const import PRECISION_TENTHS
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util.dt import parse_datetime

from . import CONF_LOCATION_IDX, _handle_exception, EvoDevice
from .const import (
    DOMAIN, EVO_RESET, EVO_AUTO, EVO_AUTOECO, EVO_AWAY, EVO_CUSTOM, EVO_DAYOFF,
    EVO_HEATOFF, EVO_FOLLOW, EVO_TEMPOVER, EVO_PERMOVER)

_LOGGER = logging.getLogger(__name__)

PRESET_RESET = 'Reset'  # reset all child zones to EVO_FOLLOW
PRESET_CUSTOM = 'Custom'

HA_HVAC_TO_TCS = {
    HVAC_MODE_OFF: EVO_HEATOFF,
    HVAC_MODE_HEAT: EVO_AUTO,
}

TCS_PRESET_TO_HA = {
    EVO_AWAY: PRESET_AWAY,
    EVO_CUSTOM: PRESET_CUSTOM,
    EVO_AUTOECO: PRESET_ECO,
    EVO_DAYOFF: PRESET_HOME,
    EVO_RESET: PRESET_RESET,
}  # EVO_AUTO: None,

HA_PRESET_TO_TCS = {v: k for k, v in TCS_PRESET_TO_HA.items()}

EVO_PRESET_TO_HA = {
    EVO_FOLLOW: PRESET_NONE,
    EVO_TEMPOVER: 'temporary',
    EVO_PERMOVER: 'permanent',
}
HA_PRESET_TO_EVO = {v: k for k, v in EVO_PRESET_TO_HA.items()}


def setup_platform(hass: HomeAssistantType, hass_config: ConfigType,
                   add_entities, discovery_info=None) -> None:
    """Create the evohome Controller, and its Zones, if any."""
    broker = hass.data[DOMAIN]['broker']
    loc_idx = broker.params[CONF_LOCATION_IDX]

    _LOGGER.debug(
        "Found Location/Controller, id=%s [%s], name=%s (location_idx=%s)",
        broker.tcs.systemId, broker.tcs.modelType, broker.tcs.location.name,
        loc_idx)

    # special case of RoundThermostat (is single zone)
    if broker.config['zones'][0]['modelType'] == 'RoundModulation':
        zone = list(broker.tcs.zones.values())[0]
        _LOGGER.debug(
            "Found %s, id=%s [%s], name=%s",
            zone.zoneType, zone.zoneId, zone.modelType, zone.name)

        add_entities([EvoThermostat(broker, zone)], update_before_add=True)
        return

    controller = EvoController(broker, broker.tcs)

    zones = []
    for zone in broker.tcs.zones.values():
        _LOGGER.debug(
            "Found %s, id=%s [%s], name=%s",
            zone.zoneType, zone.zoneId, zone.modelType, zone.name)
        zones.append(EvoZone(broker, zone))

    add_entities([controller] + zones, update_before_add=True)


class EvoClimateDevice(EvoDevice, ClimateDevice):
    """Base for a Honeywell evohome Climate device."""

    def __init__(self, evo_broker, evo_device) -> None:
        """Initialize the evohome Climate device."""
        super().__init__(evo_broker, evo_device)

        self._preset_modes = None

    def _set_temperature(self, temperature: float,
                         until: Optional[datetime] = None) -> None:
        """Set a new target temperature for the Zone.

        until == None means indefinitely (i.e. PermanentOverride)
        """
        try:
            self._evo_device.set_temperature(temperature, until)
        except (requests.exceptions.RequestException,
                evohomeclient2.AuthenticationError) as err:
            _handle_exception(err)

    def _set_zone_mode(self, op_mode: str) -> None:
        """Set a Zone to one of its native EVO_* operating modes.

        Zones inherit their _effective_ operating mode from the Controller.

        Usually, Zones are in 'FollowSchedule' mode, where their setpoints are
        a function of their own schedule and the Controller's operating mode,
        e.g. 'AutoWithEco' mode means their setpoint is (by default) 3C less
        than scheduled.

        However, Zones can _override_ these setpoints, either indefinitely,
        'PermanentOverride' mode, or for a period of time, 'TemporaryOverride',
        after which they will revert back to 'FollowSchedule'.

        Finally, some of the Controller's operating modes are _forced_ upon the
        Zones, regardless of any override mode, e.g. 'HeatingOff', Zones to
        (by default) 5C, and 'Away', Zones to (by default) 12C.
        """
        if op_mode == EVO_FOLLOW:
            try:
                self._evo_device.cancel_temp_override()
            except (requests.exceptions.RequestException,
                    evohomeclient2.AuthenticationError) as err:
                _handle_exception(err)
            return

        temperature = self._evo_device.setpointStatus['targetHeatTemperature']
        until = None  # EVO_PERMOVER

        if op_mode == EVO_TEMPOVER and self._schedule['DailySchedules']:
            self._update_schedule()
            if self._schedule['DailySchedules']:
                until = parse_datetime(self.setpoints['next']['from'])

        self._set_temperature(temperature, until=until)

    def _set_tcs_mode(self, op_mode: str) -> None:
        """Set the Controller to any of its native EVO_* operating modes."""
        try:
            self._evo_tcs._set_status(op_mode)  # noqa: E501; pylint: disable=protected-access
        except (requests.exceptions.RequestException,
                evohomeclient2.AuthenticationError) as err:
            _handle_exception(err)

    @property
    def hvac_modes(self) -> List[str]:
        """Return the list of available hvac operation modes."""
        return list(HA_HVAC_TO_TCS)

    @property
    def preset_modes(self) -> Optional[List[str]]:
        """Return a list of available preset modes."""
        return self._preset_modes


class EvoZone(EvoClimateDevice):
    """Base for a Honeywell evohome Zone."""

    def __init__(self, evo_broker, evo_device) -> None:
        """Initialize the evohome Zone."""
        super().__init__(evo_broker, evo_device)

        self._name = evo_device.name
        self._icon = 'mdi:radiator'

        self._precision = \
            self._evo_device.setpointCapabilities['valueResolution']
        self._state_attributes = [
            'zoneId', 'activeFaults', 'setpointStatus', 'temperatureStatus',
            'setpoints']

        self._supported_features = SUPPORT_PRESET_MODE | \
            SUPPORT_TARGET_TEMPERATURE
        self._preset_modes = list(HA_PRESET_TO_EVO)

    @property
    def hvac_mode(self) -> str:
        """Return the current operating mode of the evohome Zone."""
        if self._evo_tcs.systemModeStatus['mode'] in [EVO_AWAY, EVO_HEATOFF]:
            return HVAC_MODE_AUTO
        is_off = self.target_temperature <= self.min_temp
        return HVAC_MODE_OFF if is_off else HVAC_MODE_HEAT

    @property
    def hvac_action(self) -> Optional[str]:
        """Return the current running hvac operation if supported."""
        if self._evo_tcs.systemModeStatus['mode'] == EVO_HEATOFF:
            return CURRENT_HVAC_OFF
        if self.target_temperature <= self.min_temp:
            return CURRENT_HVAC_OFF
        if self.target_temperature < self.current_temperature:
            return CURRENT_HVAC_IDLE
        return CURRENT_HVAC_HEAT

    @property
    def current_temperature(self) -> Optional[float]:
        """Return the current temperature of the evohome Zone."""
        return (self._evo_device.temperatureStatus['temperature']
                if self._evo_device.temperatureStatus['isAvailable'] else None)

    @property
    def target_temperature(self) -> float:
        """Return the target temperature of the evohome Zone."""
        if self._evo_tcs.systemModeStatus['mode'] == EVO_HEATOFF:
            return self._evo_device.setpointCapabilities['minHeatSetpoint']
        return self._evo_device.setpointStatus['targetHeatTemperature']

    @property
    def preset_mode(self) -> Optional[str]:
        """Return the current preset mode, e.g., home, away, temp."""
        if self._evo_tcs.systemModeStatus['mode'] in [EVO_AWAY, EVO_HEATOFF]:
            return TCS_PRESET_TO_HA.get(self._evo_tcs.systemModeStatus['mode'])
        return EVO_PRESET_TO_HA.get(
            self._evo_device.setpointStatus['setpointMode'], 'follow')

    @property
    def min_temp(self) -> float:
        """Return the minimum target temperature of a evohome Zone.

        The default is 5, but is user-configurable within 5-35 (in Celsius).
        """
{'fraction_non_alphanumeric': 0.09588882455124494, 'fraction_numerical': 0.00225825130283729, 'mean_word_length': 3.05137227304715, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5001312', 'n_tokens_mistral': 5626, 'n_tokens_neox': 5339, 'n_words': 1292}