Skip to content

Commit b913a44

Browse files
authored
Delete guest user when SetYearDaySchedule is failed (#2876)
Signed-off-by: Hunsup Jung <hunsup.jung@samsung.com>
1 parent c3d05cf commit b913a44

2 files changed

Lines changed: 173 additions & 22 deletions

File tree

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

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,15 +1554,29 @@ local function clear_user_response_handler(driver, device, ib, response)
15541554
device.log.warn(string.format("Failed to clear user: %s", status))
15551555
end
15561556

1557-
-- Update commandResult
1558-
local command_result_info = {
1559-
commandName = cmdName,
1560-
userIndex = userIdx,
1561-
statusCode = status
1562-
}
1563-
device:emit_event(capabilities.lockUsers.commandResult(
1564-
command_result_info, {state_change = true, visibility = {displayed = false}}
1565-
))
1557+
-- This occurs in the "defaultSchedule" command failure path, when a guest user's credentials are set but
1558+
-- the scheduling fails during default setup. In this case, those set credentials should be removed, and we
1559+
-- wait to log lock credentials (note: as a "failure", though it technically succeeded) until here.
1560+
if cmdName == "defaultSchedule" then
1561+
local command_result_info = {
1562+
commandName = "addCredential",
1563+
userIndex = userIdx,
1564+
statusCode = "failure"
1565+
}
1566+
device:emit_event(capabilities.lockCredentials.commandResult(
1567+
command_result_info, {state_change = true, visibility = {displayed = false}}
1568+
))
1569+
device:set_field(lock_utils.BUSY_STATE, false, {persist = true})
1570+
else
1571+
local command_result_info = {
1572+
commandName = cmdName,
1573+
userIndex = userIdx,
1574+
statusCode = status
1575+
}
1576+
device:emit_event(capabilities.lockUsers.commandResult(
1577+
command_result_info, {state_change = true, visibility = {displayed = false}}
1578+
))
1579+
end
15661580
device:set_field(lock_utils.BUSY_STATE, false, {persist = true})
15671581
end
15681582

@@ -2418,17 +2432,22 @@ local function set_year_day_schedule_handler(driver, device, ib, response)
24182432
local cmdName = "addCredential"
24192433
local credIdx = device:get_field(lock_utils.CRED_INDEX)
24202434

2421-
-- Update commandResult
2422-
local command_result_info = {
2423-
commandName = cmdName,
2424-
userIndex = userIdx,
2425-
credentialIndex = credIdx,
2426-
statusCode = status
2427-
}
2428-
device:emit_event(capabilities.lockCredentials.commandResult(
2429-
command_result_info, {state_change = true, visibility = {displayed = false}}
2430-
))
2431-
device:set_field(lock_utils.BUSY_STATE, false, {persist = true})
2435+
if status == "success" then
2436+
-- Update commandResult
2437+
local command_result_info = {
2438+
commandName = cmdName,
2439+
userIndex = userIdx,
2440+
credentialIndex = credIdx,
2441+
statusCode = status
2442+
}
2443+
device:emit_event(capabilities.lockCredentials.commandResult(
2444+
command_result_info, {state_change = true, visibility = {displayed = false}}
2445+
))
2446+
device:set_field(lock_utils.BUSY_STATE, false, {persist = true})
2447+
else
2448+
local ep = find_default_endpoint(device, clusters.DoorLock.ID)
2449+
device:send(DoorLock.server.commands.ClearUser(device, ep, userIdx))
2450+
end
24322451
return
24332452
end
24342453

drivers/SmartThings/matter-lock/src/test/test_new_matter_lock.lua

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ local mock_device = test.mock_device.build_test_matter_device({
3535
cluster_id = DoorLock.ID,
3636
cluster_type = "SERVER",
3737
cluster_revision = 1,
38-
feature_map = 0x0181, -- PIN & USR & COTA
38+
feature_map = 0x0591, -- PIN & WDSCH & USR & COTA & YDSCH
3939
}
4040
},
4141
device_types = {
@@ -76,7 +76,7 @@ local function test_init()
7676
test.socket.capability:__expect_send(
7777
mock_device:generate_test_message("main", capabilities.lock.supportedLockCommands({"lock", "unlock"}, {visibility = {displayed = false}}))
7878
)
79-
mock_device:expect_metadata_update({ profile = "lock-user-pin" })
79+
mock_device:expect_metadata_update({ profile = "lock-user-pin-schedule" })
8080
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
8181
end
8282

@@ -2100,4 +2100,136 @@ test.register_coroutine_test(
21002100
}
21012101
)
21022102

2103+
-- mock_device:set_field(lock_utils.COTA_CRED, "654123", {persist = true}) --overwrite random cred for test expectation
2104+
test.register_coroutine_test(
2105+
"Add Guest User and failure response ",
2106+
function()
2107+
test.socket.capability:__queue_receive(
2108+
{
2109+
mock_device.id,
2110+
{
2111+
capability = capabilities.lockCredentials.ID,
2112+
command = "addCredential",
2113+
args = {0, "guest", "pin", "654123"}
2114+
},
2115+
}
2116+
)
2117+
test.socket.matter:__expect_send(
2118+
{
2119+
mock_device.id,
2120+
DoorLock.server.commands.SetCredential(
2121+
mock_device, 1, -- endpoint
2122+
DoorLock.types.DataOperationTypeEnum.ADD, -- operation_type
2123+
DoorLock.types.CredentialStruct(
2124+
{credential_type = DoorLock.types.CredentialTypeEnum.PIN, credential_index = 1}
2125+
), -- credential
2126+
"654123", -- credential_data
2127+
nil, -- user_index
2128+
nil, -- user_status
2129+
DoorLock.types.UserTypeEnum.SCHEDULE_RESTRICTED_USER -- user_type
2130+
),
2131+
}
2132+
)
2133+
test.wait_for_events()
2134+
test.socket.matter:__queue_receive(
2135+
{
2136+
mock_device.id,
2137+
DoorLock.client.commands.SetCredentialResponse:build_test_command_response(
2138+
mock_device, 1,
2139+
DoorLock.types.DlStatus.SUCCESS, -- status
2140+
1, -- user_index
2141+
2 -- next_credential_index
2142+
),
2143+
}
2144+
)
2145+
test.socket.capability:__expect_send(
2146+
mock_device:generate_test_message(
2147+
"main",
2148+
capabilities.lockUsers.users({{userIndex = 1, userType = "guest"}}, {visibility={displayed=false}})
2149+
)
2150+
)
2151+
test.socket.capability:__expect_send(
2152+
mock_device:generate_test_message(
2153+
"main",
2154+
capabilities.lockCredentials.credentials(
2155+
{{credentialIndex=1, credentialType="pin", userIndex=1}}, {visibility={displayed=false}}
2156+
)
2157+
)
2158+
)
2159+
test.socket.matter:__expect_send(
2160+
{
2161+
mock_device.id,
2162+
DoorLock.server.commands.SetYearDaySchedule(
2163+
mock_device, 1, -- endpoint
2164+
1, -- year_day_index
2165+
1, -- user_index
2166+
0, -- local_start_time
2167+
0xffffffff -- local_end_time
2168+
),
2169+
}
2170+
)
2171+
test.wait_for_events()
2172+
test.socket.matter:__queue_receive(
2173+
{
2174+
mock_device.id,
2175+
DoorLock.server.commands.SetYearDaySchedule:build_test_command_response(
2176+
mock_device, 1,
2177+
DoorLock.types.DlStatus.FAILURE -- status
2178+
),
2179+
}
2180+
)
2181+
test.socket.matter:__expect_send({
2182+
mock_device.id,
2183+
DoorLock.server.commands.ClearUser(
2184+
mock_device, 1,
2185+
1
2186+
)
2187+
})
2188+
test.wait_for_events()
2189+
test.socket.matter:__queue_receive(
2190+
{
2191+
mock_device.id,
2192+
DoorLock.server.commands.ClearUser:build_test_command_response(
2193+
mock_device, 1
2194+
),
2195+
}
2196+
)
2197+
test.socket.capability:__expect_send(
2198+
mock_device:generate_test_message(
2199+
"main",
2200+
capabilities.lockUsers.users({}, {visibility={displayed=false}})
2201+
)
2202+
)
2203+
test.socket.capability:__expect_send(
2204+
mock_device:generate_test_message(
2205+
"main",
2206+
capabilities.lockCredentials.credentials({}, {visibility={displayed=false}})
2207+
)
2208+
)
2209+
test.socket.capability:__expect_send(
2210+
mock_device:generate_test_message(
2211+
"main",
2212+
capabilities.lockSchedules.weekDaySchedules({}, {visibility={displayed=false}})
2213+
)
2214+
)
2215+
test.socket.capability:__expect_send(
2216+
mock_device:generate_test_message(
2217+
"main",
2218+
capabilities.lockSchedules.yearDaySchedules({}, {visibility={displayed=false}})
2219+
)
2220+
)
2221+
test.socket.capability:__expect_send(
2222+
mock_device:generate_test_message(
2223+
"main",
2224+
capabilities.lockCredentials.commandResult(
2225+
{commandName="addCredential", statusCode="failure", userIndex=1}, {state_change=true, visibility={displayed=false}}
2226+
)
2227+
)
2228+
)
2229+
end,
2230+
{
2231+
min_api_version = 17
2232+
}
2233+
)
2234+
21032235
test.run_registered_tests()

0 commit comments

Comments
 (0)