Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """CoolMasterNet platform to control of CoolMasterNet Climate Devices."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pycoolmasternet_async import SWING_MODES
9 
11  ClimateEntity,
12  ClimateEntityFeature,
13  HVACMode,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import CONF_SUPPORTED_MODES, DATA_COORDINATOR, DATA_INFO, DOMAIN
22 from .entity import CoolmasterEntity
23 
24 CM_TO_HA_STATE = {
25  "heat": HVACMode.HEAT,
26  "cool": HVACMode.COOL,
27  "auto": HVACMode.HEAT_COOL,
28  "dry": HVACMode.DRY,
29  "fan": HVACMode.FAN_ONLY,
30 }
31 
32 HA_STATE_TO_CM = {value: key for key, value in CM_TO_HA_STATE.items()}
33 
34 FAN_MODES = ["low", "med", "high", "auto"]
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
40  hass: HomeAssistant,
41  config_entry: ConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Set up the CoolMasterNet climate platform."""
45  info = hass.data[DOMAIN][config_entry.entry_id][DATA_INFO]
46  coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
47  supported_modes = config_entry.data.get(CONF_SUPPORTED_MODES)
49  CoolmasterClimate(coordinator, unit_id, info, supported_modes)
50  for unit_id in coordinator.data
51  )
52 
53 
55  """Representation of a coolmaster climate device."""
56 
57  _attr_name = None
58  _enable_turn_on_off_backwards_compatibility = False
59 
60  def __init__(self, coordinator, unit_id, info, supported_modes):
61  """Initialize the climate device."""
62  super().__init__(coordinator, unit_id, info)
63  self._attr_hvac_modes_attr_hvac_modes = supported_modes
64  self._attr_unique_id_attr_unique_id = unit_id
65 
66  @property
67  def supported_features(self) -> ClimateEntityFeature:
68  """Return the list of supported features."""
69  supported_features = (
70  ClimateEntityFeature.TARGET_TEMPERATURE
71  | ClimateEntityFeature.FAN_MODE
72  | ClimateEntityFeature.TURN_OFF
73  | ClimateEntityFeature.TURN_ON
74  )
75  if self.swing_modeswing_modeswing_mode:
76  supported_features |= ClimateEntityFeature.SWING_MODE
77  return supported_features
78 
79  @property
80  def temperature_unit(self) -> str:
81  """Return the unit of measurement."""
82  if self._unit_unit_unit.temperature_unit == "celsius":
83  return UnitOfTemperature.CELSIUS
84 
85  return UnitOfTemperature.FAHRENHEIT
86 
87  @property
89  """Return the current temperature."""
90  return self._unit_unit_unit.temperature
91 
92  @property
93  def target_temperature(self):
94  """Return the temperature we are trying to reach."""
95  return self._unit_unit_unit.thermostat
96 
97  @property
98  def hvac_mode(self):
99  """Return hvac target hvac state."""
100  mode = self._unit_unit_unit.mode
101  if not self._unit_unit_unit.is_on:
102  return HVACMode.OFF
103 
104  return CM_TO_HA_STATE[mode]
105 
106  @property
107  def fan_mode(self):
108  """Return the fan setting."""
109  return self._unit_unit_unit.fan_speed
110 
111  @property
112  def fan_modes(self):
113  """Return the list of available fan modes."""
114  return FAN_MODES
115 
116  @property
117  def swing_mode(self) -> str | None:
118  """Return the swing mode setting."""
119  return self._unit_unit_unit.swing
120 
121  @property
122  def swing_modes(self) -> list[str] | None:
123  """Return swing modes if supported."""
124  return SWING_MODES if self.swing_modeswing_modeswing_mode is not None else None
125 
126  async def async_set_temperature(self, **kwargs: Any) -> None:
127  """Set new target temperatures."""
128  if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None:
129  _LOGGER.debug("Setting temp of %s to %s", self.unique_idunique_id, str(temp))
130  self._unit_unit_unit = await self._unit_unit_unit.set_thermostat(temp)
131  self.async_write_ha_stateasync_write_ha_state()
132 
133  async def async_set_fan_mode(self, fan_mode: str) -> None:
134  """Set new fan mode."""
135  _LOGGER.debug("Setting fan mode of %s to %s", self.unique_idunique_id, fan_mode)
136  self._unit_unit_unit = await self._unit_unit_unit.set_fan_speed(fan_mode)
137  self.async_write_ha_stateasync_write_ha_state()
138 
139  async def async_set_swing_mode(self, swing_mode: str) -> None:
140  """Set new swing mode."""
141  _LOGGER.debug("Setting swing mode of %s to %s", self.unique_idunique_id, swing_mode)
142  try:
143  self._unit_unit_unit = await self._unit_unit_unit.set_swing(swing_mode)
144  except ValueError as error:
145  raise HomeAssistantError(error) from error
146  self.async_write_ha_stateasync_write_ha_state()
147 
148  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
149  """Set new operation mode."""
150  _LOGGER.debug("Setting operation mode of %s to %s", self.unique_idunique_id, hvac_mode)
151 
152  if hvac_mode == HVACMode.OFF:
153  await self.async_turn_offasync_turn_offasync_turn_off()
154  else:
155  self._unit_unit_unit = await self._unit_unit_unit.set_mode(HA_STATE_TO_CM[hvac_mode])
156  await self.async_turn_onasync_turn_onasync_turn_on()
157 
158  async def async_turn_on(self) -> None:
159  """Turn on."""
160  _LOGGER.debug("Turning %s on", self.unique_idunique_id)
161  self._unit_unit_unit = await self._unit_unit_unit.turn_on()
162  self.async_write_ha_stateasync_write_ha_state()
163 
164  async def async_turn_off(self) -> None:
165  """Turn off."""
166  _LOGGER.debug("Turning %s off", self.unique_idunique_id)
167  self._unit_unit_unit = await self._unit_unit_unit.turn_off()
168  self.async_write_ha_stateasync_write_ha_state()
def __init__(self, coordinator, unit_id, info, supported_modes)
Definition: climate.py:60
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:43