Skip to content
Closed
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
9 changes: 6 additions & 3 deletions drivers/SmartThings/zigbee-lock/src/lock_utils/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ local table_utils = {}
-- match_key Key used to identify entries in flat tables
-- required_keys Keys that must be non-nil when adding an entry
-- persistent_field device:set_field key used to back up the table across restarts
-- minimum Lowest valid match_key value; next_index() begins its search here
--

local DEFS = {
users = {
capability = capabilities.lockUsers,
attribute = capabilities.lockUsers.users,
max_entries = capabilities.lockUsers.totalUsersSupported,
minimum = 1,
match_key = "userIndex",
required_keys = {"userIndex", "userType"},
persistent_field = "persistedUsers",
Expand All @@ -30,6 +32,7 @@ local DEFS = {
capability = capabilities.lockCredentials,
attribute = capabilities.lockCredentials.credentials,
max_entries = capabilities.lockCredentials.pinUsersSupported,
minimum = 0,

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.

Before making this change, we also need to make sure that we sync the master codes from devices that already have them but either weren't present before migration or weren't synced when onboarding to the new lock experience. Otherwise the driver will see that slot 0 is available and we would have failures when writing to that index if that slot already exists but we just hadn't synced it to our capabilities yet.

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.

Agreed!

match_key = "credentialIndex",
required_keys = {"userIndex", "credentialIndex", "credentialType"},
persistent_field = "persistedCredentials",
Expand Down Expand Up @@ -128,15 +131,15 @@ function table_utils.find_all_entries_by(device, table_name, key, value)
return matches
end

-- Return the lowest positive integer not yet used as the match_key in the named table.
-- Used to auto-assign the next available slot for a new entry.
-- Return the lowest integer, starting at the table's minimum, not yet used as the
-- match_key in the named table. Used to auto-assign the next available slot for a new entry.
function table_utils.next_index(device, table_name)
local def = resolve_table_def(device, table_name)
if not def then return 1 end
local t = table_utils.get_state(device, def) or {}
local occupied = {}
for _, entry in ipairs(t) do occupied[entry[def.match_key]] = true end
local idx = 1
local idx = def.minimum
while occupied[idx] do idx = idx + 1 end
return idx
end
Expand Down
11 changes: 8 additions & 3 deletions drivers/SmartThings/zwave-lock/src/lock_utils/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ local table_utils = {}
-- match_key Key used to identify entries in flat tables
-- required_keys Keys that must be non-nil when adding an entry
-- persistent_field device:set_field key used to back up the table across restarts
-- minimum Lowest valid match_key value; next_index() begins its search here
--

local DEFS = {
users = {
capability = capabilities.lockUsers,
attribute = capabilities.lockUsers.users,
max_entries = capabilities.lockUsers.totalUsersSupported,
minimum = 1,
match_key = "userIndex",
required_keys = {"userIndex", "userType"},
persistent_field = "persistedUsers",
Expand All @@ -30,6 +32,9 @@ local DEFS = {
capability = capabilities.lockCredentials,
attribute = capabilities.lockCredentials.credentials,
max_entries = capabilities.lockCredentials.pinUsersSupported,
-- Z-Wave User Identifiers are a sequence starting at 1; 0 is reserved to mean "all users".
-- This differs from the lockCredentials capability, which allows a credentialIndex of 0.
minimum = 1,
match_key = "credentialIndex",
required_keys = {"userIndex", "credentialIndex", "credentialType"},
persistent_field = "persistedCredentials",
Expand Down Expand Up @@ -128,15 +133,15 @@ function table_utils.find_all_entries_by(device, table_name, key, value)
return matches
end

-- Return the lowest positive integer not yet used as the match_key in the named table.
-- Used to auto-assign the next available slot for a new entry.
-- Return the lowest integer, starting at the table's minimum, not yet used as the
-- match_key in the named table. Used to auto-assign the next available slot for a new entry.
function table_utils.next_index(device, table_name)
local def = resolve_table_def(device, table_name)
if not def then return 1 end
local t = table_utils.get_state(device, def) or {}
local occupied = {}
for _, entry in ipairs(t) do occupied[entry[def.match_key]] = true end
local idx = 1
local idx = def.minimum
while occupied[idx] do idx = idx + 1 end
return idx
end
Expand Down
Loading