feat: add Last Used timestamp sensor per code slot#585
feat: add Last Used timestamp sensor per code slot#585tykeal merged 1 commit intoFutureTense:mainfrom
Conversation
33a3e39 to
b53b155
Compare
There was a problem hiding this comment.
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 | NonetoKeymasterCodeSlotand 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.
b53b155 to
d3e7ba0
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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 | NonetoKeymasterCodeSlotand 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.
firstof9
left a comment
There was a problem hiding this comment.
Looks good, interesting idea, did someone request this feature?
|
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! |
|
LOL turns out I might have gotten some inspiration from @raman325 as I just noticed that LCM has something like this sensor! |
|
If you think it's good to merge go for it man. |
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. |
ec18bac to
f72aed2
Compare
There was a problem hiding this comment.
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
eventplatform entity per code slot that triggers on unlock and clears on slot reset. - Fires a new
keymaster_code_slot_resetbus event fromreset_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.
f72aed2 to
cddeee3
Compare
There was a problem hiding this comment.
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
eventplatform (KeymasterCodeSlotEventEntity) that listens for lock-unlock events and slot-reset events. - Fires a new
keymaster_code_slot_resetbus event fromreset_code_slot()to clear the event entity state when a slot is reset. - Updates Lovelace generation and test expectations to account for
last_usedmoving 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.
cddeee3 to
250509b
Compare
There was a problem hiding this comment.
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
eventplatform 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_resetevent fromreset_code_slot()and registersPlatform.EVENTfor the integration. - Updates Lovelace generation plus unit tests/entity-count expectations to account for
last_usedmoving 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.
firstof9
left a comment
There was a problem hiding this comment.
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>
250509b to
2149f83
Compare
Should be fixed now. Will wait on Codecov to re-report |
|
Ok, Codecov is 100% again. Merging. |
Summary
Add an EventEntity per code slot that records when the slot was last used to unlock the lock.
Changes
event.py(new):KeymasterCodeSlotEventEntityextending bothKeymasterEntityandEventEntity. Listens for the existingEVENT_KEYMASTER_LOCK_STATE_CHANGEDbus event and triggers on matching unlock events. Also listens for a newEVENT_KEYMASTER_CODE_SLOT_RESETbus event to clear state when a slot is reset.const.py: AddedPlatform.EVENTto PLATFORMS, addedEVENT_KEYMASTER_CODE_SLOT_RESETconstant.coordinator.py: FiresEVENT_KEYMASTER_CODE_SLOT_RESETbus event fromreset_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
_trigger_event("unlocked", ...)which sets the state to a UTC ISO 8601 timestamp.code_slot_num,code_slot_name, andnameattributes for rich event context.