Skip to content

feat: Add auto-lock timer sensor with dashboard badge (#509)#577

Open
tykeal wants to merge 1 commit intoFutureTense:mainfrom
tykeal:autolock_countdown
Open

feat: Add auto-lock timer sensor with dashboard badge (#509)#577
tykeal wants to merge 1 commit intoFutureTense:mainfrom
tykeal:autolock_countdown

Conversation

@tykeal
Copy link
Collaborator

@tykeal tykeal commented Mar 6, 2026

Summary

Implements the feature requested in #509 — display the auto-lock countdown timer on the Home Assistant dashboard.

Changes

New auto-lock timer sensor (sensor.autolock_timer):

  • Timestamp sensor (device_class=TIMESTAMP) that shows when the auto-lock timer will fire
  • Timer-emulating attributes: duration, remaining, finishes_at, is_running
  • State is unknown when timer is idle, a timestamp when running
  • Unavailable when lock is disconnected or autolock is disabled

Dashboard integration:

  • Conditional badge added to generated Lovelace dashboard
  • Badge only visible when timer is running (not unknown/unavailable)

Timer responsiveness:

  • Coordinator pushes data updates immediately when timer starts/cancels
  • Previously entities only saw timer state changes on the ~30-second refresh cycle

Timer state consistency:

  • Refactored KeymasterTimer to use shared _cleanup_expired()/_check_expired() methods
  • All property accessors now consistently clear _duration on timer expiry (previously only cancel() did)
  • Sensor snapshots all timer values atomically to avoid race conditions

Bug fix — float-to-int conversion:

  • HA's NumberEntity passes float values to async_set_native_value
  • Autolock minute values (autolock_min_day/autolock_min_night) are typed as int but were stored as float after user modification
  • This caused ValueError in the sensor's :02d format specifier, making the entity stay unknown

Testing

  • 17 new tests across sensor, coordinator, lovelace, and number test files
  • All 409 tests pass (excluding 2 pre-existing failures in test_init.py)

Closes #509

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements issue #509 by exposing the auto-lock countdown as a new Home Assistant timestamp sensor and wiring it into the generated Lovelace dashboard so users can see the timer while it’s running.

Changes:

  • Added sensor.autolock_timer (timestamp device class) with timer-style attributes (duration, remaining, finishes_at, is_running).
  • Updated coordinator to push immediate updates when the auto-lock timer starts/cancels (instead of waiting for the periodic refresh).
  • Fixed autolock minute number entities to coerce float inputs to int, and added/updated tests for the new behavior and dashboard badge.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
custom_components/keymaster/sensor.py Adds the new auto-lock timer sensor entity and its coordinator update handling.
custom_components/keymaster/helpers.py Extends KeymasterTimer to track duration and centralizes expiry cleanup/check logic.
custom_components/keymaster/coordinator.py Pushes async_set_updated_data immediately after timer start/cancel for responsiveness.
custom_components/keymaster/number.py Converts autolock minute values from float to int in async_set_native_value.
custom_components/keymaster/lovelace.py Adds a conditional “Auto Lock Timer” badge and supports custom visibility conditions.
tests/test_sensor.py Adds tests covering the new sensor behavior and seconds formatting helper.
tests/test_number.py Adds parametric test verifying float-to-int conversion for autolock minute numbers.
tests/test_lovelace.py Adds test ensuring the autolock timer badge is generated with the intended visibility conditions.
tests/test_coordinator.py Adds tests/expectations for immediate coordinator data push on timer start/cancel.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.05%. Comparing base (cdb4922) to head (fe47550).
⚠️ Report is 46 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #577      +/-   ##
==========================================
+ Coverage   84.14%   86.05%   +1.91%     
==========================================
  Files          10       25      +15     
  Lines         801     2804    +2003     
==========================================
+ Hits          674     2413    +1739     
- Misses        127      391     +264     
Flag Coverage Δ
python 86.05% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@firstof9
Copy link
Collaborator

firstof9 commented Mar 6, 2026

I'd like to see 100% patch coverage 😃

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 6, 2026

I'd like to see 100% patch coverage 😃

Of course you would. So would I! ;) I'll get that fixed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add a timestamp sensor that tracks the auto-lock countdown timer,
with timer-emulating attributes (duration, remaining, finishes_at,
is_running). The sensor shows a timestamp when the timer is running
and 'unknown' when idle.

Dashboard integration:
- Add conditional badge to generated Lovelace dashboard
- Badge only visible when timer is running (not unknown/unavailable)

Timer responsiveness:
- Push coordinator data updates immediately when timer starts/cancels
- Previously entities only saw timer changes on ~30s refresh cycle

Timer state consistency:
- Refactor KeymasterTimer to use shared _cleanup_expired() method
- All property accessors now consistently clear duration on expiry
- Snapshot timer values atomically in sensor to avoid race conditions

Fix float-to-int conversion:
- Convert autolock minute values from float to int in number entity
- HA NumberEntity passes float values that caused ValueError in
  _seconds_to_hhmmss format string when using :02d specifier

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
@tykeal tykeal force-pushed the autolock_countdown branch from 386b4e4 to fe47550 Compare March 6, 2026 16:19
@tykeal
Copy link
Collaborator Author

tykeal commented Mar 6, 2026

@firstof9 100% coverage on the change now :)

@firstof9
Copy link
Collaborator

firstof9 commented Mar 6, 2026

Does "auto lock" account for the door status, if it exists?

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 6, 2026

No, it doesn't. We do have a separate issue about that (#572). This is explicitly about #509 which is adding the timer to the dashboard

@tykeal tykeal added enhancement New feature or request feature New feature labels Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Display auto-lock countdown on dashboard

4 participants