Home Assistant Unofficial Reference 2024.12.1
humidifier.py
Go to the documentation of this file.
1 """Support for using humidifier with ecobee thermostats."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 
8  DEFAULT_MAX_HUMIDITY,
9  DEFAULT_MIN_HUMIDITY,
10  MODE_AUTO,
11  HumidifierDeviceClass,
12  HumidifierEntity,
13  HumidifierEntityFeature,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
21 
22 SCAN_INTERVAL = timedelta(minutes=3)
23 
24 MODE_MANUAL = "manual"
25 MODE_OFF = "off"
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the ecobee thermostat humidifier entity."""
34  data = hass.data[DOMAIN]
35  entities = []
36  for index in range(len(data.ecobee.thermostats)):
37  thermostat = data.ecobee.get_thermostat(index)
38  if thermostat["settings"]["hasHumidifier"]:
39  entities.append(EcobeeHumidifier(data, index))
40 
41  async_add_entities(entities, True)
42 
43 
45  """A humidifier class for an ecobee thermostat with humidifier attached."""
46 
47  _attr_supported_features = HumidifierEntityFeature.MODES
48  _attr_available_modes = [MODE_OFF, MODE_AUTO, MODE_MANUAL]
49  _attr_device_class = HumidifierDeviceClass.HUMIDIFIER
50  _attr_min_humidity = DEFAULT_MIN_HUMIDITY
51  _attr_max_humidity = DEFAULT_MAX_HUMIDITY
52  _attr_has_entity_name = True
53  _attr_name = None
54 
55  def __init__(self, data, thermostat_index):
56  """Initialize ecobee humidifier platform."""
57  self.datadata = data
58  self.thermostat_indexthermostat_index = thermostat_index
59  self.thermostatthermostat = self.datadata.ecobee.get_thermostat(self.thermostat_indexthermostat_index)
60  self._attr_unique_id_attr_unique_id = self.thermostatthermostat["identifier"]
61  self._last_humidifier_on_mode_last_humidifier_on_mode = MODE_MANUAL
62 
63  self.update_without_throttleupdate_without_throttle = False
64 
65  @property
66  def device_info(self) -> DeviceInfo:
67  """Return device information for the ecobee humidifier."""
68  model: str | None
69  try:
70  model = f"{ECOBEE_MODEL_TO_NAME[self.thermostat['modelNumber']]} Thermostat"
71  except KeyError:
72  # Ecobee model is not in our list
73  model = None
74 
75  return DeviceInfo(
76  identifiers={(DOMAIN, self.thermostatthermostat["identifier"])},
77  manufacturer=MANUFACTURER,
78  model=model,
79  name=self.thermostatthermostat["name"],
80  )
81 
82  @property
83  def available(self):
84  """Return if device is available."""
85  return self.thermostatthermostat["runtime"]["connected"]
86 
87  async def async_update(self):
88  """Get the latest state from the thermostat."""
89  if self.update_without_throttleupdate_without_throttle:
90  await self.datadata.update(no_throttle=True)
91  self.update_without_throttleupdate_without_throttle = False
92  else:
93  await self.datadata.update()
94  self.thermostatthermostat = self.datadata.ecobee.get_thermostat(self.thermostat_indexthermostat_index)
95  if self.modemodemode != MODE_OFF:
96  self._last_humidifier_on_mode_last_humidifier_on_mode = self.modemodemode
97 
98  @property
99  def is_on(self):
100  """Return True if the humidifier is on."""
101  return self.modemodemode != MODE_OFF
102 
103  @property
104  def mode(self):
105  """Return the current mode, e.g., off, auto, manual."""
106  return self.thermostatthermostat["settings"]["humidifierMode"]
107 
108  @property
109  def target_humidity(self) -> int:
110  """Return the desired humidity set point."""
111  return int(self.thermostatthermostat["runtime"]["desiredHumidity"])
112 
113  @property
114  def current_humidity(self) -> int | None:
115  """Return the current humidity."""
116  try:
117  return int(self.thermostatthermostat["runtime"]["actualHumidity"])
118  except KeyError:
119  return None
120 
121  def set_mode(self, mode):
122  """Set humidifier mode (auto, off, manual)."""
123  if mode.lower() not in (self.available_modesavailable_modes):
124  raise ValueError(
125  f"Invalid mode value: {mode} Valid values are"
126  f" {', '.join(self.available_modes)}."
127  )
128 
129  self.datadata.ecobee.set_humidifier_mode(self.thermostat_indexthermostat_index, mode)
130  self.update_without_throttleupdate_without_throttle = True
131 
132  def set_humidity(self, humidity: int) -> None:
133  """Set the humidity level."""
134  self.datadata.ecobee.set_humidity(self.thermostat_indexthermostat_index, humidity)
135  self.update_without_throttleupdate_without_throttle = True
136 
137  def turn_off(self, **kwargs):
138  """Set humidifier to off mode."""
139  self.set_modeset_modeset_mode(MODE_OFF)
140 
141  def turn_on(self, **kwargs):
142  """Set humidifier to on mode."""
143  self.set_modeset_modeset_mode(self._last_humidifier_on_mode_last_humidifier_on_mode)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: humidifier.py:32
IssData update(pyiss.ISS iss)
Definition: __init__.py:33