Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for all eQ-3 entities."""
2 
3 from homeassistant.core import callback
5  CONNECTION_BLUETOOTH,
6  DeviceInfo,
7  format_mac,
8 )
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 from homeassistant.util import slugify
12 
13 from . import Eq3ConfigEntry
14 from .const import (
15  DEVICE_MODEL,
16  MANUFACTURER,
17  SIGNAL_THERMOSTAT_CONNECTED,
18  SIGNAL_THERMOSTAT_DISCONNECTED,
19 )
20 
21 
23  """Base class for all eQ-3 entities."""
24 
25  _attr_has_entity_name = True
26 
27  def __init__(
28  self,
29  entry: Eq3ConfigEntry,
30  unique_id_key: str | None = None,
31  ) -> None:
32  """Initialize the eq3 entity."""
33 
34  self._eq3_config_eq3_config = entry.runtime_data.eq3_config
35  self._thermostat_thermostat = entry.runtime_data.thermostat
36  self._attr_device_info_attr_device_info = DeviceInfo(
37  name=slugify(self._eq3_config_eq3_config.mac_address),
38  manufacturer=MANUFACTURER,
39  model=DEVICE_MODEL,
40  connections={(CONNECTION_BLUETOOTH, self._eq3_config_eq3_config.mac_address)},
41  )
42  suffix = f"_{unique_id_key}" if unique_id_key else ""
43  self._attr_unique_id_attr_unique_id = f"{format_mac(self._eq3_config.mac_address)}{suffix}"
44 
45  async def async_added_to_hass(self) -> None:
46  """Run when entity about to be added to hass."""
47 
48  self._thermostat_thermostat.register_update_callback(self._async_on_updated_async_on_updated)
49 
50  self.async_on_removeasync_on_remove(
52  self.hasshass,
53  f"{SIGNAL_THERMOSTAT_DISCONNECTED}_{self._eq3_config.mac_address}",
54  self._async_on_disconnected_async_on_disconnected,
55  )
56  )
57  self.async_on_removeasync_on_remove(
59  self.hasshass,
60  f"{SIGNAL_THERMOSTAT_CONNECTED}_{self._eq3_config.mac_address}",
61  self._async_on_connected_async_on_connected,
62  )
63  )
64 
65  async def async_will_remove_from_hass(self) -> None:
66  """Run when entity will be removed from hass."""
67 
68  self._thermostat_thermostat.unregister_update_callback(self._async_on_updated_async_on_updated)
69 
70  def _async_on_updated(self) -> None:
71  """Handle updated data from the thermostat."""
72 
73  self.async_write_ha_stateasync_write_ha_state()
74 
75  @callback
76  def _async_on_disconnected(self) -> None:
77  """Handle disconnection from the thermostat."""
78 
79  self._attr_available_attr_available = False
80  self.async_write_ha_stateasync_write_ha_state()
81 
82  @callback
83  def _async_on_connected(self) -> None:
84  """Handle connection to the thermostat."""
85 
86  self._attr_available_attr_available = True
87  self.async_write_ha_stateasync_write_ha_state()
88 
89  @property
90  def available(self) -> bool:
91  """Whether the entity is available."""
92 
93  return self._thermostat_thermostat.status is not None and self._attr_available_attr_available
None __init__(self, Eq3ConfigEntry entry, str|None unique_id_key=None)
Definition: entity.py:31
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103