Skip to content

feat: add Last Used timestamp sensor per code slot#585

Open
tykeal wants to merge 1 commit intoFutureTense:mainfrom
tykeal:last-used-sensor
Open

feat: add Last Used timestamp sensor per code slot#585
tykeal wants to merge 1 commit intoFutureTense:mainfrom
tykeal:last-used-sensor

Conversation

@tykeal
Copy link
Collaborator

@tykeal tykeal commented Mar 16, 2026

Summary

Adds a per-code-slot "Last Used" timestamp sensor that records when each PIN code was last used to unlock. The sensor resets to None when the slot is reset, and only tracks usage on the specific lock (not coupled to parent locks).

Changes

  • lock.py: Added last_used: dt | None = None field to KeymasterCodeSlot and type lookup
  • coordinator.py: Set last_used timestamp in _lock_unlocked() for valid code slots (slot > 0)
  • sensor.py: Create per-slot KeymasterSensor with SensorDeviceClass.TIMESTAMP via entity description in async_setup_entry
  • lovelace.py: Insert "Last Used" entity between "Active" and "Sync Status" in both regular and parent view cards
  • test_sensor.py: 6 new tests for last-used sensor behavior + updated entity counts
  • test_coordinator.py: 4 new tests (set on unlock, not set for slot 0, parent not updated, cleared on reset)
  • test_lovelace.py: Added last_used to expected entity patterns
  • test_init.py: Refactored to baseline counting with KEYMASTER_SENSOR_COUNT constant for cross-version compatibility

Design Decisions

  • last_used is per-lock only — unlike accesslimit_count (shared budget), usage timestamps are observational data
  • Only unlock events track code usage (lock events don't carry code_slot_num)
  • reset_code_slot() replaces the entire KeymasterCodeSlot instance, so last_used defaults to None automatically

@tykeal tykeal requested a review from Copilot March 16, 2026 13:06
@github-actions github-actions bot added the feature New feature label Mar 16, 2026
@tykeal tykeal force-pushed the last-used-sensor branch from 33a3e39 to b53b155 Compare March 16, 2026 13:10
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

Adds per-code-slot “Last Used” timestamp tracking and exposes it as a Home Assistant timestamp sensor, so users can see when each PIN slot was last used to unlock.

Changes:

  • Add last_used: datetime | None to KeymasterCodeSlot and set it on unlock events for valid (non-zero) code slots.
  • Add a per-slot KeymasterLastUsedSensor (SensorDeviceClass.TIMESTAMP) and include it in setup.
  • Update Lovelace card generation and test expectations to include the new entity, plus add coordinator/sensor test coverage.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
custom_components/keymaster/lock.py Adds last_used to the code slot model and type lookup for persistence/serialization.
custom_components/keymaster/coordinator.py Sets last_used on unlock for the used code slot and triggers coordinator updates.
custom_components/keymaster/sensor.py Creates per-slot last-used sensors and defines the new sensor entity class.
custom_components/keymaster/lovelace.py Inserts “Last Used” entity into code slot cards (regular + parent view).
tests/test_sensor.py Updates entity creation counts and adds targeted tests for last-used sensor behavior.
tests/test_coordinator.py Adds tests verifying last_used is set/reset correctly and not propagated to parent locks.
tests/test_lovelace.py Updates expected generated Lovelace entity list to include last_used.
tests/test_init.py Updates sensor entity count expectations for integration setup/unload/remove.

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

Add a new per-slot sensor that records the datetime when a code was last
used to unlock the lock. The sensor uses SensorDeviceClass.TIMESTAMP and
displays on the generated dashboard between Active and Sync Status.

Changes:
- Add last_used field to KeymasterCodeSlot dataclass
- Set last_used in _lock_unlocked() when a valid code slot is used
- Add KeymasterLastUsedSensor class with timestamp device class
- Insert Last Used entity in both regular and parent Lovelace views
- Fix pre-existing broken sensor counts in test_init.py

The last_used value resets to None when a slot is reset (handled by
reset_code_slot replacing the entire KeymasterCodeSlot instance).

Co-authored-by: Copilot <copilot@github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
@tykeal tykeal force-pushed the last-used-sensor branch from b53b155 to d3e7ba0 Compare March 16, 2026 13:16
@codecov-commenter
Copy link

⚠️ 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 88.45%. Comparing base (cdb4922) to head (d3e7ba0).
⚠️ Report is 64 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     #585      +/-   ##
==========================================
+ Coverage   84.14%   88.45%   +4.31%     
==========================================
  Files          10       26      +16     
  Lines         801     3145    +2344     
==========================================
+ Hits          674     2782    +2108     
- Misses        127      363     +236     
Flag Coverage Δ
python 88.45% <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.

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

Adds a per-code-slot “Last Used” timestamp sensor to the Keymaster integration, updating coordinator state on unlock events and surfacing the value in generated Lovelace views.

Changes:

  • Add last_used: datetime | None to KeymasterCodeSlot and set it on unlock for valid code slots.
  • Create per-slot timestamp sensors (SensorDeviceClass.TIMESTAMP) and include them in Lovelace card generation.
  • Expand/adjust tests to cover entity creation, availability, timestamp behavior, and reset behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
custom_components/keymaster/lock.py Adds last_used field to code slot model and type lookup for (de)serialization.
custom_components/keymaster/coordinator.py Sets last_used on _lock_unlocked() for the used code slot and triggers coordinator update.
custom_components/keymaster/sensor.py Creates per-slot last_used sensors using KeymasterSensor + timestamp device class.
custom_components/keymaster/lovelace.py Inserts “Last Used” entity into slot cards in both regular and parent views.
tests/test_sensor.py Updates entity counts and adds last-used sensor test coverage.
tests/test_coordinator.py Adds coordinator tests for setting/clearing last_used and parent behavior.
tests/test_init.py Updates sensor count assertions to account for the new per-slot sensors.

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

Copy link
Collaborator

@firstof9 firstof9 left a comment

Choose a reason for hiding this comment

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

Looks good, interesting idea, did someone request this feature?

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 16, 2026

My brain did this morning when I was searching through the logs to see the last time one of my guest rooms in the house had their code used!

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 17, 2026

LOL turns out I might have gotten some inspiration from @raman325 as I just noticed that LCM has something like this sensor!

@firstof9
Copy link
Collaborator

If you think it's good to merge go for it man.
I assume you've tested it locally on a HA instance right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants