Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for using switch with ecobee thermostats."""
2 
3 from __future__ import annotations
4 
5 from datetime import tzinfo
6 import logging
7 from typing import Any
8 
9 from homeassistant.components.climate import HVACMode
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.util import dt as dt_util
15 
16 from . import EcobeeData
17 from .climate import HASS_TO_ECOBEE_HVAC
18 from .const import DOMAIN, ECOBEE_AUX_HEAT_ONLY
19 from .entity import EcobeeBaseEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the ecobee thermostat switch entity."""
32  data: EcobeeData = hass.data[DOMAIN]
33 
34  entities: list[SwitchEntity] = [
36  data,
37  index,
38  (await dt_util.async_get_time_zone(thermostat["location"]["timeZone"]))
39  or dt_util.get_default_time_zone(),
40  )
41  for index, thermostat in enumerate(data.ecobee.thermostats)
42  if thermostat["settings"]["ventilatorType"] != "none"
43  ]
44 
45  entities.extend(
46  (
47  EcobeeSwitchAuxHeatOnly(data, index)
48  for index, thermostat in enumerate(data.ecobee.thermostats)
49  if thermostat["settings"]["hasHeatPump"]
50  )
51  )
52 
53  async_add_entities(entities, update_before_add=True)
54 
55 
57  """A Switch class, representing 20 min timer for an ecobee thermostat with ventilator attached."""
58 
59  _attr_has_entity_name = True
60  _attr_name = "Ventilator 20m Timer"
61 
62  def __init__(
63  self,
64  data: EcobeeData,
65  thermostat_index: int,
66  operating_timezone: tzinfo,
67  ) -> None:
68  """Initialize ecobee ventilator platform."""
69  super().__init__(data, thermostat_index)
70  self._attr_unique_id_attr_unique_id = f"{self.base_unique_id}_ventilator_20m_timer"
71  self._attr_is_on_attr_is_on = False
72  self.update_without_throttleupdate_without_throttle = False
73  self._operating_timezone_operating_timezone = operating_timezone
74 
75  async def async_update(self) -> None:
76  """Get the latest state from the thermostat."""
77 
78  if self.update_without_throttleupdate_without_throttle:
79  await self.datadata.update(no_throttle=True)
80  self.update_without_throttleupdate_without_throttle = False
81  else:
82  await self.datadata.update()
83 
84  ventilator_off_date_time = self.thermostatthermostat["settings"]["ventilatorOffDateTime"]
85 
86  self._attr_is_on_attr_is_on = ventilator_off_date_time and dt_util.parse_datetime(
87  ventilator_off_date_time, raise_on_error=True
88  ).replace(tzinfo=self._operating_timezone_operating_timezone) >= dt_util.now(
89  self._operating_timezone_operating_timezone
90  )
91 
92  async def async_turn_on(self, **kwargs: Any) -> None:
93  """Set ventilator 20 min timer on."""
94  await self.hasshass.async_add_executor_job(
95  self.datadata.ecobee.set_ventilator_timer, self.thermostat_indexthermostat_index, True
96  )
97  self.update_without_throttleupdate_without_throttle = True
98 
99  async def async_turn_off(self, **kwargs: Any) -> None:
100  """Set ventilator 20 min timer off."""
101  await self.hasshass.async_add_executor_job(
102  self.datadata.ecobee.set_ventilator_timer, self.thermostat_indexthermostat_index, False
103  )
104  self.update_without_throttleupdate_without_throttle = True
105 
106 
108  """Representation of a aux_heat_only ecobee switch."""
109 
110  _attr_has_entity_name = True
111  _attr_translation_key = "aux_heat_only"
112 
113  def __init__(
114  self,
115  data: EcobeeData,
116  thermostat_index: int,
117  ) -> None:
118  """Initialize ecobee ventilator platform."""
119  super().__init__(data, thermostat_index)
120  self._attr_unique_id_attr_unique_id = f"{self.base_unique_id}_aux_heat_only"
121 
122  self._last_hvac_mode_before_aux_heat_last_hvac_mode_before_aux_heat = HASS_TO_ECOBEE_HVAC.get(
123  HVACMode.HEAT_COOL
124  )
125 
126  def turn_on(self, **kwargs: Any) -> None:
127  """Set the hvacMode to auxHeatOnly."""
128  self._last_hvac_mode_before_aux_heat_last_hvac_mode_before_aux_heat = self.thermostatthermostat["settings"]["hvacMode"]
129  self.datadata.ecobee.set_hvac_mode(self.thermostat_indexthermostat_index, ECOBEE_AUX_HEAT_ONLY)
130 
131  def turn_off(self, **kwargs: Any) -> None:
132  """Set the hvacMode back to the prior setting."""
133  self.datadata.ecobee.set_hvac_mode(
134  self.thermostat_indexthermostat_index, self._last_hvac_mode_before_aux_heat_last_hvac_mode_before_aux_heat
135  )
136 
137  @property
138  def is_on(self) -> bool:
139  """Return true if auxHeatOnly mode is active."""
140  return self.thermostatthermostat["settings"]["hvacMode"] == ECOBEE_AUX_HEAT_ONLY
None __init__(self, EcobeeData data, int thermostat_index)
Definition: switch.py:117
None __init__(self, EcobeeData data, int thermostat_index, tzinfo operating_timezone)
Definition: switch.py:67
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:30
IssData update(pyiss.ISS iss)
Definition: __init__.py:33