Add sensor platform to LD2410BLE (#85276)
* Add sensor platform to LD2410BLE - Add platform - Add moving target distance entity - Add static target distance entity - Add moving target energy entity - Add static target energy entity * Add detection distance entity * Align bluetooth-data-tools version * Generate sensors from description Also add state_class and unfactor description lambdas. * Optimise LD2410BLE collections Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
29337bc6eb
commit
4f63398941
4 changed files with 140 additions and 3 deletions
|
@ -685,6 +685,7 @@ omit =
|
|||
homeassistant/components/ld2410_ble/__init__.py
|
||||
homeassistant/components/ld2410_ble/binary_sensor.py
|
||||
homeassistant/components/ld2410_ble/coordinator.py
|
||||
homeassistant/components/ld2410_ble/sensor.py
|
||||
homeassistant/components/led_ble/__init__.py
|
||||
homeassistant/components/led_ble/light.py
|
||||
homeassistant/components/lg_netcast/media_player.py
|
||||
|
|
|
@ -16,7 +16,7 @@ from .const import DOMAIN
|
|||
from .coordinator import LD2410BLECoordinator
|
||||
from .models import LD2410BLEData
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR]
|
||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ from . import LD2410BLE, LD2410BLECoordinator
|
|||
from .const import DOMAIN
|
||||
from .models import LD2410BLEData
|
||||
|
||||
ENTITY_DESCRIPTIONS = [
|
||||
ENTITY_DESCRIPTIONS = (
|
||||
BinarySensorEntityDescription(
|
||||
key="is_moving",
|
||||
device_class=BinarySensorDeviceClass.MOTION,
|
||||
|
@ -30,7 +30,7 @@ ENTITY_DESCRIPTIONS = [
|
|||
has_entity_name=True,
|
||||
name="Occupancy",
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
|
136
homeassistant/components/ld2410_ble/sensor.py
Normal file
136
homeassistant/components/ld2410_ble/sensor.py
Normal file
|
@ -0,0 +1,136 @@
|
|||
"""LD2410 BLE integration sensor platform."""
|
||||
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfLength
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import LD2410BLE, LD2410BLECoordinator
|
||||
from .const import DOMAIN
|
||||
from .models import LD2410BLEData
|
||||
|
||||
MOVING_TARGET_DISTANCE_DESCRIPTION = SensorEntityDescription(
|
||||
key="moving_target_distance",
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_registry_visible_default=True,
|
||||
has_entity_name=True,
|
||||
name="Moving Target Distance",
|
||||
native_unit_of_measurement=UnitOfLength.CENTIMETERS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
)
|
||||
|
||||
STATIC_TARGET_DISTANCE_DESCRIPTION = SensorEntityDescription(
|
||||
key="static_target_distance",
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_registry_visible_default=True,
|
||||
has_entity_name=True,
|
||||
name="Static Target Distance",
|
||||
native_unit_of_measurement=UnitOfLength.CENTIMETERS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
)
|
||||
|
||||
DETECTION_DISTANCE_DESCRIPTION = SensorEntityDescription(
|
||||
key="detection_distance",
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_registry_visible_default=True,
|
||||
has_entity_name=True,
|
||||
name="Detection Distance",
|
||||
native_unit_of_measurement=UnitOfLength.CENTIMETERS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
)
|
||||
|
||||
MOVING_TARGET_ENERGY_DESCRIPTION = SensorEntityDescription(
|
||||
key="moving_target_energy",
|
||||
device_class=None,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_registry_visible_default=True,
|
||||
has_entity_name=True,
|
||||
name="Moving Target Energy",
|
||||
native_unit_of_measurement="Target Energy",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
)
|
||||
|
||||
STATIC_TARGET_ENERGY_DESCRIPTION = SensorEntityDescription(
|
||||
key="static_target_energy",
|
||||
device_class=None,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_registry_visible_default=True,
|
||||
has_entity_name=True,
|
||||
name="Static Target Energy",
|
||||
native_unit_of_measurement="Target Energy",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
)
|
||||
|
||||
SENSOR_DESCRIPTIONS = (
|
||||
MOVING_TARGET_DISTANCE_DESCRIPTION,
|
||||
STATIC_TARGET_DISTANCE_DESCRIPTION,
|
||||
MOVING_TARGET_ENERGY_DESCRIPTION,
|
||||
STATIC_TARGET_ENERGY_DESCRIPTION,
|
||||
DETECTION_DISTANCE_DESCRIPTION,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the platform for LD2410BLE."""
|
||||
data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities(
|
||||
LD2410BLESensor(
|
||||
data.coordinator,
|
||||
data.device,
|
||||
entry.title,
|
||||
description,
|
||||
)
|
||||
for description in SENSOR_DESCRIPTIONS
|
||||
)
|
||||
|
||||
|
||||
class LD2410BLESensor(CoordinatorEntity, SensorEntity):
|
||||
"""Moving/static target distance sensor for LD2410BLE."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: LD2410BLECoordinator,
|
||||
device: LD2410BLE,
|
||||
name: str,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._coordinator = coordinator
|
||||
self._device = device
|
||||
self._key = description.key
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device.address}_{self._key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
name=name,
|
||||
connections={(dr.CONNECTION_BLUETOOTH, device.address)},
|
||||
)
|
||||
self._attr_native_value = getattr(self._device, self._key)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._attr_native_value = getattr(self._device, self._key)
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Unavailable if coordinator isn't connected."""
|
||||
return self._coordinator.connected and super().available
|
Loading…
Add table
Reference in a new issue