@@ -15,13 +15,15 @@ local table_utils = {}
1515-- match_key Key used to identify entries in flat tables
1616-- required_keys Keys that must be non-nil when adding an entry
1717-- persistent_field device:set_field key used to back up the table across restarts
18+ -- minimum Lowest valid match_key value; next_index() begins its search here
1819--
1920
2021local DEFS = {
2122 users = {
2223 capability = capabilities .lockUsers ,
2324 attribute = capabilities .lockUsers .users ,
2425 max_entries = capabilities .lockUsers .totalUsersSupported ,
26+ minimum = 1 ,
2527 match_key = " userIndex" ,
2628 required_keys = {" userIndex" , " userType" },
2729 persistent_field = " persistedUsers" ,
@@ -30,6 +32,9 @@ local DEFS = {
3032 capability = capabilities .lockCredentials ,
3133 attribute = capabilities .lockCredentials .credentials ,
3234 max_entries = capabilities .lockCredentials .pinUsersSupported ,
35+ -- Z-Wave User Identifiers are a sequence starting at 1; 0 is reserved to mean "all users".
36+ -- This differs from the lockCredentials capability, which allows a credentialIndex of 0.
37+ minimum = 1 ,
3338 match_key = " credentialIndex" ,
3439 required_keys = {" userIndex" , " credentialIndex" , " credentialType" },
3540 persistent_field = " persistedCredentials" ,
@@ -128,15 +133,15 @@ function table_utils.find_all_entries_by(device, table_name, key, value)
128133 return matches
129134end
130135
131- -- Return the lowest positive integer not yet used as the match_key in the named table.
132- -- Used to auto-assign the next available slot for a new entry.
136+ -- Return the lowest integer, starting at the table's minimum, not yet used as the
137+ -- match_key in the named table. Used to auto-assign the next available slot for a new entry.
133138function table_utils .next_index (device , table_name )
134139 local def = resolve_table_def (device , table_name )
135140 if not def then return 1 end
136141 local t = table_utils .get_state (device , def ) or {}
137142 local occupied = {}
138143 for _ , entry in ipairs (t ) do occupied [entry [def .match_key ]] = true end
139- local idx = 1
144+ local idx = def . minimum
140145 while occupied [idx ] do idx = idx + 1 end
141146 return idx
142147end
0 commit comments