Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for the Hive climate devices."""
2 
3 from datetime import timedelta
4 import logging
5 from typing import Any
6 
7 from apyhiveapi import Hive
8 import voluptuous as vol
9 
11  PRESET_BOOST,
12  PRESET_NONE,
13  ClimateEntity,
14  ClimateEntityFeature,
15  HVACAction,
16  HVACMode,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers import config_validation as cv, entity_platform
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 
24 from . import refresh_system
25 from .const import (
26  ATTR_TIME_PERIOD,
27  DOMAIN,
28  SERVICE_BOOST_HEATING_OFF,
29  SERVICE_BOOST_HEATING_ON,
30 )
31 from .entity import HiveEntity
32 
33 HIVE_TO_HASS_STATE = {
34  "SCHEDULE": HVACMode.AUTO,
35  "MANUAL": HVACMode.HEAT,
36  "OFF": HVACMode.OFF,
37 }
38 
39 HASS_TO_HIVE_STATE = {
40  HVACMode.AUTO: "SCHEDULE",
41  HVACMode.HEAT: "MANUAL",
42  HVACMode.OFF: "OFF",
43 }
44 
45 HIVE_TO_HASS_HVAC_ACTION = {
46  "UNKNOWN": HVACAction.OFF,
47  False: HVACAction.IDLE,
48  True: HVACAction.HEATING,
49 }
50 
51 TEMP_UNIT = {
52  "C": UnitOfTemperature.CELSIUS,
53  "F": UnitOfTemperature.FAHRENHEIT,
54 }
55 PARALLEL_UPDATES = 0
56 SCAN_INTERVAL = timedelta(seconds=15)
57 _LOGGER = logging.getLogger()
58 
59 
61  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
62 ) -> None:
63  """Set up Hive thermostat based on a config entry."""
64 
65  hive = hass.data[DOMAIN][entry.entry_id]
66  devices = hive.session.deviceList.get("climate")
67  if devices:
68  async_add_entities((HiveClimateEntity(hive, dev) for dev in devices), True)
69 
70  platform = entity_platform.async_get_current_platform()
71 
72  platform.async_register_entity_service(
73  SERVICE_BOOST_HEATING_ON,
74  {
75  vol.Required(ATTR_TIME_PERIOD): vol.All(
76  cv.time_period,
77  cv.positive_timedelta,
78  lambda td: td.total_seconds() // 60,
79  ),
80  vol.Optional(ATTR_TEMPERATURE, default="25.0"): vol.Coerce(float),
81  },
82  "async_heating_boost_on",
83  )
84 
85  platform.async_register_entity_service(
86  SERVICE_BOOST_HEATING_OFF,
87  None,
88  "async_heating_boost_off",
89  )
90 
91 
93  """Hive Climate Device."""
94 
95  _attr_hvac_modes = [HVACMode.AUTO, HVACMode.HEAT, HVACMode.OFF]
96  _attr_preset_modes = [PRESET_BOOST, PRESET_NONE]
97  _attr_supported_features = (
98  ClimateEntityFeature.TARGET_TEMPERATURE
99  | ClimateEntityFeature.PRESET_MODE
100  | ClimateEntityFeature.TURN_OFF
101  | ClimateEntityFeature.TURN_ON
102  )
103  _enable_turn_on_off_backwards_compatibility = False
104 
105  def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
106  """Initialize the Climate device."""
107  super().__init__(hive, hive_device)
108  self.thermostat_node_idthermostat_node_id = hive_device["device_id"]
109  self._attr_temperature_unit_attr_temperature_unit = TEMP_UNIT[hive_device["temperatureunit"]]
110 
111  @refresh_system
112  async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
113  """Set new target hvac mode."""
114  new_mode = HASS_TO_HIVE_STATE[hvac_mode]
115  await self.hivehive.heating.setMode(self.devicedevicedevice, new_mode)
116 
117  @refresh_system
118  async def async_set_temperature(self, **kwargs: Any) -> None:
119  """Set new target temperature."""
120  new_temperature = kwargs.get(ATTR_TEMPERATURE)
121  if new_temperature is not None:
122  await self.hivehive.heating.setTargetTemperature(self.devicedevicedevice, new_temperature)
123 
124  @refresh_system
125  async def async_set_preset_mode(self, preset_mode: str) -> None:
126  """Set new preset mode."""
127  if preset_mode == PRESET_NONE and self.preset_modepreset_modepreset_mode == PRESET_BOOST:
128  await self.hivehive.heating.setBoostOff(self.devicedevicedevice)
129  elif preset_mode == PRESET_BOOST:
130  curtemp = round((self.current_temperaturecurrent_temperature or 0) * 2) / 2
131  temperature = curtemp + 0.5
132  await self.hivehive.heating.setBoostOn(self.devicedevicedevice, 30, temperature)
133 
134  @refresh_system
135  async def async_heating_boost_on(self, time_period, temperature):
136  """Handle boost heating service call."""
137  await self.hivehive.heating.setBoostOn(self.devicedevicedevice, time_period, temperature)
138 
139  @refresh_system
140  async def async_heating_boost_off(self) -> None:
141  """Handle boost heating service call."""
142  await self.hivehive.heating.setBoostOff(self.devicedevicedevice)
143 
144  async def async_update(self) -> None:
145  """Update all Node data from Hive."""
146  await self.hivehive.session.updateData(self.devicedevicedevice)
147  self.devicedevicedevice = await self.hivehive.heating.getClimate(self.devicedevicedevice)
148  self._attr_available_attr_available = self.devicedevicedevice["deviceData"].get("online")
149  if self._attr_available_attr_available:
150  self._attr_hvac_mode_attr_hvac_mode = HIVE_TO_HASS_STATE.get(self.devicedevicedevice["status"]["mode"])
151  self._attr_hvac_action_attr_hvac_action = HIVE_TO_HASS_HVAC_ACTION.get(
152  self.devicedevicedevice["status"]["action"]
153  )
154  self._attr_current_temperature_attr_current_temperature = self.devicedevicedevice["status"][
155  "current_temperature"
156  ]
157  self._attr_target_temperature_attr_target_temperature = self.devicedevicedevice["status"]["target_temperature"]
158  self._attr_min_temp_attr_min_temp = self.devicedevicedevice["min_temp"]
159  self._attr_max_temp_attr_max_temp = self.devicedevicedevice["max_temp"]
160  if self.devicedevicedevice["status"]["boost"] == "ON":
161  self._attr_preset_mode_attr_preset_mode = PRESET_BOOST
162  self._attr_hvac_mode_attr_hvac_mode = HVACMode.HEAT
163  else:
164  self._attr_preset_mode_attr_preset_mode = PRESET_NONE
def async_heating_boost_on(self, time_period, temperature)
Definition: climate.py:135
None __init__(self, Hive hive, dict[str, Any] hive_device)
Definition: climate.py:105
None async_set_hvac_mode(self, HVACMode hvac_mode)
Definition: climate.py:112
None async_set_preset_mode(self, str preset_mode)
Definition: climate.py:125
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: climate.py:62