Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """The Flexit Nordic (BACnet) integration."""
2 
3 import asyncio.exceptions
4 from typing import Any
5 
6 from flexit_bacnet import (
7  VENTILATION_MODE_AWAY,
8  VENTILATION_MODE_HOME,
9  VENTILATION_MODE_STOP,
10 )
11 from flexit_bacnet.bacnet import DecodingError
12 
14  PRESET_AWAY,
15  PRESET_BOOST,
16  PRESET_HOME,
17  ClimateEntity,
18  ClimateEntityFeature,
19  HVACAction,
20  HVACMode,
21 )
22 from homeassistant.config_entries import ConfigEntry
23 from homeassistant.const import ATTR_TEMPERATURE, PRECISION_HALVES, UnitOfTemperature
24 from homeassistant.core import HomeAssistant
25 from homeassistant.exceptions import HomeAssistantError
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 
28 from .const import (
29  DOMAIN,
30  MAX_TEMP,
31  MIN_TEMP,
32  PRESET_TO_VENTILATION_MODE_MAP,
33  VENTILATION_TO_PRESET_MODE_MAP,
34 )
35 from .coordinator import FlexitCoordinator
36 from .entity import FlexitEntity
37 
38 
40  hass: HomeAssistant,
41  config_entry: ConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Set up the Flexit Nordic unit."""
45  coordinator: FlexitCoordinator = hass.data[DOMAIN][config_entry.entry_id]
46 
48 
49 
51  """Flexit air handling unit."""
52 
53  _attr_name = None
54 
55  _attr_hvac_modes = [
56  HVACMode.OFF,
57  HVACMode.FAN_ONLY,
58  ]
59 
60  _attr_preset_modes = [
61  PRESET_AWAY,
62  PRESET_HOME,
63  PRESET_BOOST,
64  ]
65 
66  _attr_supported_features = (
67  ClimateEntityFeature.PRESET_MODE
68  | ClimateEntityFeature.TARGET_TEMPERATURE
69  | ClimateEntityFeature.TURN_OFF
70  | ClimateEntityFeature.TURN_ON
71  )
72 
73  _attr_target_temperature_step = PRECISION_HALVES
74  _attr_temperature_unit = UnitOfTemperature.CELSIUS
75  _attr_max_temp = MAX_TEMP
76  _attr_min_temp = MIN_TEMP
77  _enable_turn_on_off_backwards_compatibility = False
78 
79  def __init__(self, coordinator: FlexitCoordinator) -> None:
80  """Initialize the Flexit unit."""
81  super().__init__(coordinator)
82  self._attr_unique_id_attr_unique_id = coordinator.device.serial_number
83 
84  async def async_update(self) -> None:
85  """Refresh unit state."""
86  await self.devicedevicedevice.update()
87 
88  @property
89  def hvac_action(self) -> HVACAction | None:
90  """Return current HVAC action."""
91  if self.devicedevicedevice.electric_heater:
92  return HVACAction.HEATING
93  return HVACAction.FAN
94 
95  @property
96  def current_temperature(self) -> float:
97  """Return the current temperature."""
98  return self.devicedevicedevice.room_temperature
99 
100  @property
101  def target_temperature(self) -> float:
102  """Return the temperature we try to reach."""
103  if self.devicedevicedevice.ventilation_mode == VENTILATION_MODE_AWAY:
104  return self.devicedevicedevice.air_temp_setpoint_away
105 
106  return self.devicedevicedevice.air_temp_setpoint_home
107 
108  async def async_set_temperature(self, **kwargs: Any) -> None:
109  """Set new target temperature."""
110  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
111  return
112 
113  try:
114  if self.devicedevicedevice.ventilation_mode == VENTILATION_MODE_AWAY:
115  await self.devicedevicedevice.set_air_temp_setpoint_away(temperature)
116  else:
117  await self.devicedevicedevice.set_air_temp_setpoint_home(temperature)
118  except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
119  raise HomeAssistantError from exc
120  finally:
121  await self.coordinator.async_refresh()
122 
123  @property
124  def preset_mode(self) -> str:
125  """Return the current preset mode, e.g., home, away, temp.
126 
127  Requires ClimateEntityFeature.PRESET_MODE.
128  """
129  return VENTILATION_TO_PRESET_MODE_MAP[self.devicedevicedevice.ventilation_mode]
130 
131  async def async_set_preset_mode(self, preset_mode: str) -> None:
132  """Set new preset mode."""
133  ventilation_mode = PRESET_TO_VENTILATION_MODE_MAP[preset_mode]
134 
135  try:
136  await self.devicedevicedevice.set_ventilation_mode(ventilation_mode)
137  except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
138  raise HomeAssistantError from exc
139  finally:
140  await self.coordinator.async_refresh()
141 
142  @property
143  def hvac_mode(self) -> HVACMode:
144  """Return hvac operation ie. heat, cool mode."""
145  if self.devicedevicedevice.ventilation_mode == VENTILATION_MODE_STOP:
146  return HVACMode.OFF
147 
148  return HVACMode.FAN_ONLY
149 
150  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
151  """Set new target hvac mode."""
152  try:
153  if hvac_mode == HVACMode.OFF:
154  await self.devicedevicedevice.set_ventilation_mode(VENTILATION_MODE_STOP)
155  else:
156  await self.devicedevicedevice.set_ventilation_mode(VENTILATION_MODE_HOME)
157  except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
158  raise HomeAssistantError from exc
159  finally:
160  await self.coordinator.async_refresh()
None __init__(self, FlexitCoordinator coordinator)
Definition: climate.py:79
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:43
IssData update(pyiss.ISS iss)
Definition: __init__.py:33