feat: add Last Used timestamp sensor per code slot#585
feat: add Last Used timestamp sensor per code slot#585tykeal wants to merge 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.
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>
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.45% +4.31%
==========================================
Files 10 26 +16
Lines 801 3145 +2344
==========================================
+ Hits 674 2782 +2108
- Misses 127 363 +236
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. |
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
Nonewhen the slot is reset, and only tracks usage on the specific lock (not coupled to parent locks).Changes
lock.py: Addedlast_used: dt | None = Nonefield toKeymasterCodeSlotand type lookupcoordinator.py: Setlast_usedtimestamp in_lock_unlocked()for valid code slots (slot > 0)sensor.py: Create per-slotKeymasterSensorwithSensorDeviceClass.TIMESTAMPvia entity description inasync_setup_entrylovelace.py: Insert "Last Used" entity between "Active" and "Sync Status" in both regular and parent view cardstest_sensor.py: 6 new tests for last-used sensor behavior + updated entity countstest_coordinator.py: 4 new tests (set on unlock, not set for slot 0, parent not updated, cleared on reset)test_lovelace.py: Addedlast_usedto expected entity patternstest_init.py: Refactored to baseline counting withKEYMASTER_SENSOR_COUNTconstant for cross-version compatibilityDesign Decisions
last_usedis per-lock only — unlikeaccesslimit_count(shared budget), usage timestamps are observational datareset_code_slot()replaces the entireKeymasterCodeSlotinstance, solast_useddefaults toNoneautomatically