Skip to content

[Aqara/Locks] Remove unnecessary sequence number increment check#2931

Open
haedo-doo wants to merge 2 commits intoSmartThingsCommunity:mainfrom
haedo-doo:Aqara-Lock-sequenceNumber
Open

[Aqara/Locks] Remove unnecessary sequence number increment check#2931
haedo-doo wants to merge 2 commits intoSmartThingsCommunity:mainfrom
haedo-doo:Aqara-Lock-sequenceNumber

Conversation

@haedo-doo
Copy link
Copy Markdown
Contributor

@haedo-doo haedo-doo commented Apr 29, 2026

Check all that apply

Type of Change

  • WWST Certification Request
    • If this is your first time contributing code:
      • I have reviewed the README.md file
      • I have reviewed the CODE_OF_CONDUCT.md file
      • I have signed the CLA
    • I plan on entering a WWST Certification Request or have entered a request through the WWST Certification console at developer.smartthings.com
  • Bug fix
  • New feature
  • Refactor

Checklist

  • I have performed a self-review of my code
  • I have commented my code in hard-to-understand areas
  • I have verified my changes by testing with a device or have communicated a plan for testing
  • I am adding new behavior, such as adding a sub-driver, and have added and run new unit tests to cover the new behavior

Description of Change

Summary of Completed Tests

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

Test Results

   72 files    506 suites   0s ⏱️
2 790 tests 2 790 ✅ 0 💤 0 ❌
4 678 runs  4 678 ✅ 0 💤 0 ❌

Results for commit b02a6de.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

File Coverage
All files 58%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/Aqara/aqara-lock/src/credential_utils.lua 66%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/Aqara/aqara-lock/src/init.lua 54%

Minimum allowed coverage is 90%

Generated by 🐒 cobertura-action against b02a6de

@haedo-doo
Copy link
Copy Markdown
Contributor Author

Avoid redundant key exchanges triggered by out-of-order Zigbee messages under poor signal conditions.

Comment thread drivers/Aqara/aqara-lock/src/init.lua Outdated
end
else
request_generate_shared_key(device)
device:set_field(SERIAL_NUM_RX, serial_num)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you anticipate a future use of this or could be considered for removal? In the current code I don't see anything else reading the SERIAL_NUM_RX field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I've removed the unnecessary fields.

Copy link
Copy Markdown
Contributor

@cjswedes cjswedes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There currently are not any tests for how an old serial number in a message is handled. Please add a test to show what events the driver will emit if such a message is received.

local serial_num = toValue(msg, 3, 2)
local last_serial_num = device:get_field(SERIAL_NUM_RX) or 0

if serial_num > last_serial_num then
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this check removed, how will duplicate/stale messages that are sent by a device be detected? It seems like this could result in extra/stale lock events being emitted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. The main reason for this change is to prevent an infinite loop of shared key regeneration caused by out-of-order messages in unstable Zigbee networks.

In the current logic, if a message (e.g., N+1) is lost but subsequent messages (N+2 to N+8) arrive first, the driver expects the next sequence to be higher than N+8. When the door lock later retries the lost N+1, the driver treats it as an invalid or stale packet and immediately triggers the shared key regeneration process. In poor network conditions, this cycle repeats indefinitely, leading to a deadlock where the device becomes unusable.

toValue(payload, 6, string.byte(payload, 5)))
end
else
request_generate_shared_key(device)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the initial reason for having this here? are there cases where a key does need to be generated if the message serial numbers start getting out of order?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this validation was initially implemented following the manufacturer's guide, it causes severe side effects in real-world environments. Therefore, I decided to remove it to ensure connectivity and stability.

Regarding the test cases, the SmartThings Edge Driver framework does not support simulating the encryption key generation process or assigning arbitrary keys. Since the door lock communicates all data with the hub exclusively through encrypted messages, it is currently impossible to implement meaningful test cases that incorporate this encryption logic. We ask for your understanding regarding these platform-level constraints.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants