Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Tracking for bluetooth low energy devices."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 import binascii
7 
8 from homeassistant.components import bluetooth
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import callback
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity import Entity
13 
14 from .const import DOMAIN
15 from .coordinator import async_get_coordinator, async_last_service_info
16 
17 
19  """Base Private Bluetooth Entity."""
20 
21  _attr_should_poll = False
22  _attr_has_entity_name = True
23 
24  def __init__(self, config_entry: ConfigEntry) -> None:
25  """Set up a new BleScanner entity."""
26  irk = config_entry.data["irk"]
27 
28  if self.translation_keytranslation_key:
29  self._attr_unique_id_attr_unique_id = f"{irk}_{self.translation_key}"
30  else:
31  self._attr_unique_id_attr_unique_id = irk
32 
33  self._attr_device_info_attr_device_info = DeviceInfo(
34  name=f"Private BLE Device {irk[:6]}",
35  identifiers={(DOMAIN, irk)},
36  )
37 
38  self._entry_entry = config_entry
39  self._irk_irk = binascii.unhexlify(irk)
40  self._last_info: bluetooth.BluetoothServiceInfoBleak | None = None
41 
42  async def async_added_to_hass(self) -> None:
43  """Configure entity when it is added to Home Assistant."""
44  coordinator = async_get_coordinator(self.hasshass)
45  self.async_on_removeasync_on_remove(
46  coordinator.async_track_service_info(
47  self._async_track_service_info_async_track_service_info, self._irk_irk
48  )
49  )
50  self.async_on_removeasync_on_remove(
51  coordinator.async_track_unavailable(
52  self._async_track_unavailable_async_track_unavailable, self._irk_irk
53  )
54  )
55 
56  if service_info := async_last_service_info(self.hasshass, self._irk_irk):
57  self._async_track_service_info_async_track_service_info(
58  service_info, bluetooth.BluetoothChange.ADVERTISEMENT
59  )
60 
61  @abstractmethod
62  @callback
64  self, service_info: bluetooth.BluetoothServiceInfoBleak
65  ) -> None:
66  """Respond when the bluetooth device being tracked is no longer visible."""
67 
68  @abstractmethod
69  @callback
71  self,
72  service_info: bluetooth.BluetoothServiceInfoBleak,
73  change: bluetooth.BluetoothChange,
74  ) -> None:
75  """Respond when the bluetooth device being tracked broadcasted updated information."""
None _async_track_service_info(self, bluetooth.BluetoothServiceInfoBleak service_info, bluetooth.BluetoothChange change)
Definition: entity.py:74
None _async_track_unavailable(self, bluetooth.BluetoothServiceInfoBleak service_info)
Definition: entity.py:65
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:80
PrivateDevicesCoordinator async_get_coordinator(HomeAssistant hass)
Definition: coordinator.py:235