Skip to content

Raise when a lock rejects a lock, unlock or user code command - #1056

Draft
zigpy-review-bot wants to merge 3 commits into
devfrom
zigpy-bot/audit/raise-on-rejected-lock-commands
Draft

zigpy-review-bot wants to merge 3 commits into
devfrom
zigpy-bot/audit/raise-on-rejected-lock-commands

Conversation

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator

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 ZHAException into 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's status field, and raises ZHAException when it isn't SUCCESS. 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 as MAC_PAN_ACCESS_DENIED.
  • async_lock, async_unlock, async_set_lock_user_code, async_clear_lock_user_code, async_enable_lock_user_code and async_disable_lock_user_code call 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 carrying SUCCESS used 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.unlock and 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 sets continue_on_error, the same as for a switch or cover today.

Testing

  • With the lock code of 2da1a5b, tests/test_lock.py fails 8 of 9 (test_lock_command_failure_raises for all six commands, test_set_user_code_failure_reason and the updated test_lock, each "DID NOT RAISE ZHAException"); with the fix all pass.
  • Full test suite: 1788 passed. pre-commit (codespell, ruff, ruff format, mypy) passes on the changed files.

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

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.
@zigpy-review-bot zigpy-review-bot added bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bugfix This PR fixes a bug bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) labels Nov 22, 2026
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.48%. Comparing base (2da1a5b) to head (c305a76).

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

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

Labels

bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lock entity reports a rejected lock, unlock or user code command as a success

1 participant