Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Climate platform that offers a climate device for the TFIAC protocol."""
2 
3 from __future__ import annotations
4 
5 from concurrent import futures
6 from datetime import timedelta
7 import logging
8 from typing import Any
9 
10 from pytfiac import Tfiac
11 import voluptuous as vol
12 
14  FAN_AUTO,
15  FAN_HIGH,
16  FAN_LOW,
17  FAN_MEDIUM,
18  PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA,
19  SWING_BOTH,
20  SWING_HORIZONTAL,
21  SWING_OFF,
22  SWING_VERTICAL,
23  ClimateEntity,
24  ClimateEntityFeature,
25  HVACMode,
26 )
27 from homeassistant.const import ATTR_TEMPERATURE, CONF_HOST, UnitOfTemperature
28 from homeassistant.core import HomeAssistant
30 from homeassistant.helpers.entity_platform import AddEntitiesCallback
31 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
32 
33 SCAN_INTERVAL = timedelta(seconds=60)
34 
35 PLATFORM_SCHEMA = CLIMATE_PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
36 
37 _LOGGER = logging.getLogger(__name__)
38 
39 MIN_TEMP = 61
40 MAX_TEMP = 88
41 
42 HVAC_MAP = {
43  HVACMode.HEAT: "heat",
44  HVACMode.AUTO: "selfFeel",
45  HVACMode.DRY: "dehumi",
46  HVACMode.FAN_ONLY: "fan",
47  HVACMode.COOL: "cool",
48  HVACMode.OFF: "off",
49 }
50 
51 HVAC_MAP_REV = {v: k for k, v in HVAC_MAP.items()}
52 
53 SUPPORT_FAN = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW]
54 SUPPORT_SWING = [SWING_OFF, SWING_HORIZONTAL, SWING_VERTICAL, SWING_BOTH]
55 
56 CURR_TEMP = "current_temp"
57 TARGET_TEMP = "target_temp"
58 OPERATION_MODE = "operation"
59 FAN_MODE = "fan_mode"
60 SWING_MODE = "swing_mode"
61 ON_MODE = "is_on"
62 
63 
65  hass: HomeAssistant,
66  config: ConfigType,
67  async_add_entities: AddEntitiesCallback,
68  discovery_info: DiscoveryInfoType | None = None,
69 ) -> None:
70  """Set up the TFIAC climate device."""
71  tfiac_client = Tfiac(config[CONF_HOST])
72  try:
73  await tfiac_client.update()
74  except futures.TimeoutError:
75  _LOGGER.error("Unable to connect to %s", config[CONF_HOST])
76  return
77  async_add_entities([TfiacClimate(hass, tfiac_client)])
78 
79 
81  """TFIAC class."""
82 
83  _attr_supported_features = (
84  ClimateEntityFeature.FAN_MODE
85  | ClimateEntityFeature.SWING_MODE
86  | ClimateEntityFeature.TARGET_TEMPERATURE
87  | ClimateEntityFeature.TURN_OFF
88  | ClimateEntityFeature.TURN_ON
89  )
90  _attr_temperature_unit = UnitOfTemperature.FAHRENHEIT
91  _enable_turn_on_off_backwards_compatibility = False
92 
93  def __init__(self, hass, client):
94  """Init class."""
95  self._client_client = client
96  self._available_available = True
97 
98  @property
99  def available(self):
100  """Return if the device is available."""
101  return self._available_available
102 
103  async def async_update(self) -> None:
104  """Update status via socket polling."""
105  try:
106  await self._client_client.update()
107  self._available_available = True
108  except futures.TimeoutError:
109  self._available_available = False
110 
111  @property
112  def min_temp(self):
113  """Return the minimum temperature."""
114  return MIN_TEMP
115 
116  @property
117  def max_temp(self):
118  """Return the maximum temperature."""
119  return MAX_TEMP
120 
121  @property
122  def name(self):
123  """Return the name of the climate device."""
124  return self._client_client.name
125 
126  @property
128  """Return the temperature we try to reach."""
129  return self._client_client.status["target_temp"]
130 
131  @property
133  """Return the current temperature."""
134  return self._client_client.status["current_temp"]
135 
136  @property
137  def hvac_mode(self) -> HVACMode | None:
138  """Return hvac operation ie. heat, cool mode.
139 
140  Need to be one of HVAC_MODE_*.
141  """
142  if self._client_client.status[ON_MODE] != "on":
143  return HVACMode.OFF
144 
145  state = self._client_client.status["operation"]
146  return HVAC_MAP_REV.get(state)
147 
148  @property
149  def hvac_modes(self) -> list[HVACMode]:
150  """Return the list of available hvac operation modes.
151 
152  Need to be a subset of HVAC_MODES.
153  """
154  return list(HVAC_MAP)
155 
156  @property
157  def fan_mode(self):
158  """Return the fan setting."""
159  return self._client_client.status["fan_mode"].lower()
160 
161  @property
162  def fan_modes(self):
163  """Return the list of available fan modes."""
164  return SUPPORT_FAN
165 
166  @property
167  def swing_mode(self):
168  """Return the swing setting."""
169  return self._client_client.status["swing_mode"].lower()
170 
171  @property
172  def swing_modes(self):
173  """List of available swing modes."""
174  return SUPPORT_SWING
175 
176  async def async_set_temperature(self, **kwargs: Any) -> None:
177  """Set new target temperature."""
178  if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None:
179  await self._client_client.set_state(TARGET_TEMP, temp)
180 
181  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
182  """Set new target hvac mode."""
183  if hvac_mode == HVACMode.OFF:
184  await self._client_client.set_state(ON_MODE, "off")
185  else:
186  await self._client_client.set_state(OPERATION_MODE, HVAC_MAP[hvac_mode])
187 
188  async def async_set_fan_mode(self, fan_mode: str) -> None:
189  """Set new fan mode."""
190  await self._client_client.set_state(FAN_MODE, fan_mode.capitalize())
191 
192  async def async_set_swing_mode(self, swing_mode: str) -> None:
193  """Set new swing mode."""
194  await self._client_client.set_swing(swing_mode.capitalize())
195 
196  async def async_turn_on(self) -> None:
197  """Turn device on."""
198  await self._client_client.set_state(OPERATION_MODE)
199 
200  async def async_turn_off(self) -> None:
201  """Turn device off."""
202  await self._client_client.set_state(ON_MODE, "off")
None async_set_swing_mode(self, str swing_mode)
Definition: climate.py:192
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:181
None async_set_temperature(self, **Any kwargs)
Definition: climate.py:176
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: climate.py:69