Skip to content

Commit b8ee72c

Browse files
committed
drop max entries concept from tables apis
1 parent 0d608dd commit b8ee72c

4 files changed

Lines changed: 22 additions & 44 deletions

File tree

‎drivers/SmartThings/zigbee-lock/src/lock_handlers/capabilities.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function CapabilityHandlers.add_credential(driver, device, command)
129129

130130
-- addCredential has no credentialIndex argument, so its userIndex argument selects the credential slot. A value
131131
-- of 0 means "auto-assign the next available slot". The user is always new, and takes the next available user slot.
132-
local cred_index = command.args.userIndex == 0 and tables.next_index(device, "credentials") or command.args.userIndex
132+
local cred_index = command.args.userIndex == 0 and tables.next_index(device, "credentials", 1) or command.args.userIndex
133133
local user_index = tables.next_index(device, "users")
134134

135135
if #(tables.get_state(device, "credentials") or {}) >= tables.get_max_entries(device, "credentials") then

‎drivers/SmartThings/zigbee-lock/src/lock_utils/tables.lua‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
132132
end
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
145146
end
@@ -150,9 +151,10 @@ function table_utils.get_minimum(device, table_name)
150151
return def.minimum
151152
end
152153

154+
-- Returns nil when the table has no limit.
153155
function 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
157159
end
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
))

‎drivers/SmartThings/zigbee-lock/src/test/test_lock_tables.lua‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ test.register_coroutine_test(
108108
)
109109

110110
test.register_coroutine_test(
111-
"add_entry: returns RESOURCE_EXHAUSTED when table is at max capacity",
111+
"add_entry: returns RESOURCE_EXHAUSTED when the credentials table is at max capacity",
112112
function()
113113
local entries = {}
114-
for i = 1, 20 do
115-
entries[i] = { userIndex = i, userType = "guest", userName = "User" .. i }
114+
for i = 0, 19 do
115+
entries[#entries + 1] = { userIndex = i + 1, credentialIndex = i, credentialType = "pin" }
116116
end
117-
seed_users(entries)
117+
seed_credentials(entries)
118118

119-
local overflow = { userIndex = 21, userType = "guest", userName = "Overflow" }
120-
local result = table_utils.add_entry(mock_device, "users", overflow)
119+
local overflow = { userIndex = 21, credentialIndex = 20, credentialType = "pin" }
120+
local result = table_utils.add_entry(mock_device, "credentials", overflow)
121121
assert(result == constants.COMMAND_RESULT.RESOURCE_EXHAUSTED,
122122
"Expected RESOURCE_EXHAUSTED, got: " .. tostring(result))
123123
end
@@ -513,11 +513,11 @@ test.register_coroutine_test(
513513
-- ===========================================================================
514514

515515
test.register_coroutine_test(
516-
"get_max_entries: returns the default of 20 when the attribute is not set",
516+
"get_max_entries: returns nil for the users table, which has no limit",
517517
function()
518518
local result = table_utils.get_max_entries(mock_device, "users")
519-
assert(result == 20,
520-
"Expected default max of 20, got: " .. tostring(result))
519+
assert(result == nil,
520+
"Expected no max, got: " .. tostring(result))
521521
end
522522
)
523523

‎drivers/SmartThings/zigbee-lock/src/test/test_lock_users_commands.lua‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -169,31 +169,6 @@ test.register_coroutine_test(
169169
end
170170
)
171171

172-
test.register_coroutine_test(
173-
"addUser: returns resourceExhausted when totalUsersSupported has been reached",
174-
function()
175-
set_total_users_supported(2)
176-
seed_users({
177-
{ userIndex = 1, userName = "Alice", userType = "guest" },
178-
{ userIndex = 2, userName = "Bob", userType = "guest" },
179-
})
180-
181-
test.socket.capability:__queue_receive({
182-
mock_device.id,
183-
{ capability = capabilities.lockUsers.ID, command = "addUser", args = { "Carol", "guest" } },
184-
})
185-
186-
test.socket.capability:__expect_send(
187-
mock_device:generate_test_message("main",
188-
capabilities.lockUsers.commandResult(
189-
{ commandName = "addUser", statusCode = "resourceExhausted" },
190-
{ state_change = true, visibility = { displayed = false } }
191-
))
192-
)
193-
test.wait_for_events()
194-
end
195-
)
196-
197172
-- ============================================================================
198173
-- updateUser
199174
-- ============================================================================

0 commit comments

Comments
 (0)