Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for SwitchBot Air Conditioner remotes."""
2 
3 from typing import Any
4 
5 from switchbot_api import AirConditionerCommands
6 
7 import homeassistant.components.climate as FanState
9  ClimateEntity,
10  ClimateEntityFeature,
11  HVACMode,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import SwitchbotCloudData
19 from .const import DOMAIN
20 from .entity import SwitchBotCloudEntity
21 
22 _SWITCHBOT_HVAC_MODES: dict[HVACMode, int] = {
23  HVACMode.HEAT_COOL: 1,
24  HVACMode.COOL: 2,
25  HVACMode.DRY: 3,
26  HVACMode.FAN_ONLY: 4,
27  HVACMode.HEAT: 5,
28 }
29 
30 _DEFAULT_SWITCHBOT_HVAC_MODE = _SWITCHBOT_HVAC_MODES[HVACMode.FAN_ONLY]
31 
32 _SWITCHBOT_FAN_MODES: dict[str, int] = {
33  FanState.FAN_AUTO: 1,
34  FanState.FAN_LOW: 2,
35  FanState.FAN_MEDIUM: 3,
36  FanState.FAN_HIGH: 4,
37 }
38 
39 _DEFAULT_SWITCHBOT_FAN_MODE = _SWITCHBOT_FAN_MODES[FanState.FAN_AUTO]
40 
41 
43  hass: HomeAssistant,
44  config: ConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up SwitchBot Cloud entry."""
48  data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
50  SwitchBotCloudAirConditioner(data.api, device, coordinator)
51  for device, coordinator in data.devices.climates
52  )
53 
54 
56  """Representation of a SwitchBot air conditioner.
57 
58  As it is an IR device, we don't know the actual state.
59  """
60 
61  _attr_assumed_state = True
62  _attr_supported_features = (
63  ClimateEntityFeature.FAN_MODE | ClimateEntityFeature.TARGET_TEMPERATURE
64  )
65  _attr_fan_modes = [
66  FanState.FAN_AUTO,
67  FanState.FAN_LOW,
68  FanState.FAN_MEDIUM,
69  FanState.FAN_HIGH,
70  ]
71  _attr_fan_mode = FanState.FAN_AUTO
72  _attr_hvac_modes = [
73  HVACMode.HEAT_COOL,
74  HVACMode.COOL,
75  HVACMode.DRY,
76  HVACMode.FAN_ONLY,
77  HVACMode.HEAT,
78  ]
79  _attr_hvac_mode = HVACMode.FAN_ONLY
80  _attr_temperature_unit = UnitOfTemperature.CELSIUS
81  _attr_target_temperature = 21
82  _attr_name = None
83  _enable_turn_on_off_backwards_compatibility = False
84 
85  async def _do_send_command(
86  self,
87  hvac_mode: HVACMode | None = None,
88  fan_mode: str | None = None,
89  temperature: float | None = None,
90  ) -> None:
91  new_temperature = temperature or self._attr_target_temperature_attr_target_temperature_attr_target_temperature
92  new_mode = _SWITCHBOT_HVAC_MODES.get(
93  hvac_mode or self._attr_hvac_mode_attr_hvac_mode, _DEFAULT_SWITCHBOT_HVAC_MODE
94  )
95  new_fan_speed = _SWITCHBOT_FAN_MODES.get(
96  fan_mode or self._attr_fan_mode_attr_fan_mode, _DEFAULT_SWITCHBOT_FAN_MODE
97  )
98  await self.send_api_commandsend_api_command(
99  AirConditionerCommands.SET_ALL,
100  parameters=f"{new_temperature},{new_mode},{new_fan_speed},on",
101  )
102 
103  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
104  """Set target hvac mode."""
105  await self._do_send_command_do_send_command(hvac_mode=hvac_mode)
106  self._attr_hvac_mode_attr_hvac_mode = hvac_mode
107  self.async_write_ha_stateasync_write_ha_state()
108 
109  async def async_set_fan_mode(self, fan_mode: str) -> None:
110  """Set target fan mode."""
111  await self._do_send_command_do_send_command(fan_mode=fan_mode)
112  self._attr_fan_mode_attr_fan_mode = fan_mode
113  self.async_write_ha_stateasync_write_ha_state()
114 
115  async def async_set_temperature(self, **kwargs: Any) -> None:
116  """Set target temperature."""
117  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
118  return
119  await self._do_send_command_do_send_command(temperature=temperature)
120  self._attr_target_temperature_attr_target_temperature_attr_target_temperature = temperature
121  self.async_write_ha_stateasync_write_ha_state()
None _do_send_command(self, HVACMode|None hvac_mode=None, str|None fan_mode=None, float|None temperature=None)
Definition: climate.py:90
None send_api_command(self, Commands command, str command_type="command", dict|str parameters="default")
Definition: entity.py:43
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Definition: climate.py:46