Repository navigation
Raise when a lock rejects a lock, unlock or user code command - #1056
Draft
zigpy-review-bot wants to merge 3 commits into
Draft
zigpy-review-bot wants to merge 3 commits into
zigpy-review-bot wants to merge 3 commits into
Conversation
The lock entity ignored the status of the set/clear PIN code and set user status responses, and only logged a rejected lock or unlock. A command the lock refused (failure, memory full, duplicate code, not authorized) passed as a success, so Home Assistant reported e.g. a cleared user code that the lock still accepts. Raise ZHAException on a non-success status, from the command's own response or from a Default Response, as the switch and cover entities already do.
The DoorLock responses carry their own status values (ZCL R8 7.3.2.16: 1 general failure, and for set PIN code 2 memory full, 3 duplicate code), which zigpy decodes as a ZCL Status (2 reads as MAC_PAN_ACCESS_DENIED). Name them in the exception, and cover the state after a Default Response.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #1056 +/- ##
==========================================
- Coverage 97.48% 97.48% -0.01%
==========================================
Files 61 61
Lines 10870 10881 +11
==========================================
+ Hits 10597 10607 +10
- Misses 273 274 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Severity: medium (the audit's estimate)
The lock entity never raised when a lock refused a command. The set, clear, enable and disable user code commands ignored the lock's response entirely, and lock / unlock only logged a failure status. A refused command therefore passed as a success in Home Assistant: for example, clearing a user code that the lock kept (the old code still opens the door) reported success. Home Assistant's ZHA lock entity already turns a
ZHAExceptioninto an error shown to the user, as it does for the switch and cover entities, which raise on a non-success status; the lock now does the same.Changes
DoorLock._raise_on_failure()reads the status of a DoorLock command's answer, from the command's own response (its first field) or, when the lock answers with a Default Response, from that response'sstatusfield, and raisesZHAExceptionwhen it isn'tSUCCESS. The lock's own status values are named in the message (RESPONSE_STATUS_REASONS: 1 general failure, 2 memory full, 3 duplicate code, ZCL R8 §7.3.2.16), since zigpy decodes them as a ZCL status and 2 would otherwise read asMAC_PAN_ACCESS_DENIED.async_lock,async_unlock,async_set_lock_user_code,async_clear_lock_user_code,async_enable_lock_user_codeandasync_disable_lock_user_codecall it. Lock and unlock still change the entity state only after a successful response. One difference: a lock that answers lock or unlock with a Default Response carryingSUCCESSused to be logged as an error with the state left alone (the check read the Default Response's command id); it now counts as a success.tests/test_lock.py: the two failure checks for lock and unlock now expect the exception; a new parametrized test covers all six commands with a failure in the command's own response, an error in a Default Response, and both forms of success, with the lock state after each (changed by a successful lock or unlock, untouched otherwise); another checks the reasons named for set PIN code statuses 2, 3 and an unknown value.Behaviour change
lock.lock,lock.unlockand the four user code services now fail with an error in Home Assistant when the lock refuses the command, where they used to return normally. An automation step that calls them stops on such an error unless it setscontinue_on_error, the same as for a switch or cover today.Testing
tests/test_lock.pyfails 8 of 9 (test_lock_command_failure_raisesfor all six commands,test_set_user_code_failure_reasonand the updatedtest_lock, each "DID NOT RAISE ZHAException"); with the fix all pass.Related: #678 (draft), which explores removing these status checks in ZHA once zigpy raises on a failure response.
Opened by the scheduled code audit; not requested by a maintainer.
Fixes #1055