Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """LD2410 BLE integration sensor platform."""
2 
4  SensorDeviceClass,
5  SensorEntity,
6  SensorEntityDescription,
7  SensorStateClass,
8 )
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import EntityCategory, UnitOfLength
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers import device_registry as dr
13 from homeassistant.helpers.device_registry import DeviceInfo
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.update_coordinator import CoordinatorEntity
16 
17 from . import LD2410BLE, LD2410BLECoordinator
18 from .const import DOMAIN
19 from .models import LD2410BLEData
20 
21 MOVING_TARGET_DISTANCE_DESCRIPTION = SensorEntityDescription(
22  key="moving_target_distance",
23  translation_key="moving_target_distance",
24  device_class=SensorDeviceClass.DISTANCE,
25  entity_registry_enabled_default=False,
26  entity_registry_visible_default=True,
27  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
28  state_class=SensorStateClass.MEASUREMENT,
29 )
30 
31 STATIC_TARGET_DISTANCE_DESCRIPTION = SensorEntityDescription(
32  key="static_target_distance",
33  translation_key="static_target_distance",
34  device_class=SensorDeviceClass.DISTANCE,
35  entity_registry_enabled_default=False,
36  entity_registry_visible_default=True,
37  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
38  state_class=SensorStateClass.MEASUREMENT,
39 )
40 
41 DETECTION_DISTANCE_DESCRIPTION = SensorEntityDescription(
42  key="detection_distance",
43  translation_key="detection_distance",
44  device_class=SensorDeviceClass.DISTANCE,
45  entity_registry_enabled_default=False,
46  entity_registry_visible_default=True,
47  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
48  state_class=SensorStateClass.MEASUREMENT,
49 )
50 
51 MOVING_TARGET_ENERGY_DESCRIPTION = SensorEntityDescription(
52  key="moving_target_energy",
53  translation_key="moving_target_energy",
54  device_class=None,
55  entity_registry_enabled_default=False,
56  entity_registry_visible_default=True,
57  native_unit_of_measurement="Target Energy",
58  state_class=SensorStateClass.MEASUREMENT,
59 )
60 
61 STATIC_TARGET_ENERGY_DESCRIPTION = SensorEntityDescription(
62  key="static_target_energy",
63  translation_key="static_target_energy",
64  device_class=None,
65  entity_registry_enabled_default=False,
66  entity_registry_visible_default=True,
67  native_unit_of_measurement="Target Energy",
68  state_class=SensorStateClass.MEASUREMENT,
69 )
70 
71 MAX_MOTION_GATES_DESCRIPTION = SensorEntityDescription(
72  key="max_motion_gates",
73  translation_key="max_motion_gates",
74  entity_category=EntityCategory.DIAGNOSTIC,
75  entity_registry_enabled_default=False,
76  native_unit_of_measurement="Gates",
77 )
78 
79 MAX_STATIC_GATES_DESCRIPTION = SensorEntityDescription(
80  key="max_static_gates",
81  translation_key="max_static_gates",
82  entity_category=EntityCategory.DIAGNOSTIC,
83  entity_registry_enabled_default=False,
84  native_unit_of_measurement="Gates",
85 )
86 
87 MOTION_ENERGY_GATES = [
89  key=f"motion_energy_gate_{i}",
90  translation_key=f"motion_energy_gate_{i}",
91  entity_category=EntityCategory.DIAGNOSTIC,
92  entity_registry_enabled_default=False,
93  native_unit_of_measurement="Target Energy",
94  )
95  for i in range(9)
96 ]
97 
98 STATIC_ENERGY_GATES = [
100  key=f"static_energy_gate_{i}",
101  translation_key=f"static_energy_gate_{i}",
102  entity_category=EntityCategory.DIAGNOSTIC,
103  entity_registry_enabled_default=False,
104  native_unit_of_measurement="Target Energy",
105  )
106  for i in range(9)
107 ]
108 
109 SENSOR_DESCRIPTIONS = [
110  MOVING_TARGET_DISTANCE_DESCRIPTION,
111  STATIC_TARGET_DISTANCE_DESCRIPTION,
112  MOVING_TARGET_ENERGY_DESCRIPTION,
113  STATIC_TARGET_ENERGY_DESCRIPTION,
114  DETECTION_DISTANCE_DESCRIPTION,
115  MAX_MOTION_GATES_DESCRIPTION,
116  MAX_STATIC_GATES_DESCRIPTION,
117  *MOTION_ENERGY_GATES,
118  *STATIC_ENERGY_GATES,
119 ]
120 
121 
123  hass: HomeAssistant,
124  entry: ConfigEntry,
125  async_add_entities: AddEntitiesCallback,
126 ) -> None:
127  """Set up the platform for LD2410BLE."""
128  data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
131  data.coordinator,
132  data.device,
133  entry.title,
134  description,
135  )
136  for description in SENSOR_DESCRIPTIONS
137  )
138 
139 
140 class LD2410BLESensor(CoordinatorEntity[LD2410BLECoordinator], SensorEntity):
141  """Generic sensor for LD2410BLE."""
142 
143  _attr_has_entity_name = True
144 
145  def __init__(
146  self,
147  coordinator: LD2410BLECoordinator,
148  device: LD2410BLE,
149  name: str,
150  description: SensorEntityDescription,
151  ) -> None:
152  """Initialize the sensor."""
153  super().__init__(coordinator)
154  self._coordinator_coordinator = coordinator
155  self._device_device = device
156  self._key_key = description.key
157  self.entity_descriptionentity_description = description
158  self._attr_unique_id_attr_unique_id = f"{device.address}_{self._key}"
159  self._attr_device_info_attr_device_info = DeviceInfo(
160  name=name,
161  connections={(dr.CONNECTION_BLUETOOTH, device.address)},
162  )
163  self._attr_native_value_attr_native_value = getattr(self._device_device, self._key_key)
164 
165  @callback
166  def _handle_coordinator_update(self) -> None:
167  """Handle updated data from the coordinator."""
168  self._attr_native_value_attr_native_value = getattr(self._device_device, self._key_key)
169  self.async_write_ha_stateasync_write_ha_state()
170 
171  @property
172  def available(self) -> bool:
173  """Unavailable if coordinator isn't connected."""
174  return self._coordinator_coordinator.connected and super().available
None __init__(self, LD2410BLECoordinator coordinator, LD2410BLE device, str name, SensorEntityDescription description)
Definition: sensor.py:151
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:126