Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Huawei LTE routers."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from datetime import timedelta
7 
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 
12 from . import Router
13 from .const import UPDATE_SIGNAL
14 
15 SCAN_INTERVAL = timedelta(seconds=10)
16 
17 
19  """Huawei LTE entity base class."""
20 
21  _available = True
22  _attr_has_entity_name = True
23  _attr_should_poll = False
24 
25  def __init__(self, router: Router) -> None:
26  """Initialize."""
27  self.routerrouter = router
28  self._unsub_handlers: list[Callable] = []
29 
30  @property
31  def _device_unique_id(self) -> str:
32  """Return unique ID for entity within a router."""
33  raise NotImplementedError
34 
35  @property
36  def unique_id(self) -> str:
37  """Return unique ID for entity."""
38  return f"{self.router.config_entry.unique_id}-{self._device_unique_id}"
39 
40  @property
41  def available(self) -> bool:
42  """Return whether the entity is available."""
43  return self._available_available
44 
45  async def async_update(self) -> None:
46  """Update state."""
47  raise NotImplementedError
48 
49  async def async_added_to_hass(self) -> None:
50  """Connect to update signals."""
51  self._unsub_handlers.append(
52  async_dispatcher_connect(self.hasshass, UPDATE_SIGNAL, self._async_maybe_update_async_maybe_update)
53  )
54 
55  async def _async_maybe_update(self, config_entry_unique_id: str) -> None:
56  """Update state if the update signal comes from our router."""
57  if config_entry_unique_id == self.routerrouter.config_entry.unique_id:
58  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
59 
60  async def async_will_remove_from_hass(self) -> None:
61  """Invoke unsubscription handlers."""
62  for unsub in self._unsub_handlers:
63  unsub()
64  self._unsub_handlers.clear()
65 
66 
68  """Base entity with device info."""
69 
70  @property
71  def device_info(self) -> DeviceInfo:
72  """Get info for matching with parent router."""
73  return DeviceInfo(
74  connections=self.routerrouter.device_connections,
75  identifiers=self.routerrouter.device_identifiers,
76  )
None _async_maybe_update(self, str config_entry_unique_id)
Definition: entity.py:55
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103