Michael eaa32146a6
Add sensor platform to Proximity (#101497)
* add sensor platform

* transl. of distance already covered by dev.class

* add untested files to .coveragerc

* add missing state translations

* remove translation key for distance sensor

* proximity entity do not use HA number system

* fix

* extend tests

* make const final to be usable as key for TypedDict

* remove proximity from .coveragerc

* replace typeddict by simple dict definition

* make black happy

* rework to create proximity sensor for each tracked entity and always recalculate all entites

* apply review comments

* move direction of travel calc out of the loop

* make direction of travel an enum sensor

* remove unique_id from sensors

* don't set distance=0 when in monitored zone

* set None when direction is unknown

* keep distance 0 in case arrived for legacy entity

* exclude from nearest when in ignored zone

* keep distance=0 when arrived

* use description name for entity name

* remove uneeded typing

* uses consistent variable name

* fix debug messages

* use entity_id as loop var

* rename device_state to tracked_entity_state

* correct MRO for sensor entity classes
2024-01-23 10:56:02 +01:00

139 lines
4.2 KiB
Python

"""Support for Proximity sensors."""
from __future__ import annotations
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import CONF_NAME, UnitOfLength
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ATTR_DIR_OF_TRAVEL, ATTR_DIST_TO, ATTR_NEAREST, DOMAIN
from .coordinator import ProximityDataUpdateCoordinator
SENSORS_PER_ENTITY: list[SensorEntityDescription] = [
SensorEntityDescription(
key=ATTR_DIST_TO,
name="Distance",
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement=UnitOfLength.METERS,
),
SensorEntityDescription(
key=ATTR_DIR_OF_TRAVEL,
name="Direction of travel",
translation_key=ATTR_DIR_OF_TRAVEL,
icon="mdi:compass-outline",
device_class=SensorDeviceClass.ENUM,
options=[
"arrived",
"away_from",
"stationary",
"towards",
],
),
]
SENSORS_PER_PROXIMITY: list[SensorEntityDescription] = [
SensorEntityDescription(
key=ATTR_NEAREST,
name="Nearest",
translation_key=ATTR_NEAREST,
icon="mdi:near-me",
),
]
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Proximity sensor platform."""
if discovery_info is None:
return
coordinator: ProximityDataUpdateCoordinator = hass.data[DOMAIN][
discovery_info[CONF_NAME]
]
entities: list[ProximitySensor | ProximityTrackedEntitySensor] = [
ProximitySensor(description, coordinator)
for description in SENSORS_PER_PROXIMITY
]
entities += [
ProximityTrackedEntitySensor(description, coordinator, tracked_entity_id)
for description in SENSORS_PER_ENTITY
for tracked_entity_id in coordinator.tracked_entities
]
async_add_entities(entities)
class ProximitySensor(CoordinatorEntity[ProximityDataUpdateCoordinator], SensorEntity):
"""Represents a Proximity sensor."""
_attr_has_entity_name = True
def __init__(
self,
description: SensorEntityDescription,
coordinator: ProximityDataUpdateCoordinator,
) -> None:
"""Initialize the proximity."""
super().__init__(coordinator)
self.entity_description = description
# entity name will be removed as soon as we have a config entry
# and can follow the entity naming guidelines
self._attr_name = f"{coordinator.friendly_name} {description.name}"
@property
def native_value(self) -> str | float | None:
"""Return native sensor value."""
if (
value := self.coordinator.data.proximity[self.entity_description.key]
) == "not set":
return None
return value
class ProximityTrackedEntitySensor(
CoordinatorEntity[ProximityDataUpdateCoordinator], SensorEntity
):
"""Represents a Proximity tracked entity sensor."""
_attr_has_entity_name = True
def __init__(
self,
description: SensorEntityDescription,
coordinator: ProximityDataUpdateCoordinator,
tracked_entity_id: str,
) -> None:
"""Initialize the proximity."""
super().__init__(coordinator)
self.entity_description = description
self.tracked_entity_id = tracked_entity_id
# entity name will be removed as soon as we have a config entry
# and can follow the entity naming guidelines
self._attr_name = (
f"{coordinator.friendly_name} {tracked_entity_id} {description.name}"
)
@property
def native_value(self) -> str | float | None:
"""Return native sensor value."""
if (data := self.coordinator.data.entities.get(self.tracked_entity_id)) is None:
return None
return data.get(self.entity_description.key)