Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Python Control of Nobø Hub - Nobø Energy Control."""
2 
3 from __future__ import annotations
4 
5 from pynobo import nobo
6 
7 from homeassistant.components.select import SelectEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import ATTR_NAME
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import (
16  ATTR_HARDWARE_VERSION,
17  ATTR_SERIAL,
18  ATTR_SOFTWARE_VERSION,
19  CONF_OVERRIDE_TYPE,
20  DOMAIN,
21  NOBO_MANUFACTURER,
22  OVERRIDE_TYPE_NOW,
23 )
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up any temperature sensors connected to the Nobø Ecohub."""
32 
33  # Setup connection with hub
34  hub: nobo = hass.data[DOMAIN][config_entry.entry_id]
35 
36  override_type = (
37  nobo.API.OVERRIDE_TYPE_NOW
38  if config_entry.options.get(CONF_OVERRIDE_TYPE) == OVERRIDE_TYPE_NOW
39  else nobo.API.OVERRIDE_TYPE_CONSTANT
40  )
41 
42  entities: list[SelectEntity] = [
43  NoboProfileSelector(zone_id, hub) for zone_id in hub.zones
44  ]
45  entities.append(NoboGlobalSelector(hub, override_type))
46  async_add_entities(entities, True)
47 
48 
50  """Global override selector for Nobø Ecohub."""
51 
52  _attr_has_entity_name = True
53  _attr_translation_key = "global_override"
54  _attr_device_class = "nobo_hub__override"
55  _attr_should_poll = False
56  _modes = {
57  nobo.API.OVERRIDE_MODE_NORMAL: "none",
58  nobo.API.OVERRIDE_MODE_AWAY: "away",
59  nobo.API.OVERRIDE_MODE_COMFORT: "comfort",
60  nobo.API.OVERRIDE_MODE_ECO: "eco",
61  }
62  _attr_options = list(_modes.values())
63  _attr_current_option: str | None = None
64 
65  def __init__(self, hub: nobo, override_type) -> None:
66  """Initialize the global override selector."""
67  self._nobo_nobo = hub
68  self._attr_unique_id_attr_unique_id = hub.hub_serial
69  self._override_type_override_type = override_type
70  self._attr_device_info_attr_device_info = DeviceInfo(
71  identifiers={(DOMAIN, hub.hub_serial)},
72  name=hub.hub_info[ATTR_NAME],
73  manufacturer=NOBO_MANUFACTURER,
74  model=f"Nobø Ecohub ({hub.hub_info[ATTR_HARDWARE_VERSION]})",
75  sw_version=hub.hub_info[ATTR_SOFTWARE_VERSION],
76  )
77 
78  async def async_added_to_hass(self) -> None:
79  """Register callback from hub."""
80  self._nobo_nobo.register_callback(self._after_update_after_update)
81 
82  async def async_will_remove_from_hass(self) -> None:
83  """Deregister callback from hub."""
84  self._nobo_nobo.deregister_callback(self._after_update_after_update)
85 
86  async def async_select_option(self, option: str) -> None:
87  """Set override."""
88  mode = [k for k, v in self._modes_modes.items() if v == option][0]
89  try:
90  await self._nobo_nobo.async_create_override(
91  mode, self._override_type_override_type, nobo.API.OVERRIDE_TARGET_GLOBAL
92  )
93  except Exception as exp:
94  raise HomeAssistantError from exp
95 
96  async def async_update(self) -> None:
97  """Fetch new state data for this zone."""
98  self._read_state_read_state()
99 
100  @callback
101  def _read_state(self) -> None:
102  for override in self._nobo_nobo.overrides.values():
103  if override["target_type"] == nobo.API.OVERRIDE_TARGET_GLOBAL:
104  self._attr_current_option_attr_current_option = self._modes_modes[override["mode"]]
105  break
106 
107  @callback
108  def _after_update(self, hub) -> None:
109  self._read_state_read_state()
110  self.async_write_ha_stateasync_write_ha_state()
111 
112 
114  """Week profile selector for Nobø zones."""
115 
116  _attr_translation_key = "week_profile"
117  _attr_has_entity_name = True
118  _attr_should_poll = False
119  _profiles: dict[int, str] = {}
120  _attr_options: list[str] = []
121  _attr_current_option: str | None = None
122 
123  def __init__(self, zone_id: str, hub: nobo) -> None:
124  """Initialize the week profile selector."""
125  self._id_id = zone_id
126  self._nobo_nobo = hub
127  self._attr_unique_id_attr_unique_id = f"{hub.hub_serial}:{zone_id}:profile"
128  self._attr_device_info_attr_device_info = DeviceInfo(
129  identifiers={(DOMAIN, f"{hub.hub_serial}:{zone_id}")},
130  name=hub.zones[zone_id][ATTR_NAME],
131  via_device=(DOMAIN, hub.hub_info[ATTR_SERIAL]),
132  suggested_area=hub.zones[zone_id][ATTR_NAME],
133  )
134 
135  async def async_added_to_hass(self) -> None:
136  """Register callback from hub."""
137  self._nobo_nobo.register_callback(self._after_update_after_update)
138 
139  async def async_will_remove_from_hass(self) -> None:
140  """Deregister callback from hub."""
141  self._nobo_nobo.deregister_callback(self._after_update_after_update)
142 
143  async def async_select_option(self, option: str) -> None:
144  """Set week profile."""
145  week_profile_id = [k for k, v in self._profiles_profiles.items() if v == option][0]
146  try:
147  await self._nobo_nobo.async_update_zone(
148  self._id_id, week_profile_id=week_profile_id
149  )
150  except Exception as exp:
151  raise HomeAssistantError from exp
152 
153  async def async_update(self) -> None:
154  """Fetch new state data for this zone."""
155  self._read_state_read_state()
156 
157  @callback
158  def _read_state(self) -> None:
159  self._profiles_profiles = {
160  profile["week_profile_id"]: profile["name"].replace("\xa0", " ")
161  for profile in self._nobo_nobo.week_profiles.values()
162  }
163  self._attr_options_attr_options = sorted(self._profiles_profiles.values())
164  self._attr_current_option_attr_current_option = self._profiles_profiles[
165  self._nobo_nobo.zones[self._id_id]["week_profile_id"]
166  ]
167 
168  @callback
169  def _after_update(self, hub) -> None:
170  self._read_state_read_state()
171  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, nobo hub, override_type)
Definition: select.py:65
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:30