Skip to content

Commit b01aa05

Browse files
authored
Merge pull request #3382 from SmartThingsCommunity/main
Hotfix Main -> Beta 11/24/26
2 parents dbe8d96 + b01c9e1 commit b01aa05

3 files changed

Lines changed: 168 additions & 34 deletions

File tree

‎drivers/SmartThings/matter-lock/src/init.lua‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,8 @@ end
12811281

12821282
-- An error status for GetUser carries the path of the request command, not GetUserResponse.
12831283
local function get_user_error_handler(driver, device, ib, response)
1284+
-- A successful GetUser sometimes reports a SUCCESS status for the command alongside the GetUserResponse.
1285+
if ib.status == im.InteractionResponse.Status.SUCCESS then return end
12841286
if lock_sync.is_syncing(device) then
12851287
lock_sync.get_user_response_handler(device, ib)
12861288
end
@@ -1291,9 +1293,10 @@ local function get_user_response_handler(driver, device, ib, response)
12911293
lock_sync.get_user_response_handler(device, ib)
12921294
return
12931295
end
1296+
local cmdName = device:get_field(lock_utils.COMMAND_NAME)
1297+
if cmdName ~= "addUser" then return end
12941298
local elements = ib.info_block.data.elements
12951299
local userIdx = elements.user_index.value
1296-
local cmdName = device:get_field(lock_utils.COMMAND_NAME)
12971300
local status = "success"
12981301
if ib.status == DoorLock.types.DlStatus.FAILURE then
12991302
status = "failure"

‎drivers/SmartThings/matter-lock/src/lock_sync.lua‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ local function merge_users(device, reported_users)
106106
for i, user in ipairs(users) do
107107
local st_user = st_user_for[i]
108108
read_user_indices[user.userIndex] = true
109-
-- the stored/legacy name, else the name held by the lock, else a name made from the user index
110-
local user_name = st_user and st_user.userName or user.userName or string.format("Guest %d", user.userIndex)
109+
-- the stored/legacy name, else a name made from the user index
110+
local user_name = st_user and st_user.userName or string.format("Guest %d", user.userIndex)
111111
table.insert(user_table, {
112112
userIndex = user.userIndex,
113113
userName = user_name,
@@ -269,11 +269,9 @@ function sync.get_user_response_handler(device, ib)
269269
table.insert(pins, fields.credential_index.value)
270270
end
271271
end
272-
local user_name = elements.user_name and elements.user_name.value
273272
local reported_users = utils.deep_copy(device:get_field(REPORTED_USERS) or {})
274273
table.insert(reported_users, {
275274
userIndex = user_index,
276-
userName = (user_name ~= nil and user_name ~= "") and user_name or nil,
277275
pins = pins,
278276
remoteOnly = elements.user_type ~= nil and elements.user_type.value == clusters.DoorLock.types.UserTypeEnum.REMOTE_ONLY_USER,
279277
})

‎drivers/SmartThings/matter-lock/src/test/test_matter_lock_user_sync.lua‎

Lines changed: 162 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ test.register_coroutine_test(
128128
)})
129129
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
130130
capabilities.lockUsers.users({
131-
{userIndex = 1, userName = "Alice", userType = "guest"},
131+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
132132
{userIndex = 4, userName = "Guest 4", userType = "guest"},
133133
}, {visibility = {displayed = false}})
134134
))
135135
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
136136
capabilities.lockCredentials.credentials({
137-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
138-
{userIndex = 1, credentialIndex = 5, credentialType = "pin", credentialName = "Alice"},
137+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
138+
{userIndex = 1, credentialIndex = 5, credentialType = "pin", credentialName = "Guest 1"},
139139
{userIndex = 4, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 4"},
140140
}, {visibility = {displayed = false}})
141141
))
@@ -221,12 +221,12 @@ test.register_coroutine_test(
221221
)})
222222
expect_tables(
223223
{
224-
{userIndex = 1, userName = "Alice", userType = "guest"},
225-
{userIndex = 4, userName = "Dan", userType = "guest"},
224+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
225+
{userIndex = 4, userName = "Guest 4", userType = "guest"},
226226
},
227227
{
228-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
229-
{userIndex = 4, credentialIndex = 2, credentialType = "pin", credentialName = "Dan"},
228+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
229+
{userIndex = 4, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 4"},
230230
}
231231
)
232232
test.wait_for_events()
@@ -248,12 +248,12 @@ test.register_coroutine_test(
248248
test.socket.matter:__queue_receive({mock_device.id, get_user_response(3, "Bob", OCCUPIED, UNRESTRICTED, {pin(2)}, nil)})
249249
expect_tables(
250250
{
251-
{userIndex = 1, userName = "Alice", userType = "guest"},
252-
{userIndex = 3, userName = "Bob", userType = "guest"},
251+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
252+
{userIndex = 3, userName = "Guest 3", userType = "guest"},
253253
},
254254
{
255-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
256-
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Bob"},
255+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
256+
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 3"},
257257
}
258258
)
259259
test.wait_for_events()
@@ -273,12 +273,12 @@ test.register_coroutine_test(
273273
-- Bob is still in the tables
274274
expect_tables(
275275
{
276-
{userIndex = 1, userName = "Alice", userType = "guest"},
277-
{userIndex = 3, userName = "Bob", userType = "guest"},
276+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
277+
{userIndex = 3, userName = "Guest 3", userType = "guest"},
278278
},
279279
{
280-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
281-
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Bob"},
280+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
281+
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 3"},
282282
}
283283
)
284284
test.wait_for_events()
@@ -306,8 +306,8 @@ test.register_coroutine_test(
306306
13, "", types.UserStatusEnum.OCCUPIED_ENABLED, types.UserTypeEnum.REMOTE_ONLY_USER, {pin(12)}, nil
307307
)})
308308
expect_tables(
309-
{{userIndex = 1, userName = "Alice", userType = "guest"}},
310-
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"}}
309+
{{userIndex = 1, userName = "Guest 1", userType = "guest"}},
310+
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"}}
311311
)
312312
test.wait_for_events()
313313
end,
@@ -353,12 +353,12 @@ test.register_coroutine_test(
353353
test.socket.matter:__queue_receive({mock_device.id, get_user_response(3, "Bob", OCCUPIED, UNRESTRICTED, {pin(2)}, nil)})
354354
expect_tables(
355355
{
356-
{userIndex = 1, userName = "Alice", userType = "guest"},
357-
{userIndex = 3, userName = "Bob", userType = "guest"},
356+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
357+
{userIndex = 3, userName = "Guest 3", userType = "guest"},
358358
},
359359
{
360-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
361-
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Bob"},
360+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
361+
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 3"},
362362
}
363363
)
364364
test.wait_for_events()
@@ -433,8 +433,8 @@ test.register_coroutine_test(
433433
-- user 3 does not answer in time
434434
test.mock_time.advance_time(10)
435435
expect_tables(
436-
{{userIndex = 1, userName = "Alice", userType = "guest"}},
437-
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"}}
436+
{{userIndex = 1, userName = "Guest 1", userType = "guest"}},
437+
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"}}
438438
)
439439
test.wait_for_events()
440440
assert(mock_device:get_field(lock_sync.USERS_SYNCED) == nil, "sync should not be complete")
@@ -445,12 +445,12 @@ test.register_coroutine_test(
445445
test.socket.matter:__queue_receive({mock_device.id, get_user_response(3, "Bob", OCCUPIED, UNRESTRICTED, {pin(2)}, nil)})
446446
expect_tables(
447447
{
448-
{userIndex = 1, userName = "Alice", userType = "guest"},
449-
{userIndex = 3, userName = "Bob", userType = "guest"},
448+
{userIndex = 1, userName = "Guest 1", userType = "guest"},
449+
{userIndex = 3, userName = "Guest 3", userType = "guest"},
450450
},
451451
{
452-
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"},
453-
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Bob"},
452+
{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"},
453+
{userIndex = 3, credentialIndex = 2, credentialType = "pin", credentialName = "Guest 3"},
454454
}
455455
)
456456
test.wait_for_events()
@@ -502,6 +502,8 @@ local function store_tables_like_old_migration(codes)
502502
table.insert(users, {userIndex = slot, userName = name, userType = "guest"})
503503
table.insert(creds, {userIndex = slot, credentialIndex = slot, credentialType = "pin", credentialName = name})
504504
end
505+
-- the lock's user names are not read, so the names come from the legacy codes, as in a real migration
506+
mock_device:set_field(lock_utils.LOCK_CODES_FOR_MIGRATION, codes, {persist = true})
505507
sync_with_lock(lock_users, users, creds)
506508
mock_device:set_field(lock_sync.USERS_SYNCED, nil, {persist = true})
507509
end
@@ -581,8 +583,8 @@ test.register_coroutine_test(
581583
{index = 1, name = "Alice", pins = {1}},
582584
{index = 2, type = REMOTE_ONLY, pins = {2}}, -- the driver's COTA user
583585
},
584-
{{userIndex = 1, userName = "Alice", userType = "guest"}},
585-
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Alice"}}
586+
{{userIndex = 1, userName = "Guest 1", userType = "guest"}},
587+
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"}}
586588
)
587589
end,
588590
{ min_api_version = 15 }
@@ -626,6 +628,137 @@ test.register_coroutine_test(
626628
{ min_api_version = 15 }
627629
)
628630

631+
test.register_coroutine_test(
632+
"The SUCCESS status that accompanies a GetUserResponse does not advance the sync a second time",
633+
function()
634+
set_total_users_supported(3)
635+
refresh_and_wait_for_sync()
636+
expect_get_user(1)
637+
test.wait_for_events()
638+
639+
test.socket.matter:__queue_receive({mock_device.id, DoorLock:build_test_command_response(
640+
mock_device, 1, DoorLock.ID, DoorLock.server.commands.GetUser.ID, nil, im.InteractionResponse.Status.SUCCESS)})
641+
test.wait_for_events() -- nothing is sent
642+
643+
test.socket.matter:__queue_receive({mock_device.id, get_user_response(
644+
1, "Alice", types.UserStatusEnum.OCCUPIED_ENABLED, types.UserTypeEnum.UNRESTRICTED_USER, {pin(1)}, 2
645+
)})
646+
expect_get_user(2)
647+
test.wait_for_events()
648+
end,
649+
{ min_api_version = 15 }
650+
)
651+
652+
test.register_coroutine_test(
653+
"A GetUserResponse received when no addUser or sync is in progress does not send SetUser",
654+
function()
655+
test.socket.matter:__queue_receive({mock_device.id, get_user_response(
656+
10, nil, types.UserStatusEnum.AVAILABLE, nil, nil, nil
657+
)})
658+
test.wait_for_events() -- nothing is sent or emitted
659+
end,
660+
{ min_api_version = 15 }
661+
)
662+
663+
-- The replies below are copied from an ULTRALOQ Smart Door Lock on a production hub.
664+
-- A successful GetUser returns two blocks in one message: the GetUserResponse, then a SUCCESS status on
665+
-- the GetUser command path. User 1's name is not valid UTF-8, users 3 and 6 are NUL-padded to 10 bytes,
666+
-- user 6 holds only a programming PIN, and user 6 reports no NextUserIndex.
667+
local ULTRALOQ_USERS = {
668+
{index = 1, name = "\xC4\x0E\x02\x20\x00\x00\x00\x00\xA4\x0E", type = types.UserTypeEnum.UNRESTRICTED_USER, pin = 1, next = 2},
669+
{index = 2, name = "Brian", type = types.UserTypeEnum.SCHEDULE_RESTRICTED_USER, pin = 2, next = 3},
670+
{index = 3, name = "Alex\x00", type = types.UserTypeEnum.SCHEDULE_RESTRICTED_USER, pin = 3, next = 4},
671+
{index = 4, name = "Hudson ", type = types.UserTypeEnum.SCHEDULE_RESTRICTED_USER, pin = 4, next = 5},
672+
{index = 5, name = "Dog", type = types.UserTypeEnum.SCHEDULE_RESTRICTED_USER, pin = 5, next = 6},
673+
{index = 6, name = "Nobody\x00", type = types.UserTypeEnum.SCHEDULE_RESTRICTED_USER, programming_pin = 0, next = nil},
674+
}
675+
676+
-- A GetUserResponse followed by the SUCCESS status block for the GetUser command, in one message.
677+
local function get_user_reply_as_sent_by_lock(user)
678+
local credentials
679+
if user.pin then
680+
credentials = {pin(user.pin)}
681+
else
682+
credentials = {types.CredentialStruct({
683+
credential_type = types.CredentialTypeEnum.PROGRAMMINGPIN, credential_index = user.programming_pin
684+
})}
685+
end
686+
local reply = get_user_response(user.index, user.name, OCCUPIED, user.type, credentials, user.next)
687+
local status_block = DoorLock:build_test_command_response(
688+
mock_device, 1, DoorLock.ID, DoorLock.server.commands.GetUser.ID, nil, im.InteractionResponse.Status.SUCCESS
689+
).info_blocks[1]
690+
table.insert(reply.info_blocks, status_block)
691+
return reply
692+
end
693+
694+
-- The datastore save JSON-encodes every persisted string, and the hub's encoder rejects invalid UTF-8.
695+
local function assert_utf8_only(value, path)
696+
if type(value) == "string" then
697+
assert(utf8.len(value) ~= nil, string.format("persisted string at %s is not valid UTF-8", path))
698+
elseif type(value) == "table" then
699+
for k, v in pairs(value) do
700+
assert_utf8_only(k, path .. "[key]")
701+
assert_utf8_only(v, path .. "." .. tostring(k))
702+
end
703+
end
704+
end
705+
706+
test.register_coroutine_test(
707+
"The ULTRALOQ's real GetUser replies sync every user and persist only valid UTF-8",
708+
function()
709+
set_total_users_supported(10)
710+
refresh_and_wait_for_sync()
711+
expect_get_user(1)
712+
test.wait_for_events()
713+
714+
for _, user in ipairs(ULTRALOQ_USERS) do
715+
test.socket.matter:__queue_receive({mock_device.id, get_user_reply_as_sent_by_lock(user)})
716+
if user.next then
717+
-- exactly one GetUser for the next index: the SUCCESS status block must not skip ahead
718+
expect_get_user(user.next)
719+
else
720+
local users, creds = {}, {}
721+
for _, u in ipairs(ULTRALOQ_USERS) do
722+
table.insert(users, {userIndex = u.index, userName = "Guest " .. u.index, userType = "guest"})
723+
if u.pin then
724+
table.insert(creds, {userIndex = u.index, credentialIndex = u.pin, credentialType = "pin", credentialName = "Guest " .. u.index})
725+
end
726+
end
727+
expect_tables(users, creds)
728+
end
729+
test.wait_for_events()
730+
assert_utf8_only(mock_device:get_field("syncReportedUsers"), "syncReportedUsers")
731+
end
732+
assert(mock_device:get_field(lock_sync.USERS_SYNCED) == lock_sync.VERSION, "sync should be complete")
733+
end,
734+
{ min_api_version = 15 }
735+
)
736+
737+
test.register_coroutine_test(
738+
"A late GetUserResponse for an empty index after the sync finishes does not add a user to the lock",
739+
function()
740+
-- left behind by an earlier command, as on the production hub
741+
mock_device:set_field(lock_utils.USER_NAME, "Melinda", {persist = true})
742+
set_total_users_supported(10)
743+
refresh_and_wait_for_sync()
744+
expect_get_user(1)
745+
test.wait_for_events()
746+
747+
local only_user = {index = 1, name = nil, type = types.UserTypeEnum.UNRESTRICTED_USER, pin = 1, next = nil}
748+
test.socket.matter:__queue_receive({mock_device.id, get_user_reply_as_sent_by_lock(only_user)})
749+
expect_tables(
750+
{{userIndex = 1, userName = "Guest 1", userType = "guest"}},
751+
{{userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1"}}
752+
)
753+
test.wait_for_events()
754+
755+
-- the lock's answer for an empty index carries only the user index; every other field is null
756+
test.socket.matter:__queue_receive({mock_device.id, get_user_response(10, nil, nil, nil, nil, nil)})
757+
test.wait_for_events() -- no SetUser is sent and no commandResult is emitted
758+
end,
759+
{ min_api_version = 15 }
760+
)
761+
629762
-- A device paired with the "lock" profile, which has no lockUsers, as a newly paired lock has before
630763
-- match_profile runs.
631764
local new_lock = test.mock_device.build_test_matter_device({

0 commit comments

Comments
 (0)