Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """The lookin integration climate platform."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any, Final, cast
7 
8 from aiolookin import Climate, MeteoSensor, Remote
9 from aiolookin.models import UDPCommandType, UDPEvent
10 
12  ATTR_HVAC_MODE,
13  FAN_AUTO,
14  FAN_HIGH,
15  FAN_LOW,
16  FAN_MIDDLE,
17  SWING_BOTH,
18  SWING_OFF,
19  ClimateEntity,
20  ClimateEntityFeature,
21  HVACMode,
22 )
23 from homeassistant.config_entries import ConfigEntry
24 from homeassistant.const import (
25  ATTR_TEMPERATURE,
26  PRECISION_WHOLE,
27  Platform,
28  UnitOfTemperature,
29 )
30 from homeassistant.core import HomeAssistant, callback
31 from homeassistant.helpers.entity_platform import AddEntitiesCallback
32 
33 from .const import DOMAIN, TYPE_TO_PLATFORM
34 from .coordinator import LookinDataUpdateCoordinator
35 from .entity import LookinCoordinatorEntity
36 from .models import LookinData
37 
38 LOOKIN_FAN_MODE_IDX_TO_HASS: Final = [FAN_AUTO, FAN_LOW, FAN_MIDDLE, FAN_HIGH]
39 LOOKIN_SWING_MODE_IDX_TO_HASS: Final = [SWING_OFF, SWING_BOTH]
40 LOOKIN_HVAC_MODE_IDX_TO_HASS: Final = [
41  HVACMode.OFF,
42  HVACMode.AUTO,
43  HVACMode.COOL,
44  HVACMode.HEAT,
45  HVACMode.DRY,
46  HVACMode.FAN_ONLY,
47 ]
48 
49 HASS_TO_LOOKIN_HVAC_MODE: dict[str, int] = {
50  mode: idx for idx, mode in enumerate(LOOKIN_HVAC_MODE_IDX_TO_HASS)
51 }
52 HASS_TO_LOOKIN_FAN_MODE: dict[str, int] = {
53  mode: idx for idx, mode in enumerate(LOOKIN_FAN_MODE_IDX_TO_HASS)
54 }
55 HASS_TO_LOOKIN_SWING_MODE: dict[str, int] = {
56  mode: idx for idx, mode in enumerate(LOOKIN_SWING_MODE_IDX_TO_HASS)
57 }
58 
59 
60 MIN_TEMP: Final = 16
61 MAX_TEMP: Final = 30
62 LOGGER = logging.getLogger(__name__)
63 
64 
66  hass: HomeAssistant,
67  config_entry: ConfigEntry,
68  async_add_entities: AddEntitiesCallback,
69 ) -> None:
70  """Set up the climate platform for lookin from a config entry."""
71  lookin_data: LookinData = hass.data[DOMAIN][config_entry.entry_id]
72  entities = []
73 
74  for remote in lookin_data.devices:
75  if TYPE_TO_PLATFORM.get(remote["Type"]) != Platform.CLIMATE:
76  continue
77  uuid = remote["UUID"]
78  coordinator = lookin_data.device_coordinators[uuid]
79  device = cast(Climate, coordinator.data)
80  entities.append(
82  uuid=uuid,
83  device=device,
84  lookin_data=lookin_data,
85  coordinator=coordinator,
86  )
87  )
88 
89  async_add_entities(entities)
90 
91 
93  """An aircon or heat pump."""
94 
95  _attr_current_humidity: float | None = None # type: ignore[assignment]
96  _attr_temperature_unit = UnitOfTemperature.CELSIUS
97  _attr_supported_features = (
98  ClimateEntityFeature.TARGET_TEMPERATURE
99  | ClimateEntityFeature.FAN_MODE
100  | ClimateEntityFeature.SWING_MODE
101  | ClimateEntityFeature.TURN_OFF
102  | ClimateEntityFeature.TURN_ON
103  )
104  _attr_fan_modes: list[str] = LOOKIN_FAN_MODE_IDX_TO_HASS
105  _attr_swing_modes: list[str] = LOOKIN_SWING_MODE_IDX_TO_HASS
106  _attr_hvac_modes: list[HVACMode] = LOOKIN_HVAC_MODE_IDX_TO_HASS
107  _attr_min_temp = MIN_TEMP
108  _attr_max_temp = MAX_TEMP
109  _attr_target_temperature_step = PRECISION_WHOLE
110  _enable_turn_on_off_backwards_compatibility = False
111 
112  def __init__(
113  self,
114  uuid: str,
115  device: Climate,
116  lookin_data: LookinData,
117  coordinator: LookinDataUpdateCoordinator[Remote],
118  ) -> None:
119  """Init the ConditionerEntity."""
120  super().__init__(coordinator, uuid, device, lookin_data)
121  self._async_update_from_data_async_update_from_data()
122 
123  @property
124  def _climate(self) -> Climate:
125  return cast(Climate, self.coordinator.data)
126 
127  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
128  """Set the hvac mode of the device."""
129  if (mode := HASS_TO_LOOKIN_HVAC_MODE.get(hvac_mode)) is None:
130  return
131  self._climate_climate.hvac_mode = mode
132  await self._async_update_conditioner_async_update_conditioner()
133 
134  async def async_set_temperature(self, **kwargs: Any) -> None:
135  """Set the temperature of the device."""
136  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
137  return
138  self._climate_climate.temp_celsius = int(temperature)
139  lookin_index = LOOKIN_HVAC_MODE_IDX_TO_HASS
140  if hvac_mode := kwargs.get(ATTR_HVAC_MODE):
141  self._climate_climate.hvac_mode = HASS_TO_LOOKIN_HVAC_MODE[hvac_mode]
142  elif self._climate_climate.hvac_mode == lookin_index.index(HVACMode.OFF):
143  #
144  # If the device is off, and the user didn't specify an HVAC mode
145  # (which is the default when using the HA UI), the device won't turn
146  # on without having an HVAC mode passed.
147  #
148  # We picked the hvac mode based on the current temp if its available
149  # since only some units support auto, but most support either heat
150  # or cool otherwise we set auto since we don't have a way to make
151  # an educated guess.
152  #
153 
154  if self._meteo_coordinator_meteo_coordinator:
155  meteo_data: MeteoSensor = self._meteo_coordinator_meteo_coordinator.data
156  if not (current_temp := meteo_data.temperature):
157  self._climate_climate.hvac_mode = lookin_index.index(HVACMode.AUTO)
158  elif current_temp >= self._climate_climate.temp_celsius:
159  self._climate_climate.hvac_mode = lookin_index.index(HVACMode.COOL)
160  else:
161  self._climate_climate.hvac_mode = lookin_index.index(HVACMode.HEAT)
162  else:
163  self._climate_climate.hvac_mode = lookin_index.index(HVACMode.AUTO)
164 
165  await self._async_update_conditioner_async_update_conditioner()
166 
167  async def async_set_fan_mode(self, fan_mode: str) -> None:
168  """Set the fan mode of the device."""
169  if (mode := HASS_TO_LOOKIN_FAN_MODE.get(fan_mode)) is None:
170  return
171  self._climate_climate.fan_mode = mode
172  await self._async_update_conditioner_async_update_conditioner()
173 
174  async def async_set_swing_mode(self, swing_mode: str) -> None:
175  """Set the swing mode of the device."""
176  if (mode := HASS_TO_LOOKIN_SWING_MODE.get(swing_mode)) is None:
177  return
178  self._climate_climate.swing_mode = mode
179  await self._async_update_conditioner_async_update_conditioner()
180 
181  async def _async_update_conditioner(self) -> None:
182  """Update the conditioner state from the climate data."""
183  self.coordinator.async_set_updated_data(self._climate_climate)
184  await self._lookin_protocol_lookin_protocol.update_conditioner(
185  uuid=self._attr_unique_id_attr_unique_id, status=self._climate_climate.to_status
186  )
187 
188  def _async_update_from_data(self) -> None:
189  """Update attrs from data."""
190  if self._meteo_coordinator_meteo_coordinator:
191  temperature = self._meteo_coordinator_meteo_coordinator.data.temperature
192  humidity = int(self._meteo_coordinator_meteo_coordinator.data.humidity)
193  else:
194  temperature = humidity = None
195 
196  self._attr_current_temperature_attr_current_temperature = temperature
197  self._attr_current_humidity_attr_current_humidity = humidity
198  self._attr_target_temperature_attr_target_temperature = self._climate_climate.temp_celsius
199  self._attr_fan_mode_attr_fan_mode = LOOKIN_FAN_MODE_IDX_TO_HASS[self._climate_climate.fan_mode]
200  self._attr_swing_mode_attr_swing_mode = LOOKIN_SWING_MODE_IDX_TO_HASS[self._climate_climate.swing_mode]
201  self._attr_hvac_mode_attr_hvac_mode = LOOKIN_HVAC_MODE_IDX_TO_HASS[self._climate_climate.hvac_mode]
202 
203  @callback
204  def _async_update_meteo_from_value(self, event: UDPEvent) -> None:
205  """Update temperature and humidity from UDP event."""
206  self._attr_current_temperature_attr_current_temperature = float(int(event.value[:4], 16)) / 10
207  self._attr_current_humidity_attr_current_humidity = float(int(event.value[-4:], 16)) / 10
208 
209  @callback
210  def _handle_coordinator_update(self) -> None:
211  """Handle updated data from the coordinator."""
212  self._async_update_from_data_async_update_from_data()
214 
215  @callback
216  def _async_push_update(self, event: UDPEvent) -> None:
217  """Process an update pushed via UDP."""
218  LOGGER.debug("Processing push message for %s: %s", self.entity_identity_id, event)
219  self._climate_climate.update_from_status(event.value)
220  self.coordinator.async_set_updated_data(self._climate_climate)
221 
222  async def async_added_to_hass(self) -> None:
223  """Call when the entity is added to hass."""
224  self.async_on_removeasync_on_remove(
225  self._lookin_udp_subs_lookin_udp_subs.subscribe_event(
226  self._lookin_device_lookin_device.id,
227  UDPCommandType.ir,
228  self._uuid_uuid,
229  self._async_push_update_async_push_update,
230  )
231  )
232  self.async_on_removeasync_on_remove(
233  self._lookin_udp_subs_lookin_udp_subs.subscribe_event(
234  self._lookin_device_lookin_device.id,
235  UDPCommandType.meteo,
236  None,
237  self._async_update_meteo_from_value_async_update_meteo_from_value,
238  )
239  )
240  return await super().async_added_to_hass()
None _async_update_meteo_from_value(self, UDPEvent event)
Definition: climate.py:204
None __init__(self, str uuid, Climate device, LookinData lookin_data, LookinDataUpdateCoordinator[Remote] coordinator)
Definition: climate.py:118
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:127
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:69