Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sensor platform to LD2410BLE #85276

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/ld2410_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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__)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/ld2410_ble/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .const import DOMAIN
from .models import LD2410BLEData

ENTITY_DESCRIPTIONS = [
ENTITY_DESCRIPTIONS = (
BinarySensorEntityDescription(
key="is_moving",
device_class=BinarySensorDeviceClass.MOTION,
Expand All @@ -30,7 +30,7 @@
has_entity_name=True,
name="Occupancy",
),
]
)


async def async_setup_entry(
Expand Down
136 changes: 136 additions & 0 deletions homeassistant/components/ld2410_ble/sensor.py
Original file line number Diff line number Diff line change
@@ -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