Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Connected Wi-Fi device scanners for TP-Link Omada access points."""
2 
3 import logging
4 
5 from tplink_omada_client.clients import OmadaWirelessClient
6 
7 from homeassistant.components.device_tracker import ScannerEntity
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 from homeassistant.helpers.update_coordinator import CoordinatorEntity
11 
12 from . import OmadaConfigEntry
13 from .config_flow import CONF_SITE
14 from .controller import OmadaClientsCoordinator
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
20  hass: HomeAssistant,
21  config_entry: OmadaConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up device trackers and scanners."""
25 
26  controller = config_entry.runtime_data
27 
28  site_id = config_entry.data[CONF_SITE]
29 
30  # Add all known WiFi devices as potentially tracked devices. They will only be
31  # tracked if the user enables the entity.
33  [
35  site_id, client.mac, client.name, controller.clients_coordinator
36  )
37  async for client in controller.omada_client.get_known_clients()
38  if isinstance(client, OmadaWirelessClient)
39  ]
40  )
41 
42 
44  CoordinatorEntity[OmadaClientsCoordinator], ScannerEntity
45 ):
46  """Entity for a client connected to the Omada network."""
47 
48  _client_details: OmadaWirelessClient | None = None
49 
50  def __init__(
51  self,
52  site_id: str,
53  client_id: str,
54  display_name: str,
55  coordinator: OmadaClientsCoordinator,
56  ) -> None:
57  """Initialize the scanner."""
58  super().__init__(coordinator)
59  self._site_id_site_id = site_id
60  self._client_id_client_id = client_id
61  self._attr_name_attr_name = display_name
62 
63  def _do_update(self) -> None:
64  self._client_details_client_details = self.coordinator.data.get(self._client_id_client_id)
65 
66  async def async_added_to_hass(self) -> None:
67  """When entity is added to hass."""
68  await super().async_added_to_hass()
69  self._do_update_do_update()
70 
71  @callback
72  def _handle_coordinator_update(self) -> None:
73  """Handle updated data from the coordinator."""
74  self._do_update_do_update()
75  self.async_write_ha_state()
76 
77  @property
78  def ip_address(self) -> str | None:
79  """Return the primary ip address of the device."""
80  return self._client_details_client_details.ip if self._client_details_client_details else None
81 
82  @property
83  def mac_address(self) -> str | None:
84  """Return the mac address of the device."""
85  return self._client_id_client_id
86 
87  @property
88  def hostname(self) -> str | None:
89  """Return hostname of the device."""
90  return self._client_details_client_details.host_name if self._client_details_client_details else None
91 
92  @property
93  def is_connected(self) -> bool:
94  """Return true if the device is connected to the network."""
95  return self._client_details_client_details.is_active if self._client_details_client_details else False
96 
97  @property
98  def unique_id(self) -> str | None:
99  """Return the unique id of the device."""
100  return f"scanner_{self._site_id}_{self._client_id}"