Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for Broadlink climate devices."""
2 
3 from enum import IntEnum
4 from typing import Any
5 
7  ATTR_TEMPERATURE,
8  ClimateEntity,
9  ClimateEntityFeature,
10  HVACAction,
11  HVACMode,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import PRECISION_HALVES, Platform, UnitOfTemperature
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN, DOMAINS_AND_TYPES
19 from .device import BroadlinkDevice
20 from .entity import BroadlinkEntity
21 
22 
23 class SensorMode(IntEnum):
24  """Thermostat sensor modes."""
25 
26  INNER_SENSOR_CONTROL = 0
27  OUTER_SENSOR_CONTROL = 1
28  INNER_SENSOR_CONTROL_OUTER_LIMIT = 2
29 
30 
32  hass: HomeAssistant,
33  config_entry: ConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up the Broadlink climate entities."""
37  device = hass.data[DOMAIN].devices[config_entry.entry_id]
38 
39  if device.api.type in DOMAINS_AND_TYPES[Platform.CLIMATE]:
41 
42 
44  """Representation of a Broadlink Hysen climate entity."""
45 
46  _attr_has_entity_name = True
47  _attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF, HVACMode.AUTO]
48  _attr_supported_features = (
49  ClimateEntityFeature.TARGET_TEMPERATURE
50  | ClimateEntityFeature.TURN_OFF
51  | ClimateEntityFeature.TURN_ON
52  )
53  _attr_target_temperature_step = PRECISION_HALVES
54  _attr_temperature_unit = UnitOfTemperature.CELSIUS
55  _enable_turn_on_off_backwards_compatibility = False
56 
57  def __init__(self, device: BroadlinkDevice) -> None:
58  """Initialize the climate entity."""
59  super().__init__(device)
60  self._attr_unique_id_attr_unique_id = device.unique_id
61  self._attr_hvac_mode_attr_hvac_mode = None
62  self.sensor_modesensor_mode = SensorMode.INNER_SENSOR_CONTROL
63 
64  async def async_set_temperature(self, **kwargs: Any) -> None:
65  """Set new target temperature."""
66  temperature = kwargs[ATTR_TEMPERATURE]
67  await self._device_device.async_request(self._device_device.api.set_temp, temperature)
68  self._attr_target_temperature_attr_target_temperature = temperature
69  self.async_write_ha_stateasync_write_ha_state()
70 
71  @callback
72  def _update_state(self, data: dict[str, Any]) -> None:
73  """Update data."""
74  if (sensor := data.get("sensor")) is not None:
75  self.sensor_modesensor_mode = SensorMode(sensor)
76  if data.get("power"):
77  if data.get("auto_mode"):
78  self._attr_hvac_mode_attr_hvac_mode = HVACMode.AUTO
79  else:
80  self._attr_hvac_mode_attr_hvac_mode = HVACMode.HEAT
81 
82  if data.get("active"):
83  self._attr_hvac_action_attr_hvac_action = HVACAction.HEATING
84  else:
85  self._attr_hvac_action_attr_hvac_action = HVACAction.IDLE
86  else:
87  self._attr_hvac_mode_attr_hvac_mode = HVACMode.OFF
88  self._attr_hvac_action_attr_hvac_action = HVACAction.OFF
89  if self.sensor_modesensor_mode is SensorMode.OUTER_SENSOR_CONTROL:
90  self._attr_current_temperature_attr_current_temperature = data.get("external_temp")
91  else:
92  self._attr_current_temperature_attr_current_temperature = data.get("room_temp")
93  self._attr_target_temperature_attr_target_temperature = data.get("thermostat_temp")
94 
95  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
96  """Set new target hvac mode."""
97  if hvac_mode == HVACMode.OFF:
98  await self._device_device.async_request(self._device_device.api.set_power, 0)
99  else:
100  await self._device_device.async_request(self._device_device.api.set_power, 1)
101  mode = 0 if hvac_mode == HVACMode.HEAT else 1
102  await self._device_device.async_request(
103  self._device_device.api.set_mode, mode, 0, self.sensor_modesensor_mode.value
104  )
105 
106  self._attr_hvac_mode_attr_hvac_mode = hvac_mode
107  self.async_write_ha_stateasync_write_ha_state()