Home Assistant Unofficial Reference 2024.12.1
climate.py
Go to the documentation of this file.
1 """Support for the PRT Heatmiser thermostats using the V3 protocol."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from heatmiserv3 import connection, heatmiser
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA,
13  ClimateEntity,
14  ClimateEntityFeature,
15  HVACMode,
16 )
17 from homeassistant.const import (
18  ATTR_TEMPERATURE,
19  CONF_HOST,
20  CONF_ID,
21  CONF_NAME,
22  CONF_PORT,
23  UnitOfTemperature,
24 )
25 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 CONF_THERMOSTATS = "tstats"
33 
34 TSTATS_SCHEMA = vol.Schema(
35  vol.All(
36  cv.ensure_list,
37  [{vol.Required(CONF_ID): cv.positive_int, vol.Required(CONF_NAME): cv.string}],
38  )
39 )
40 
41 PLATFORM_SCHEMA = CLIMATE_PLATFORM_SCHEMA.extend(
42  {
43  vol.Required(CONF_HOST): cv.string,
44  vol.Required(CONF_PORT): cv.string,
45  vol.Optional(CONF_THERMOSTATS, default=[]): TSTATS_SCHEMA,
46  }
47 )
48 
49 
51  hass: HomeAssistant,
52  config: ConfigType,
53  add_entities: AddEntitiesCallback,
54  discovery_info: DiscoveryInfoType | None = None,
55 ) -> None:
56  """Set up the heatmiser thermostat."""
57 
58  heatmiser_v3_thermostat = heatmiser.HeatmiserThermostat
59 
60  host = config[CONF_HOST]
61  port = config[CONF_PORT]
62 
63  thermostats = config[CONF_THERMOSTATS]
64 
65  uh1_hub = connection.HeatmiserUH1(host, port)
66 
68  [
69  HeatmiserV3Thermostat(heatmiser_v3_thermostat, thermostat, uh1_hub)
70  for thermostat in thermostats
71  ],
72  True,
73  )
74 
75 
77  """Representation of a HeatmiserV3 thermostat."""
78 
79  _attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF]
80  _attr_supported_features = (
81  ClimateEntityFeature.TARGET_TEMPERATURE
82  | ClimateEntityFeature.TURN_OFF
83  | ClimateEntityFeature.TURN_ON
84  )
85  _enable_turn_on_off_backwards_compatibility = False
86 
87  def __init__(self, therm, device, uh1):
88  """Initialize the thermostat."""
89  self.thermtherm = therm(device[CONF_ID], "prt", uh1)
90  self.uh1uh1 = uh1
91  self._name_name = device[CONF_NAME]
92  self._current_temperature_current_temperature = None
93  self._target_temperature_target_temperature = None
94  self._id_id = device
95  self.dcbdcb = None
96  self._attr_hvac_mode_attr_hvac_mode = HVACMode.HEAT
97 
98  @property
99  def name(self):
100  """Return the name of the thermostat, if any."""
101  return self._name_name
102 
103  @property
105  """Return the current temperature."""
106  return self._current_temperature_current_temperature
107 
108  @property
110  """Return the temperature we try to reach."""
111  return self._target_temperature_target_temperature
112 
113  def set_temperature(self, **kwargs: Any) -> None:
114  """Set new target temperature."""
115  if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
116  return
117  self._target_temperature_target_temperature = int(temperature)
118  self.thermtherm.set_target_temp(self._target_temperature_target_temperature)
119 
120  def update(self) -> None:
121  """Get the latest data."""
122  self.uh1uh1.reopen()
123  if not self.uh1uh1.status:
124  _LOGGER.error("Failed to update device %s", self._name_name)
125  return
126  self.dcbdcb = self.thermtherm.read_dcb()
127  self._attr_temperature_unit_attr_temperature_unit = (
128  UnitOfTemperature.CELSIUS
129  if (self.thermtherm.get_temperature_format() == "C")
130  else UnitOfTemperature.FAHRENHEIT
131  )
132  self._current_temperature_current_temperature = int(self.thermtherm.get_floor_temp())
133  self._target_temperature_target_temperature = int(self.thermtherm.get_target_temp())
134  self._attr_hvac_mode_attr_hvac_mode = (
135  HVACMode.OFF
136  if (int(self.thermtherm.get_current_state()) == 0)
137  else HVACMode.HEAT
138  )
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: climate.py:55