feat: ephemeral timers with auto-cleanup and optional naming#42
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
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)API Examples
Implementation
Timer._handle_state_changedoverride schedules cleanup after all user listeners_auto_cleanupgracefully handles delete failures (logs + resets state)persistent=Truewithout name raisesValueError