1 """Support for Proximity sensors."""
3 from __future__
import annotations
5 from typing
import NamedTuple
10 SensorEntityDescription,
23 ATTR_NEAREST_DIR_OF_TRAVEL,
27 from .coordinator
import ProximityConfigEntry, ProximityDataUpdateCoordinator
29 DIRECTIONS = [
"arrived",
"away_from",
"stationary",
"towards"]
31 SENSORS_PER_ENTITY: list[SensorEntityDescription] = [
34 translation_key=ATTR_DIST_TO,
35 device_class=SensorDeviceClass.DISTANCE,
36 native_unit_of_measurement=UnitOfLength.METERS,
39 key=ATTR_DIR_OF_TRAVEL,
40 translation_key=ATTR_DIR_OF_TRAVEL,
41 device_class=SensorDeviceClass.ENUM,
46 SENSORS_PER_PROXIMITY: list[SensorEntityDescription] = [
49 translation_key=ATTR_NEAREST,
53 translation_key=ATTR_NEAREST_DIST_TO,
54 device_class=SensorDeviceClass.DISTANCE,
55 native_unit_of_measurement=UnitOfLength.METERS,
58 key=ATTR_DIR_OF_TRAVEL,
59 translation_key=ATTR_NEAREST_DIR_OF_TRAVEL,
60 device_class=SensorDeviceClass.ENUM,
67 """Descriptor of a tracked entity."""
74 def _device_info(coordinator: ProximityDataUpdateCoordinator) -> DeviceInfo:
76 identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
77 name=coordinator.config_entry.title,
78 entry_type=DeviceEntryType.SERVICE,
84 entry: ProximityConfigEntry,
85 async_add_entities: AddEntitiesCallback,
87 """Set up the proximity sensors."""
89 coordinator = entry.runtime_data
91 entities: list[ProximitySensor | ProximityTrackedEntitySensor] = [
93 for description
in SENSORS_PER_PROXIMITY
96 tracked_entity_descriptors: list[TrackedEntityDescriptor] = []
98 entity_reg = er.async_get(hass)
99 for tracked_entity_id
in coordinator.tracked_entities:
100 tracked_entity_object_id = tracked_entity_id.split(
".")[-1]
101 if (entity_entry := entity_reg.async_get(tracked_entity_id))
is not None:
102 tracked_entity_descriptors.append(
107 or entity_entry.original_name
108 or tracked_entity_object_id,
112 tracked_entity_descriptors.append(
116 tracked_entity_object_id,
122 description, coordinator, tracked_entity_descriptor
124 for description
in SENSORS_PER_ENTITY
125 for tracked_entity_descriptor
in tracked_entity_descriptors
132 """Represents a Proximity sensor."""
134 _attr_has_entity_name =
True
138 description: SensorEntityDescription,
139 coordinator: ProximityDataUpdateCoordinator,
141 """Initialize the proximity."""
146 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.config_entry.entry_id}_{description.key}"
151 """Return native sensor value."""
153 value := self.coordinator.data.proximity[self.
entity_descriptionentity_description.key]
160 CoordinatorEntity[ProximityDataUpdateCoordinator], SensorEntity
162 """Represents a Proximity tracked entity sensor."""
164 _attr_has_entity_name =
True
168 description: SensorEntityDescription,
169 coordinator: ProximityDataUpdateCoordinator,
170 tracked_entity_descriptor: TrackedEntityDescriptor,
172 """Initialize the proximity."""
178 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.config_entry.entry_id}_{tracked_entity_descriptor.identifier}_{description.key}"
181 "tracked_entity": tracked_entity_descriptor.name
185 """Register entity mapping."""
192 def data(self) -> dict[str, str | int | None]:
193 """Get data from coordinator."""
198 """Return if entity is available."""
206 """Return native sensor value."""
None async_add_entity_mapping(self, str tracked_entity_id, str entity_id)
None __init__(self, SensorEntityDescription description, ProximityDataUpdateCoordinator coordinator)
str|float|None native_value(self)
str|float|None native_value(self)
None async_added_to_hass(self)
_attr_translation_placeholders
dict[str, str|int|None] data(self)
None __init__(self, SensorEntityDescription description, ProximityDataUpdateCoordinator coordinator, TrackedEntityDescriptor tracked_entity_descriptor)
web.Response get(self, web.Request request, str config_key)
DeviceInfo _device_info(ProximityDataUpdateCoordinator coordinator)
None async_setup_entry(HomeAssistant hass, ProximityConfigEntry entry, AddEntitiesCallback async_add_entities)