Skip to content

fix: BLE entities unavailable after deep-sleep wakeup; configurable upload queue expiry#33

Merged
Misiu merged 5 commits into
deep_sleepfrom
copilot/deep-sleep
May 27, 2026
Merged

fix: BLE entities unavailable after deep-sleep wakeup; configurable upload queue expiry#33
Misiu merged 5 commits into
deep_sleepfrom
copilot/deep-sleep

Conversation

Copilot AI commented May 27, 2026

Copy link
Copy Markdown

Summary:
This PR fixes a real-world deep-sleep wakeup issue for BLE OpenDisplay tags and makes deep-sleep upload handling explicit and configurable.

The key user-visible problem was: when tags woke and advertised as non-connectable, entities could remain unavailable and sensor-driven automations would not reliably trigger.

Main functional changes:

  1. BLE availability fix for wakeup advertisements
    OpenDisplayBLEEntity.available now checks presence with connectable=False.
    This ensures tags are considered available when they wake and emit non-connectable advertisements.
    Result: sensor entities come back reliably and automations can trigger on wakeup updates.
  2. Deep-sleep upload queue (behavior + lifecycle)
    A dedicated DeepSleepUploadQueue is used for sleeping tags so uploads are not lost or sent at the wrong time.

How it works:

  1. Upload request arrives for a tag.
  2. Integration checks whether tag is currently sleeping in deep-sleep mode.
  3. If sleeping, upload is queued per tag instead of sent immediately.
  4. On tag check-in/wakeup signal, queued upload is moved into normal upload flow and executed.

Replacement semantics (important)
Queue is intentionally single-slot per tag:

  • If a new upload is queued for the same tag while one is already pending, the old one is replaced by the newest one.
  • This keeps behavior deterministic and avoids stale/obsolete images being sent after wake.
  • Maintainers get predictable “last write wins” semantics for sleeping devices.

Expiration semantics

  • Pending deep-sleep uploads expire after a configurable timeout.
  • Default timeout is 4 hours.
  • Expired items are dropped to prevent very old payloads being uploaded after long sleep gaps.
  1. Options Flow support for queue expiry
    Added integration option for deep-sleep queue expiry hours.
    Range is validated/clamped (1–24h).
    Default remains 4h.

About the broader file diff

This branch includes a few small cleanup/refactor edits outside strict deep-sleep code paths (for example removing unused imports/unused locals in some files).

These are low-risk, no-behavior-change cleanups and are left in the PR intentionally to keep the branch history intact. The deep-sleep behavior changes are still concentrated in:

  • custom_components/opendisplay/upload.py
  • custom_components/opendisplay/services.py
  • custom_components/opendisplay/coordinator.py
  • custom_components/opendisplay/config_flow.py
  • custom_components/opendisplay/entity.py
  • custom_components/opendisplay/const.py
  • tests/test_deep_sleep_queue.py
  • tests/test_ble_entity_available.py

Test coverage
Deep-sleep queue tests

tests/test_deep_sleep_queue.py verifies:

  • replacement of existing pending upload for same tag
  • default expiry behavior (4h)
  • configurable expiry behavior
  • wake/check-in flush into active upload queue
  • queue decision logic for sleeping vs awake tags

Availability regression tests
tests/test_ble_entity_available.py verifies:

  • entity availability check uses connectable=False
  • non-connectable-only presence marks entity available
  • unavailable remains correct when device is not seen

Why this should be merged

  • Fixes a user-facing reliability issue (wakeups not reflected in HA availability/sensors).
  • Prevents stale deep-sleep uploads by design (single-slot replacement + expiry).
  • Adds operator control via Options Flow instead of hardcoded behavior.
  • Includes targeted tests for both queue semantics and BLE availability regression.

Copilot AI changed the title fix: accept non-connectable BLE advertisements for entity availability fix: BLE entities unavailable after deep-sleep wakeup; configurable upload queue expiry May 27, 2026
Copilot finished work on behalf of Misiu May 27, 2026 05:22
Copilot AI requested a review from Misiu May 27, 2026 05:22
Copilot finished work on behalf of Misiu May 27, 2026 05:31
Copilot finished work on behalf of Misiu May 27, 2026 05:33
Copilot finished work on behalf of Misiu May 27, 2026 05:35
@Misiu Misiu marked this pull request as ready for review May 27, 2026 05:41
@Misiu Misiu merged commit 7e83ffa into deep_sleep May 27, 2026
4 of 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