Skip to content

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

Merged
tykeal merged 1 commit intoFutureTense:mainfrom
tykeal:last-used-sensor
Mar 19, 2026
Merged

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

Conversation

@tykeal
Copy link
Collaborator

@tykeal tykeal commented Mar 16, 2026

Summary

Add an EventEntity per code slot that records when the slot was last used to unlock the lock.

Changes

  • event.py (new): KeymasterCodeSlotEventEntity extending both KeymasterEntity and EventEntity. Listens for the existing EVENT_KEYMASTER_LOCK_STATE_CHANGED bus event and triggers on matching unlock events. Also listens for a new EVENT_KEYMASTER_CODE_SLOT_RESET bus event to clear state when a slot is reset.
  • const.py: Added Platform.EVENT to PLATFORMS, added EVENT_KEYMASTER_CODE_SLOT_RESET constant.
  • coordinator.py: Fires EVENT_KEYMASTER_CODE_SLOT_RESET bus event from reset_code_slot().
  • lovelace.py: Added "Last Used" event entity between "Active" and "Sync Status" on the generated dashboard.
  • test_event.py (new): 12 tests covering initialization, unlock triggering, event filtering (wrong slot, slot 0, wrong lock, locked state), reset clearing, reset filtering (wrong slot, wrong lock), availability, and setup.
  • test_coordinator.py: Added test for reset bus event firing.
  • test_init.py, test_sensor.py: Updated entity counts.

Design Notes

  • EventEntity extends RestoreEntity, so the last-used timestamp automatically survives Home Assistant restarts without custom restore logic.
  • The entity uses _trigger_event("unlocked", ...) which sets the state to a UTC ISO 8601 timestamp.
  • On slot reset, internal EventEntity state is cleared via name-mangled private attributes (no public API exists for this).
  • Event data includes code_slot_num, code_slot_name, and name attributes for rich event context.

@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.

@tykeal tykeal force-pushed the last-used-sensor branch from b53b155 to d3e7ba0 Compare March 16, 2026 13:16
@codecov-commenter
Copy link

codecov-commenter commented Mar 16, 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 88.72%. Comparing base (cdb4922) to head (2149f83).
⚠️ Report is 66 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.72%   +4.58%     
==========================================
  Files          10       27      +17     
  Lines         801     3211    +2410     
==========================================
+ Hits          674     2849    +2175     
- Misses        127      362     +235     
Flag Coverage Δ
python 88.72% <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?

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 18, 2026

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

Yes, my test and home system, not other systems, but it's working well.

In the mean time. I've pushed up a version that takes care of the merge conflict that happened after I merged the notification PR fix.

@tykeal tykeal force-pushed the last-used-sensor branch 2 times, most recently from ec18bac to f72aed2 Compare March 18, 2026 21:46
@tykeal tykeal requested a review from Copilot March 18, 2026 21:48
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 as an event entity so Home Assistant can persist and surface the last unlock time per slot, including reset behavior when a slot is cleared.

Changes:

  • Introduces an event platform entity per code slot that triggers on unlock and clears on slot reset.
  • Fires a new keymaster_code_slot_reset bus event from reset_code_slot() and updates Lovelace generation to display “Last Used”.
  • Updates and adds tests to cover the new platform and updated entity counts.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
custom_components/keymaster/event.py New event platform and entity implementation for per-slot “Last Used”.
custom_components/keymaster/const.py Adds Platform.EVENT and EVENT_KEYMASTER_CODE_SLOT_RESET.
custom_components/keymaster/coordinator.py Fires reset bus event when a code slot is reset.
custom_components/keymaster/lovelace.py Adds “Last Used” event entity to generated Lovelace cards.
tests/test_event.py New tests for event entity behavior and setup.
tests/test_coordinator.py Adds test ensuring reset event is fired.
tests/test_init.py Adjusts sensor entity count assertions (last_used moved off sensors).
tests/test_sensor.py Updates sensor tests/comments to reflect move to event platform.
tests/test_lovelace.py Updates expected Lovelace entities to include last_used.

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

@tykeal tykeal force-pushed the last-used-sensor branch from f72aed2 to cddeee3 Compare March 18, 2026 21:56
@tykeal tykeal requested a review from Copilot March 18, 2026 21:58
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 as a Home Assistant event entity, enabling persistent tracking of the most recent unlock by each code slot and integrating it into the generated Lovelace dashboard.

Changes:

  • Introduces an event platform (KeymasterCodeSlotEventEntity) that listens for lock-unlock events and slot-reset events.
  • Fires a new keymaster_code_slot_reset bus event from reset_code_slot() to clear the event entity state when a slot is reset.
  • Updates Lovelace generation and test expectations to account for last_used moving from sensor to event entities.

Reviewed changes

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

Show a summary per file
File Description
custom_components/keymaster/event.py New event platform entity that records “Last Used” unlock timestamps and clears on reset.
custom_components/keymaster/const.py Adds Platform.EVENT and introduces EVENT_KEYMASTER_CODE_SLOT_RESET.
custom_components/keymaster/coordinator.py Fires the new slot-reset bus event from reset_code_slot().
custom_components/keymaster/lovelace.py Adds “Last Used” event entity to generated code slot cards.
tests/test_event.py New tests covering event entity behavior (unlock, filtering, reset, availability, setup).
tests/test_coordinator.py Adds test verifying reset bus event firing.
tests/test_init.py Updates sensor entity count expectations (last_used moved off sensor platform).
tests/test_sensor.py Updates comments/expectations reflecting last_used moving to event platform.
tests/test_lovelace.py Updates expected Lovelace entities to include code_slots_1_last_used.

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

@tykeal tykeal force-pushed the last-used-sensor branch from cddeee3 to 250509b Compare March 18, 2026 22:04
@tykeal tykeal requested a review from Copilot March 18, 2026 22:06
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 as a Home Assistant event entity, allowing each slot to record the last unlock time (and clear it when the slot is reset), with dashboard and test updates to reflect the new platform.

Changes:

  • Introduces event platform entities (KeymasterCodeSlotEventEntity) that trigger on existing lock-state bus events and clear on a new code-slot-reset bus event.
  • Fires a new keymaster_code_slot_reset event from reset_code_slot() and registers Platform.EVENT for the integration.
  • Updates Lovelace generation plus unit tests/entity-count expectations to account for last_used moving from sensor to event.

Reviewed changes

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

Show a summary per file
File Description
custom_components/keymaster/event.py New event platform + entity to track “Last Used” per slot and clear on reset.
custom_components/keymaster/const.py Adds Platform.EVENT and the new reset bus-event constant.
custom_components/keymaster/coordinator.py Fires keymaster_code_slot_reset from reset_code_slot().
custom_components/keymaster/lovelace.py Adds “Last Used” event entity to generated dashboard slot cards.
tests/test_event.py New test suite for event entity behavior and setup.
tests/test_coordinator.py Adds test coverage for reset bus-event firing.
tests/test_init.py Adjusts sensor entity counts (last_used moved off sensors).
tests/test_sensor.py Updates comments/count expectations for sensor setup tests.
tests/test_lovelace.py Updates expected slot entities to include code_slots_1_last_used.

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

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 18, 2026

@raman325 & @firstof9 ok, code is now using an event entity. I've verified it's working on my test rig. Data survives restarts too.

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 like lines 161-162 in event.py need some coverage, but other than that I'm good with the changes.

Add an EventEntity per code slot that records when the slot was last
used to unlock the lock.  EventEntity extends RestoreEntity, so the
timestamp automatically survives Home Assistant restarts.

The entity listens for the existing EVENT_KEYMASTER_LOCK_STATE_CHANGED
bus event and triggers on matching unlock events.  A new
EVENT_KEYMASTER_CODE_SLOT_RESET bus event is fired from
reset_code_slot() so the entity can clear its state when a slot is
reset.

The Last Used entity also appears on the generated Lovelace dashboard
between Active and Sync Status.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
@tykeal tykeal force-pushed the last-used-sensor branch from 250509b to 2149f83 Compare March 19, 2026 16:52
@tykeal
Copy link
Collaborator Author

tykeal commented Mar 19, 2026

Looks like lines 161-162 in event.py need some coverage, but other than that I'm good with the changes.

Should be fixed now. Will wait on Codecov to re-report

@tykeal
Copy link
Collaborator Author

tykeal commented Mar 19, 2026

Ok, Codecov is 100% again. Merging.

@tykeal tykeal merged commit fea5c46 into FutureTense:main Mar 19, 2026
9 checks passed
@tykeal tykeal deleted the last-used-sensor branch March 19, 2026 17:04
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.

5 participants