Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Climate device for CCM15 coordinator."""
2 
3 import logging
4 from typing import Any
5 
6 from ccm15 import CCM15DeviceState
7 
9  FAN_AUTO,
10  FAN_HIGH,
11  FAN_LOW,
12  FAN_MEDIUM,
13  PRECISION_WHOLE,
14  SWING_OFF,
15  SWING_ON,
16  ClimateEntity,
17  ClimateEntityFeature,
18  HVACMode,
19 )
20 from homeassistant.config_entries import ConfigEntry
21 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
22 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.device_registry import DeviceInfo
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.update_coordinator import CoordinatorEntity
26 
27 from .const import CONST_CMD_FAN_MAP, CONST_CMD_STATE_MAP, DOMAIN
28 from .coordinator import CCM15Coordinator
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 
34  hass: HomeAssistant,
35  config_entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up all climate."""
39  coordinator: CCM15Coordinator = hass.data[DOMAIN][config_entry.entry_id]
40 
41  ac_data: CCM15DeviceState = coordinator.data
42  entities = [
43  CCM15Climate(coordinator.get_host(), ac_index, coordinator)
44  for ac_index in ac_data.devices
45  ]
46  async_add_entities(entities)
47 
48 
49 class CCM15Climate(CoordinatorEntity[CCM15Coordinator], ClimateEntity):
50  """Climate device for CCM15 coordinator."""
51 
52  _attr_temperature_unit = UnitOfTemperature.CELSIUS
53  _attr_has_entity_name = True
54  _attr_target_temperature_step = PRECISION_WHOLE
55  _attr_hvac_modes = [
56  HVACMode.OFF,
57  HVACMode.HEAT,
58  HVACMode.COOL,
59  HVACMode.DRY,
60  HVACMode.FAN_ONLY,
61  HVACMode.AUTO,
62  ]
63  _attr_fan_modes = [FAN_AUTO, FAN_LOW, FAN_MEDIUM, FAN_HIGH]
64  _attr_swing_modes = [SWING_OFF, SWING_ON]
65  _attr_supported_features = (
66  ClimateEntityFeature.TARGET_TEMPERATURE
67  | ClimateEntityFeature.FAN_MODE
68  | ClimateEntityFeature.SWING_MODE
69  | ClimateEntityFeature.TURN_OFF
70  | ClimateEntityFeature.TURN_ON
71  )
72  _attr_name = None
73  _enable_turn_on_off_backwards_compatibility = False
74 
75  def __init__(
76  self, ac_host: str, ac_index: int, coordinator: CCM15Coordinator
77  ) -> None:
78  """Create a climate device managed from a coordinator."""
79  super().__init__(coordinator)
80  self._ac_index: int = ac_index
81  self._attr_unique_id_attr_unique_id = f"{ac_host}.{ac_index}"
82  self._attr_device_info_attr_device_info = DeviceInfo(
83  identifiers={
84  # Serial numbers are unique identifiers within a specific domain
85  (DOMAIN, f"{ac_host}.{ac_index}"),
86  },
87  name=f"Midea {ac_index}",
88  manufacturer="Midea",
89  model="CCM15",
90  )
91 
92  @property
93  def data(self) -> CCM15DeviceState | None:
94  """Return device data."""
95  return self.coordinator.get_ac_data(self._ac_index)
96 
97  @property
98  def current_temperature(self) -> int | None:
99  """Return current temperature."""
100  if (data := self.datadatadata) is not None:
101  return data.temperature
102  return None
103 
104  @property
105  def target_temperature(self) -> int | None:
106  """Return target temperature."""
107  if (data := self.datadatadata) is not None:
108  return data.temperature_setpoint
109  return None
110 
111  @property
112  def hvac_mode(self) -> HVACMode | None:
113  """Return hvac mode."""
114  if (data := self.datadatadata) is not None:
115  mode = data.ac_mode
116  return CONST_CMD_STATE_MAP[mode]
117  return None
118 
119  @property
120  def fan_mode(self) -> str | None:
121  """Return fan mode."""
122  if (data := self.datadatadata) is not None:
123  mode = data.fan_mode
124  return CONST_CMD_FAN_MAP[mode]
125  return None
126 
127  @property
128  def swing_mode(self) -> str | None:
129  """Return swing mode."""
130  if (data := self.datadatadata) is not None:
131  return SWING_ON if data.is_swing_on else SWING_OFF
132  return None
133 
134  @property
135  def available(self) -> bool:
136  """Return the availability of the entity."""
137  return self.datadatadata is not None
138 
139  @property
140  def extra_state_attributes(self) -> dict[str, Any]:
141  """Return the optional state attributes."""
142  if (data := self.datadatadata) is not None:
143  return {"error_code": data.error_code}
144  return {}
145 
146  async def async_set_temperature(self, **kwargs: Any) -> None:
147  """Set the target temperature."""
148  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is not None:
149  await self.coordinator.async_set_temperature(self._ac_index, temperature)
150 
151  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
152  """Set the hvac mode."""
153  await self.coordinator.async_set_hvac_mode(self._ac_index, hvac_mode)
154 
155  async def async_set_fan_mode(self, fan_mode: str) -> None:
156  """Set the fan mode."""
157  await self.coordinator.async_set_fan_mode(self._ac_index, fan_mode)
158 
159  async def async_turn_off(self) -> None:
160  """Turn off."""
161  await self.async_set_hvac_modeasync_set_hvac_modeasync_set_hvac_modeasync_set_hvac_mode(HVACMode.OFF)
162 
163  async def async_turn_on(self) -> None:
164  """Turn on."""
165  await self.async_set_hvac_modeasync_set_hvac_modeasync_set_hvac_modeasync_set_hvac_mode(HVACMode.AUTO)
None async_set_temperature(self, **Any kwargs)
Definition: climate.py:146
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:151
None __init__(self, str ac_host, int ac_index, CCM15Coordinator coordinator)
Definition: climate.py:77
None async_set_hvac_mode(self, ac_index, HVACMode hvac_mode)
Definition: coordinator.py:64
CCM15SlaveDevice|None get_ac_data(self, int ac_index)
Definition: coordinator.py:57
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: __init__.py:813
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:37