Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Climate integration microBees."""
2 
3 from typing import Any
4 
6  ClimateEntity,
7  ClimateEntityFeature,
8  HVACMode,
9 )
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .coordinator import MicroBeesUpdateCoordinator
18 from .entity import MicroBeesActuatorEntity
19 
20 CLIMATE_PRODUCT_IDS = {
21  76, # Thermostat,
22  78, # Thermovalve,
23 }
24 THERMOSTAT_SENSOR_ID = 762
25 THERMOVALVE_SENSOR_ID = 782
26 
27 
29  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 ) -> None:
31  """Set up the microBees climate platform."""
32  coordinator: MicroBeesUpdateCoordinator = hass.data[DOMAIN][
33  entry.entry_id
34  ].coordinator
36  MBClimate(
37  coordinator,
38  bee_id,
39  bee.actuators[0].id,
40  next(
41  sensor.id
42  for sensor in bee.sensors
43  if sensor.deviceID
44  == (
45  THERMOSTAT_SENSOR_ID
46  if bee.productID == 76
47  else THERMOVALVE_SENSOR_ID
48  )
49  ),
50  )
51  for bee_id, bee in coordinator.data.bees.items()
52  if bee.productID in CLIMATE_PRODUCT_IDS
53  )
54 
55 
57  """Representation of a microBees climate."""
58 
59  _attr_temperature_unit = UnitOfTemperature.CELSIUS
60  _attr_target_temperature_step = 0.5
61  _attr_supported_features = (
62  ClimateEntityFeature.TARGET_TEMPERATURE
63  | ClimateEntityFeature.TURN_ON
64  | ClimateEntityFeature.TURN_OFF
65  )
66  _attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF]
67  _attr_fan_modes = None
68  _attr_min_temp = 15
69  _attr_max_temp = 35
70  _attr_name = None
71 
72  def __init__(
73  self,
74  coordinator: MicroBeesUpdateCoordinator,
75  bee_id: int,
76  actuator_id: int,
77  sensor_id: int,
78  ) -> None:
79  """Initialize the microBees climate."""
80  super().__init__(coordinator, bee_id, actuator_id)
81  self.sensor_idsensor_id = sensor_id
82 
83  @property
84  def current_temperature(self) -> float | None:
85  """Return the sensor temperature."""
86  return self.coordinator.data.sensors[self.sensor_idsensor_id].value
87 
88  @property
89  def hvac_mode(self) -> HVACMode | None:
90  """Return current hvac operation i.e. heat, cool mode."""
91  if self.actuatoractuator.value == 1:
92  return HVACMode.HEAT
93  return HVACMode.OFF
94 
95  @property
96  def target_temperature(self) -> float | None:
97  """Return the current target temperature."""
98  return self.beebee.instanceData.targetTemp
99 
100  async def async_set_temperature(self, **kwargs: Any) -> None:
101  """Set new target temperature."""
102  temperature = kwargs.get(ATTR_TEMPERATURE)
103  send_command = await self.coordinator.microbees.sendCommand(
104  self.actuator_idactuator_id, self.actuatoractuator.value, temperature=temperature
105  )
106 
107  if not send_command:
108  raise HomeAssistantError(f"Failed to set temperature {self.name}")
109 
110  self.beebee.instanceData.targetTemp = temperature
111  self.async_write_ha_stateasync_write_ha_state()
112 
113  async def async_set_hvac_mode(self, hvac_mode: HVACMode, **kwargs: Any) -> None:
114  """Set new target hvac mode."""
115  if hvac_mode == HVACMode.OFF:
116  return await self.async_turn_offasync_turn_offasync_turn_off()
117  return await self.async_turn_onasync_turn_onasync_turn_on()
118 
119  async def async_turn_on(self, **kwargs: Any) -> None:
120  """Turn on the climate."""
121  temperature = kwargs.get(ATTR_TEMPERATURE)
122  send_command = await self.coordinator.microbees.sendCommand(
123  self.actuator_idactuator_id, 1, temperature=temperature
124  )
125 
126  if not send_command:
127  raise HomeAssistantError(f"Failed to set temperature {self.name}")
128 
129  self.actuatoractuator.value = 1
130  self._attr_hvac_mode_attr_hvac_mode = HVACMode.HEAT
131  self.async_write_ha_stateasync_write_ha_state()
132 
133  async def async_turn_off(self, **kwargs: Any) -> None:
134  """Turn off the climate."""
135  temperature = kwargs.get(ATTR_TEMPERATURE)
136  send_command = await self.coordinator.microbees.sendCommand(
137  self.actuator_idactuator_id, 0, temperature=temperature
138  )
139 
140  if not send_command:
141  raise HomeAssistantError(f"Failed to set temperature {self.name}")
142 
143  self.actuatoractuator.value = 0
144  self._attr_hvac_mode_attr_hvac_mode = HVACMode.OFF
145  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, MicroBeesUpdateCoordinator coordinator, int bee_id, int actuator_id, int sensor_id)
Definition: climate.py:78
None async_set_hvac_mode(self, HVACMode hvac_mode, **Any kwargs)
Definition: climate.py:113
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:30