Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for the ZHA platform."""
2 
3 from __future__ import annotations
4 
5 import functools
6 
7 from homeassistant.components.device_tracker import ScannerEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .entity import ZHAEntity
16 from .helpers import (
17  SIGNAL_ADD_ENTITIES,
18  async_add_entities as zha_async_add_entities,
19  get_zha_data,
20 )
21 
22 
24  hass: HomeAssistant,
25  config_entry: ConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the Zigbee Home Automation device tracker from config entry."""
29  zha_data = get_zha_data(hass)
30  entities_to_create = zha_data.platforms[Platform.DEVICE_TRACKER]
31 
33  hass,
34  SIGNAL_ADD_ENTITIES,
35  functools.partial(
36  zha_async_add_entities,
37  async_add_entities,
38  ZHADeviceScannerEntity,
39  entities_to_create,
40  ),
41  )
42  config_entry.async_on_unload(unsub)
43 
44 
45 class ZHADeviceScannerEntity(ScannerEntity, ZHAEntity):
46  """Represent a tracked device."""
47 
48  _attr_should_poll = True # BaseZhaEntity defaults to False
49  _attr_name: str = "Device scanner"
50 
51  @property
52  def is_connected(self) -> bool:
53  """Return true if the device is connected to the network."""
54  return self.entity_data.entity.is_connected
55 
56  @property
57  def battery_level(self) -> int | None:
58  """Return the battery level of the device.
59 
60  Percentage from 0-100.
61  """
62  return self.entity_data.entity.battery_level
63 
64  @property # type: ignore[explicit-override, misc]
65  def device_info(self) -> DeviceInfo:
66  """Return device info."""
67  # We opt ZHA device tracker back into overriding this method because
68  # it doesn't track IP-based devices.
69  return ZHAEntity.device_info.__get__(self)
70 
71  @property
72  def unique_id(self) -> str:
73  """Return unique ID."""
74  # Call Super because ScannerEntity overrode it.
75  return ZHAEntity.unique_id.__get__(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103