Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for iBeacon device sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from ibeacon_ble import iBeaconAdvertisement
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfLength
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import IBeaconConfigEntry
22 from .const import SIGNAL_IBEACON_DEVICE_NEW
23 from .coordinator import IBeaconCoordinator
24 from .entity import IBeaconEntity
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Describes iBeacon sensor entity."""
30 
31  value_fn: Callable[[iBeaconAdvertisement], str | int | None]
32 
33 
34 SENSOR_DESCRIPTIONS = (
36  key="rssi",
37  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
38  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
39  entity_registry_enabled_default=False,
40  value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.rssi,
41  state_class=SensorStateClass.MEASUREMENT,
42  ),
44  key="power",
45  translation_key="power",
46  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
47  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
48  entity_registry_enabled_default=False,
49  value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.power,
50  state_class=SensorStateClass.MEASUREMENT,
51  ),
53  key="estimated_distance",
54  translation_key="estimated_distance",
55  native_unit_of_measurement=UnitOfLength.METERS,
56  value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.distance,
57  state_class=SensorStateClass.MEASUREMENT,
58  device_class=SensorDeviceClass.DISTANCE,
59  ),
61  key="vendor",
62  translation_key="vendor",
63  entity_registry_enabled_default=False,
64  value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.vendor,
65  ),
66 )
67 
68 
70  hass: HomeAssistant,
71  entry: IBeaconConfigEntry,
72  async_add_entities: AddEntitiesCallback,
73 ) -> None:
74  """Set up sensors for iBeacon Tracker component."""
75  coordinator = entry.runtime_data
76 
77  @callback
78  def _async_device_new(
79  unique_id: str,
80  identifier: str,
81  ibeacon_advertisement: iBeaconAdvertisement,
82  ) -> None:
83  """Signal a new device."""
86  coordinator,
87  description,
88  identifier,
89  unique_id,
90  ibeacon_advertisement,
91  )
92  for description in SENSOR_DESCRIPTIONS
93  )
94 
95  entry.async_on_unload(
96  async_dispatcher_connect(hass, SIGNAL_IBEACON_DEVICE_NEW, _async_device_new)
97  )
98 
99 
101  """An iBeacon sensor entity."""
102 
103  entity_description: IBeaconSensorEntityDescription
104 
105  def __init__(
106  self,
107  coordinator: IBeaconCoordinator,
108  description: IBeaconSensorEntityDescription,
109  identifier: str,
110  device_unique_id: str,
111  ibeacon_advertisement: iBeaconAdvertisement,
112  ) -> None:
113  """Initialize an iBeacon sensor entity."""
114  super().__init__(
115  coordinator, identifier, device_unique_id, ibeacon_advertisement
116  )
117  self._attr_unique_id_attr_unique_id = f"{device_unique_id}_{description.key}"
118  self.entity_descriptionentity_description = description
119 
120  @callback
122  self,
123  ibeacon_advertisement: iBeaconAdvertisement,
124  ) -> None:
125  """Update state."""
126  self._attr_available_attr_available = True
127  self._ibeacon_advertisement_ibeacon_advertisement_ibeacon_advertisement = ibeacon_advertisement
128  self.async_write_ha_stateasync_write_ha_state()
129 
130  @callback
131  def _async_unavailable(self) -> None:
132  """Update state."""
133  self._attr_available_attr_available = False
134  self.async_write_ha_stateasync_write_ha_state()
135 
136  @property
137  def native_value(self) -> str | int | None:
138  """Return the state of the sensor."""
139  return self.entity_descriptionentity_description.value_fn(self._ibeacon_advertisement_ibeacon_advertisement_ibeacon_advertisement)
None __init__(self, IBeaconCoordinator coordinator, IBeaconSensorEntityDescription description, str identifier, str device_unique_id, iBeaconAdvertisement ibeacon_advertisement)
Definition: sensor.py:112
None _async_seen(self, iBeaconAdvertisement ibeacon_advertisement)
Definition: sensor.py:124
None async_setup_entry(HomeAssistant hass, IBeaconConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:73
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103