@@ -11,7 +11,8 @@ local table_utils = {}
1111--
1212-- capability SmartThings capability (used for device support checks and to get latest state)
1313-- attribute Capability attribute function (used to emit state)
14- -- max_entries Attribute of the capability that defines the maximum number of entries allowed in the table
14+ -- max_entries Optional. Attribute of the capability that defines the maximum number of entries
15+ -- allowed in the table. Tables without it are unbounded.
1516-- match_key Key used to identify entries in flat tables
1617-- required_keys Keys that must be non-nil when adding an entry
1718-- persistent_field device:set_field key used to back up the table across restarts
@@ -22,7 +23,6 @@ local DEFS = {
2223 users = {
2324 capability = capabilities .lockUsers ,
2425 attribute = capabilities .lockUsers .users ,
25- max_entries = capabilities .lockUsers .totalUsersSupported ,
2626 minimum = 1 ,
2727 match_key = " userIndex" ,
2828 required_keys = {" userIndex" , " userType" },
@@ -131,15 +131,16 @@ function table_utils.find_all_entries_by(device, table_name, key, value)
131131 return matches
132132end
133133
134- -- Return the lowest positive integer not yet used as the match_key in the named table.
135- -- Used to auto-assign the next available slot for a new entry.
136- function table_utils .next_index (device , table_name )
134+ -- Return the lowest integer, starting at the table's minimum, not yet used as the
135+ -- match_key in the named table. Used to auto-assign the next available slot for a new entry.
136+ -- An optional `start` overrides the table's minimum, e.g. to skip a reserved slot.
137+ function table_utils .next_index (device , table_name , start )
137138 local def = resolve_table_def (device , table_name )
138- if not def then return 1 end
139+ assert ( def , string.format ( " table_helpers: unknown table %q " , tostring ( table_name )))
139140 local t = table_utils .get_state (device , def ) or {}
140141 local occupied = {}
141142 for _ , entry in ipairs (t ) do occupied [entry [def .match_key ]] = true end
142- local idx = 1
143+ local idx = start or def . minimum
143144 while occupied [idx ] do idx = idx + 1 end
144145 return idx
145146end
@@ -150,9 +151,10 @@ function table_utils.get_minimum(device, table_name)
150151 return def .minimum
151152end
152153
154+ -- Returns nil when the table has no limit.
153155function table_utils .get_max_entries (device , table_name )
154156 local def = resolve_table_def (device , table_name )
155- if not def then return end
157+ if not def or not def . max_entries then return end
156158 return device :get_latest_state (" main" , def .capability .ID , def .max_entries .NAME , 20 ) -- arbitrary, default to 20 if the attribute is missing
157159end
158160
@@ -168,7 +170,8 @@ function table_utils.add_entry(device, table_name, entry)
168170 local t = table_utils .get_state (device , def )
169171 if not t then return COMMAND_RESULT .FAILURE end
170172
171- if # t >= table_utils .get_max_entries (device , table_name ) then
173+ local max_entries = table_utils .get_max_entries (device , table_name )
174+ if max_entries and # t >= max_entries then
172175 device .log .warn (string.format (
173176 " table_helpers: cannot add entry to %q, max entries reached" , table_name
174177 ))
0 commit comments