Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for scanning a network with nmap."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.device_tracker import ScannerEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.dispatcher import async_dispatcher_connect
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import NmapDevice, NmapDeviceScanner, short_hostname, signal_device_update
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
22 ) -> None:
23  """Set up device tracker for Nmap Tracker component."""
24  nmap_tracker = hass.data[DOMAIN][entry.entry_id]
25 
26  @callback
27  def device_new(mac_address):
28  """Signal a new device."""
29  async_add_entities([NmapTrackerEntity(nmap_tracker, mac_address, True)])
30 
31  @callback
32  def device_missing(mac_address):
33  """Signal a missing device."""
34  async_add_entities([NmapTrackerEntity(nmap_tracker, mac_address, False)])
35 
36  entry.async_on_unload(
37  async_dispatcher_connect(hass, nmap_tracker.signal_device_new, device_new)
38  )
39  entry.async_on_unload(
41  hass, nmap_tracker.signal_device_missing, device_missing
42  )
43  )
44 
45 
46 class NmapTrackerEntity(ScannerEntity):
47  """An Nmap Tracker entity."""
48 
49  _attr_should_poll = False
50  _attr_translation_key = "device_tracker"
51 
52  def __init__(
53  self, nmap_tracker: NmapDeviceScanner, mac_address: str, active: bool
54  ) -> None:
55  """Initialize an nmap tracker entity."""
56  self._mac_address_mac_address = mac_address
57  self._nmap_tracker_nmap_tracker = nmap_tracker
58  self._tracked_tracked = self._nmap_tracker_nmap_tracker.devices.tracked
59  self._active_active = active
60 
61  @property
62  def _device(self) -> NmapDevice:
63  """Get latest device state."""
64  return self._tracked_tracked[self._mac_address_mac_address]
65 
66  @property
67  def is_connected(self) -> bool:
68  """Return device status."""
69  return self._active_active
70 
71  @property
72  def name(self) -> str:
73  """Return device name."""
74  return self._device_device.name
75 
76  @property
77  def unique_id(self) -> str:
78  """Return device unique id."""
79  return self._mac_address_mac_address
80 
81  @property
82  def ip_address(self) -> str:
83  """Return the primary ip address of the device."""
84  return self._device_device.ipv4
85 
86  @property
87  def mac_address(self) -> str:
88  """Return the mac address of the device."""
89  return self._mac_address_mac_address
90 
91  @property
92  def hostname(self) -> str | None:
93  """Return hostname of the device."""
94  if not self._device_device.hostname:
95  return None
96  return short_hostname(self._device_device.hostname)
97 
98  @callback
99  def async_process_update(self, online: bool) -> None:
100  """Update device."""
101  self._active_active = online
102 
103  @property
104  def extra_state_attributes(self) -> dict[str, Any]:
105  """Return the attributes."""
106  return {
107  "last_time_reachable": self._device_device.last_update.isoformat(
108  timespec="seconds"
109  ),
110  "reason": self._device_device.reason,
111  }
112 
113  @callback
114  def async_on_demand_update(self, online: bool) -> None:
115  """Update state."""
116  self.async_process_updateasync_process_update(online)
117  self.async_write_ha_state()
118 
119  async def async_added_to_hass(self) -> None:
120  """Register state update callback."""
121  self.async_on_remove(
123  self.hass,
124  signal_device_update(self._mac_address_mac_address),
125  self.async_on_demand_updateasync_on_demand_update,
126  )
127  )
None __init__(self, NmapDeviceScanner nmap_tracker, str mac_address, bool active)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
str signal_device_update(mac_address)
Definition: __init__.py:128
str short_hostname(str hostname)
Definition: __init__.py:45
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103