Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The nexia integration base entity."""
2 
3 from typing import TYPE_CHECKING
4 
5 from nexia.thermostat import NexiaThermostat
6 from nexia.zone import NexiaThermostatZone
7 
8 from homeassistant.const import (
9  ATTR_IDENTIFIERS,
10  ATTR_NAME,
11  ATTR_SUGGESTED_AREA,
12  ATTR_VIA_DEVICE,
13 )
14 from homeassistant.helpers.device_registry import DeviceInfo
16  async_dispatcher_connect,
17  async_dispatcher_send,
18 )
19 from homeassistant.helpers.update_coordinator import CoordinatorEntity
20 
21 from .const import (
22  ATTRIBUTION,
23  DOMAIN,
24  MANUFACTURER,
25  SIGNAL_THERMOSTAT_UPDATE,
26  SIGNAL_ZONE_UPDATE,
27 )
28 from .coordinator import NexiaDataUpdateCoordinator
29 
30 
31 class NexiaEntity(CoordinatorEntity[NexiaDataUpdateCoordinator]):
32  """Base class for nexia entities."""
33 
34  _attr_attribution = ATTRIBUTION
35 
36  def __init__(self, coordinator: NexiaDataUpdateCoordinator, unique_id: str) -> None:
37  """Initialize the entity."""
38  super().__init__(coordinator)
39  self._attr_unique_id_attr_unique_id = unique_id
40 
41 
43  """Base class for nexia devices attached to a thermostat."""
44 
45  _attr_has_entity_name = True
46 
47  def __init__(
48  self,
49  coordinator: NexiaDataUpdateCoordinator,
50  thermostat: NexiaThermostat,
51  unique_id: str,
52  ) -> None:
53  """Initialize the entity."""
54  super().__init__(coordinator, unique_id)
55  self._thermostat_thermostat = thermostat
56  thermostat_id = thermostat.thermostat_id
57  self._attr_device_info_attr_device_info = DeviceInfo(
58  configuration_url=self.coordinator.nexia_home.root_url,
59  identifiers={(DOMAIN, thermostat_id)},
60  manufacturer=MANUFACTURER,
61  model=thermostat.get_model(),
62  name=thermostat.get_name(),
63  sw_version=thermostat.get_firmware(),
64  )
65  self._thermostat_signal_thermostat_signal = f"{SIGNAL_THERMOSTAT_UPDATE}-{thermostat_id}"
66 
67  async def async_added_to_hass(self) -> None:
68  """Listen for signals for services."""
69  await super().async_added_to_hass()
70  self.async_on_remove(
72  self.hasshass,
73  self._thermostat_signal_thermostat_signal,
74  self.async_write_ha_state,
75  )
76  )
77 
78  def _signal_thermostat_update(self) -> None:
79  """Signal a thermostat update.
80 
81  Whenever the underlying library does an action against
82  a thermostat, the data for the thermostat and all
83  connected zone is updated.
84 
85  Update all the zones on the thermostat.
86  """
87  async_dispatcher_send(self.hasshass, self._thermostat_signal_thermostat_signal)
88 
89  @property
90  def available(self) -> bool:
91  """Return True if thermostat is available and data is available."""
92  return super().available and self._thermostat_thermostat.is_online
93 
94 
96  """Base class for nexia devices attached to a thermostat."""
97 
98  def __init__(
99  self,
100  coordinator: NexiaDataUpdateCoordinator,
101  zone: NexiaThermostatZone,
102  unique_id: str,
103  ) -> None:
104  """Initialize the entity."""
105  super().__init__(coordinator, zone.thermostat, unique_id)
106  self._zone_zone = zone
107  zone_name = self._zone_zone.get_name()
108  if TYPE_CHECKING:
109  assert self._attr_device_info_attr_device_info is not None
110  self._attr_device_info_attr_device_info |= {
111  ATTR_IDENTIFIERS: {(DOMAIN, zone.zone_id)},
112  ATTR_NAME: zone_name,
113  ATTR_SUGGESTED_AREA: zone_name,
114  ATTR_VIA_DEVICE: (DOMAIN, zone.thermostat.thermostat_id),
115  }
116  self._zone_signal_zone_signal = f"{SIGNAL_ZONE_UPDATE}-{zone.zone_id}"
117 
118  async def async_added_to_hass(self) -> None:
119  """Listen for signals for services."""
120  await super().async_added_to_hass()
121  self.async_on_remove(
123  self.hasshass,
124  self._zone_signal_zone_signal,
125  self.async_write_ha_state,
126  )
127  )
128 
129  def _signal_zone_update(self) -> None:
130  """Signal a zone update.
131 
132  Whenever the underlying library does an action against
133  a zone, the data for the zone is updated.
134 
135  Update a single zone.
136  """
137  async_dispatcher_send(self.hasshass, self._zone_signal_zone_signal)
None __init__(self, NexiaDataUpdateCoordinator coordinator, str unique_id)
Definition: entity.py:36
None __init__(self, NexiaDataUpdateCoordinator coordinator, NexiaThermostat thermostat, str unique_id)
Definition: entity.py:52
None __init__(self, NexiaDataUpdateCoordinator coordinator, NexiaThermostatZone zone, str unique_id)
Definition: entity.py:103
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193