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 a debounce to ld2410 to prevent it from overwhelming the state machine with many devices #93819

Merged
merged 2 commits into from
May 31, 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
5 changes: 4 additions & 1 deletion homeassistant/components/ld2410_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from bleak_retry_connector import BleakError, get_device
from bleak_retry_connector import BleakError, close_stale_connections, get_device
from ld2410_ble import LD2410BLE

from homeassistant.components import bluetooth
Expand Down Expand Up @@ -31,6 +31,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
raise ConfigEntryNotReady(
f"Could not find LD2410B device with address {address}"
)

await close_stale_connections(ble_device)

ld2410_ble = LD2410BLE(ble_device)

coordinator = LD2410BLECoordinator(hass, ld2410_ble)
Expand Down
38 changes: 36 additions & 2 deletions homeassistant/components/ld2410_ble/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""Data coordinator for receiving LD2410B updates."""

from datetime import datetime
import logging
import time

from ld2410_ble import LD2410BLE, LD2410BLEState

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

NEVER_TIME = -86400.0
DEBOUNCE_SECONDS = 1.0


class LD2410BLECoordinator(DataUpdateCoordinator[None]):
"""Data coordinator for receiving LD2410B updates."""
Expand All @@ -26,15 +32,43 @@ def __init__(self, hass: HomeAssistant, ld2410_ble: LD2410BLE) -> None:
ld2410_ble.register_callback(self._async_handle_update)
ld2410_ble.register_disconnected_callback(self._async_handle_disconnect)
self.connected = False
self._last_update_time = NEVER_TIME
self._debounce_cancel: CALLBACK_TYPE | None = None
self._debounced_update_job = HassJob(
self._async_handle_debounced_update,
f"LD2410 {ld2410_ble.address} BLE debounced update",
)

@callback
def _async_handle_debounced_update(self, _now: datetime) -> None:
"""Handle debounced update."""
self._debounce_cancel = None
self._last_update_time = time.monotonic()
self.async_set_updated_data(None)
bdraco marked this conversation as resolved.
Show resolved Hide resolved

@callback
def _async_handle_update(self, state: LD2410BLEState) -> None:
"""Just trigger the callbacks."""
self.connected = True
self.async_set_updated_data(None)
previous_last_updated_time = self._last_update_time
self._last_update_time = time.monotonic()
if self._last_update_time - previous_last_updated_time >= DEBOUNCE_SECONDS:
self.async_set_updated_data(None)
return
if self._debounce_cancel is None:
self._debounce_cancel = async_call_later(
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self.hass, DEBOUNCE_SECONDS, self._debounced_update_job
)

@callback
def _async_handle_disconnect(self) -> None:
"""Trigger the callbacks for disconnected."""
self.connected = False
self.async_update_listeners()

async def async_shutdown(self) -> None:
"""Shutdown the coordinator."""
if self._debounce_cancel is not None:
self._debounce_cancel()
self._debounce_cancel = None
await super().async_shutdown()