Skip to content

feat: ephemeral timers with auto-cleanup and optional naming#42

Merged
Faerkeren merged 2 commits into
mainfrom
feature/ephemeral-timers
Apr 27, 2026
Merged

feat: ephemeral timers with auto-cleanup and optional naming#42
Faerkeren merged 2 commits into
mainfrom
feature/ephemeral-timers

Conversation

@Faerkeren

Copy link
Copy Markdown
Contributor

Summary

  • Timers are ephemeral by default — HA helper is auto-created on first action and auto-deleted when the timer returns to idle, preventing accumulation of unused helpers
  • client.timer() without a name auto-generates a unique ID (timer.haclient_a1b2c3d4)
  • client.timer("name", persistent=True) keeps the HA helper alive after idle (requires explicit name)
  • Same Timer object can be restarted after auto-cleanup — helper is transparently re-created

API Examples

# Ephemeral (default) — auto-created, auto-deleted on idle
t = client.timer()
await t.start(duration="00:05:00")
# HA helper deleted when timer finishes

# Named ephemeral
t = client.timer("my_cooldown")

# Persistent — stays in HA
t = client.timer("kitchen_timer", persistent=True)

Implementation

  • Timer._handle_state_changed override schedules cleanup after all user listeners
  • _auto_cleanup gracefully handles delete failures (logs + resets state)
  • persistent=True without name raises ValueError
  • 23 tests (179 total), timer.py at 100% coverage, overall 95.26%

Timers are now ephemeral by default — the HA helper is created on
the first action and automatically deleted when the timer returns
to idle. This prevents accumulation of unused timer helpers.

- client.timer() without a name auto-generates a unique ID
- client.timer(persistent=True) keeps the HA helper alive (requires name)
- Timer._handle_state_changed overridden to schedule cleanup after
  all user listeners have been dispatched
- Timer._auto_cleanup gracefully handles delete failures
- 23 tests covering ephemeral lifecycle, persistent mode, auto-naming,
  edge cases, and error handling
Only timers created by the library (via _ensure_exists/timer/create)
are eligible for ephemeral auto-cleanup. Timers that already exist
in Home Assistant (e.g. created via the UI or config) are never
auto-deleted, regardless of the persistent flag.

Adds _created_by_us tracking flag set during _ensure_exists and
checked before scheduling auto-cleanup.
@Faerkeren Faerkeren merged commit 077ca29 into main Apr 27, 2026
12 checks passed
@Faerkeren Faerkeren deleted the feature/ephemeral-timers branch April 27, 2026 15:56
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.

1 participant