Skip to content

Connectivity diagnostic sensors (v0.2.0)#1

Merged
magicx78 merged 1 commit into
mainfrom
feature/diagnostic-connectivity-sensors
Jun 3, 2026
Merged

Connectivity diagnostic sensors (v0.2.0)#1
magicx78 merged 1 commit into
mainfrom
feature/diagnostic-connectivity-sensors

Conversation

@magicx78

@magicx78 magicx78 commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Adds per-device diagnostic entities (entity_category=diagnostic, auto-created, no YAML) to monitor whether an HLK-LD2450 stays reliably online.

New entities: ble_connected (connectivity), connection_state (enum), last_seen, last_disconnect, disconnect_count, reconnect_count, offline_duration, online_duration.

Implementation: central state tracking in the coordinator (stale watchdog 30s, disconnect counted once per interruption, reconnect on recovery, heartbeat refresh); diagnostics stay available when offline; new LD2450BLE.is_connected; no changes to existing entity IDs/behaviour.

Deferred: reliability_24h (needs persistent history) - documented as TODO.

Quality: ruff clean, 35 pytest tests pass (7 new); verified live on real HLK-LD2450 hardware (48 entities incl. 8 diagnostics).

Add per-device diagnostic entities (entity_category=diagnostic, auto-created,
no YAML) to monitor whether an LD2450 stays online:

- binary_sensor ble_connected (connectivity)
- sensor connection_state (enum: connected/reconnecting/disconnected/stale)
- sensor last_seen, last_disconnect (timestamp)
- sensor disconnect_count, reconnect_count
- sensor offline_duration, online_duration (seconds)

Central state tracking in the coordinator: last_seen on each packet, one
disconnect counted per interruption, reconnect counted on recovery, stale
watchdog (STALE_TIMEOUT=30s), periodic refresh so durations tick while offline.
Diagnostic entities stay available when the device is offline. Added
LD2450BLE.is_connected. No changes to existing entity ids/behaviour.

reliability_24h intentionally deferred (needs persistent history) - documented
as TODO.

Tests: 7 new diagnostic tests (initial/last_seen/disconnect-once/reconnect/
stale/states/durations). ruff + 35 pytest tests pass. Verified live on real
HLK-LD2450 hardware (48 entities incl. 8 diagnostics).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9b0c294562

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +166 to +172
if self.device.is_connected:
if (
self.last_seen is not None
and (now - self.last_seen).total_seconds() > STALE_TIMEOUT
):
return STATE_STALE
return STATE_CONNECTED

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid reporting connected before any data arrives

When the BLE client connects but no valid target frame has ever been decoded (for example notifications are not delivered or parsing rejects all frames), last_seen remains None, so this branch returns connected indefinitely and ble_connected is true. The stale watchdog is only armed from _async_emit() after a decoded update, so the new diagnostics never transition to stale in this no-data scenario, which defeats the intended "fresh data" connectivity check.

Useful? React with 👍 / 👎.

@magicx78 magicx78 merged commit 6d432c7 into main Jun 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants