From 06c3bda49e10e89994f8c7b0d0cbccef07329131 Mon Sep 17 00:00:00 2001 From: Jean-Emmanuel Losi Date: Fri, 4 Dec 2026 21:22:29 -0500 Subject: [PATCH] zwave lock: do not assume a 20-entry limit when the lock's capacity is unknown A migrated lock whose capacity was never read (pinUsersSupported and totalUsersSupported unset: the lock did not answer UsersNumberGet and the hub held no lockCodes maxCodes to copy at migration) fell back to an arbitrary limit of 20 in is_full. A lock already holding 20 or more codes then answered every addCredential with resourceExhausted without the command reaching the lock, and add_credential_and_user would also drop a code the lock confirmed. Seen on Yale Z-Wave locks (0129-8002-0600, 0129-8109-0DD2, 0129-8004-0600) reporting maxCodes 250 and holding 21-47 codes after migration. is_full now uses the lock's reported capacity, else the lockCodes maxCodes it reported before migration, and treats the table as not full when neither is known, so the lock itself decides. A lock that reports its capacity is still refused at that capacity. --- .../zwave-lock/src/lock_utils/tables.lua | 15 +++- .../test/test_lock_credentials_commands.lua | 83 +++++++++++++++++++ .../src/test/test_lock_programming_code.lua | 13 ++- .../zwave-lock/src/test/test_lock_tables.lua | 1 + 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua b/drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua index a482ea46a9..5c6f6a8e7b 100644 --- a/drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua +++ b/drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua @@ -157,11 +157,22 @@ function table_utils.get_max_entries(device, table_name) return device:get_latest_state("main", def.capability.ID, def.max_entries.NAME, 20) -- arbitrary, default to 20 if the attribute is missing end +-- The lock's own capacity for the named table: its max_entries attribute, else the lockCodes maxCodes it reported +-- before migration. nil when neither has been read. +local function reported_max_entries(device, def) + return device:get_latest_state("main", def.capability.ID, def.max_entries.NAME) + or device:get_latest_state("main", capabilities.lockCodes.ID, capabilities.lockCodes.maxCodes.NAME) +end + -- Return true if the named table has reached its max_entries limit. A programming user, and the credential it owns, --- is not one of the lock's user slots, so it does not count toward the limit. +-- is not one of the lock's user slots, so it does not count toward the limit. When the lock's capacity is unknown +-- (it did not answer UsersNumberGet and reported no maxCodes), the table is never full: the lock itself refuses a +-- code it has no room for, and an assumed limit would refuse codes a lock with room for hundreds would take. function table_utils.is_full(device, table_name) local def = resolve_table_def(device, table_name) if not def then return true end + local max_entries = reported_max_entries(device, def) + if max_entries == nil then return false end local programming_users = {} for _, user in ipairs(table_utils.get_state(device, "users") or {}) do if user.userType == consts.PROGRAMMING_USER.USER_TYPE then programming_users[user.userIndex] = true end @@ -170,7 +181,7 @@ function table_utils.is_full(device, table_name) for _, entry in ipairs(table_utils.get_state(device, def) or {}) do if not programming_users[entry.userIndex] then count = count + 1 end end - return count >= table_utils.get_max_entries(device, table_name) + return count >= max_entries end -- Add an entry to a named table. The entry must satisfy all required_keys for diff --git a/drivers/SmartThings/zwave-lock/src/test/test_lock_credentials_commands.lua b/drivers/SmartThings/zwave-lock/src/test/test_lock_credentials_commands.lua index 7cbfb825fd..2dfb6f0950 100644 --- a/drivers/SmartThings/zwave-lock/src/test/test_lock_credentials_commands.lua +++ b/drivers/SmartThings/zwave-lock/src/test/test_lock_credentials_commands.lua @@ -248,6 +248,89 @@ test.register_coroutine_test( } ) +-- A migrated lock whose capacity was never read (pinUsersSupported and +-- totalUsersSupported both unset, as when the lock does not answer +-- UsersNumberGet and lockCodes.maxCodes is not in the hub's state) must not be +-- held to an assumed limit: the lock is the authority on whether it is full. +local function numbered_entries(count) + local users, credentials = {}, {} + for i = 1, count do + table.insert(users, { userIndex = i, userName = "User " .. i, userType = "guest" }) + table.insert(credentials, { userIndex = i, credentialIndex = i, credentialType = "pin", credentialName = "User " .. i }) + end + return users, credentials +end + +test.register_coroutine_test( + "addCredential: sends the code to the lock past 20 entries when the lock's capacity is unknown", + function() + local users, credentials = numbered_entries(20) + seed_users(users) + seed_credentials(credentials) + + test.socket.capability:__queue_receive({ + mock_device.id, + { capability = capabilities.lockCredentials.ID, command = "addCredential", + args = { 0, "guest", "pin", "1234" } }, + }) + expect_set_pin_code(21, "1234") + test.wait_for_events() + + queue_user_code_added(21) + table.insert(credentials, { userIndex = 21, credentialIndex = 21, credentialType = "pin", credentialName = "Guest 21" }) + table.insert(users, { userIndex = 21, userName = "Guest 21", userType = "guest" }) + test.socket.capability:__expect_send( + mock_device:generate_test_message("main", + capabilities.lockCredentials.credentials(credentials, { visibility = { displayed = false } }))) + test.socket.capability:__expect_send( + mock_device:generate_test_message("main", + capabilities.lockUsers.users(users, { visibility = { displayed = false } }))) + test.socket.capability:__expect_send( + mock_device:generate_test_message("main", + capabilities.lockCredentials.commandResult( + { commandName = "addCredential", statusCode = "success", userIndex = 21, credentialIndex = 21 }, + { state_change = true, visibility = { displayed = false } } + )) + ) + test.wait_for_events() + end, + { + min_api_version = 15 + } +) + +test.register_coroutine_test( + "addCredential: emits resourceExhausted when the lock's reported capacity is reached", + function() + local users, credentials = numbered_entries(20) + seed_users(users) + seed_credentials(credentials) + test.socket.zwave:__queue_receive({ mock_device.id, UserCode:UsersNumberReport({ supported_users = 20 }) }) + test.socket.capability:__expect_send(mock_device:generate_test_message("main", + capabilities.lockUsers.totalUsersSupported(20, { state_change = true, visibility = { displayed = false } }))) + test.socket.capability:__expect_send(mock_device:generate_test_message("main", + capabilities.lockCredentials.pinUsersSupported(20, { state_change = true, visibility = { displayed = false } }))) + test.wait_for_events() + + test.socket.capability:__queue_receive({ + mock_device.id, + { capability = capabilities.lockCredentials.ID, command = "addCredential", + args = { 0, "guest", "pin", "1234" } }, + }) + test.socket.capability:__expect_send( + mock_device:generate_test_message("main", + capabilities.lockCredentials.commandResult( + { commandName = "addCredential", statusCode = "resourceExhausted" }, + { state_change = true, visibility = { displayed = false } } + )) + ) + test.wait_for_events() + end, + { + min_api_version = 15 + } +) + -- ============================================================================ -- updateCredential -- ============================================================================ diff --git a/drivers/SmartThings/zwave-lock/src/test/test_lock_programming_code.lua b/drivers/SmartThings/zwave-lock/src/test/test_lock_programming_code.lua index 32529699e3..1a8372fbb8 100644 --- a/drivers/SmartThings/zwave-lock/src/test/test_lock_programming_code.lua +++ b/drivers/SmartThings/zwave-lock/src/test/test_lock_programming_code.lua @@ -305,7 +305,16 @@ test.register_coroutine_test( } ) --- the default limit is 20 when the lock has not reported one +-- the lock reports a limit of 20 user slots +local function report_user_limit(device, limit) + test.socket.zwave:__queue_receive({ device.id, UserCode:UsersNumberReport({ supported_users = limit }) }) + test.socket.capability:__expect_send(device:generate_test_message("main", + capabilities.lockUsers.totalUsersSupported(limit, { state_change = true, visibility = { displayed = false } }))) + test.socket.capability:__expect_send(device:generate_test_message("main", + capabilities.lockCredentials.pinUsersSupported(limit, { state_change = true, visibility = { displayed = false } }))) + test.wait_for_events() +end + local function seed_guests_and_programming_code(device, guest_count) local users, credentials = { PROGRAMMING_USER_ENTRY }, { PROGRAMMING_CREDENTIAL } for i = 1, guest_count do @@ -318,6 +327,7 @@ end test.register_coroutine_test( "addCredential: the programming code does not count toward the credential limit", function() + report_user_limit(yale_mock_device, 20) seed_guests_and_programming_code(yale_mock_device, 19) test.socket.capability:__queue_receive({ yale_mock_device.id, { capability = capabilities.lockCredentials.ID, command = "addCredential", args = { 0, "guest", "pin", "1234" } } }) @@ -337,6 +347,7 @@ test.register_coroutine_test( test.register_coroutine_test( "addCredential: a full lock is still full with a programming code", function() + report_user_limit(yale_mock_device, 20) seed_guests_and_programming_code(yale_mock_device, 20) test.socket.capability:__queue_receive({ yale_mock_device.id, { capability = capabilities.lockCredentials.ID, command = "addCredential", args = { 0, "guest", "pin", "1234" } } }) diff --git a/drivers/SmartThings/zwave-lock/src/test/test_lock_tables.lua b/drivers/SmartThings/zwave-lock/src/test/test_lock_tables.lua index 63f846cf49..3464768a98 100644 --- a/drivers/SmartThings/zwave-lock/src/test/test_lock_tables.lua +++ b/drivers/SmartThings/zwave-lock/src/test/test_lock_tables.lua @@ -167,6 +167,7 @@ test.register_coroutine_test( test.register_coroutine_test( "add_entry: returns RESOURCE_EXHAUSTED when table is at max capacity", function() + mock_latest_state[mock_state_key("main", capabilities.lockUsers.ID, capabilities.lockUsers.totalUsersSupported.NAME)] = 20 local entries = {} for i = 1, 20 do entries[i] = { userIndex = i, userType = "guest", userName = "User" .. i }