Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- ============================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" } } })
Expand All @@ -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" } } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down