Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions drivers/SmartThings/zwave-lock/src/schlage-lock/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,28 @@ local function is_user_code_report_mfr_specific(device, cmd)
end
end

-- Schlage confirms a Set by reporting the slot as occupied with a masked ("**********") or blank code, which
-- is_user_code_report_mfr_specific would otherwise consume. While an add or update is waiting on that slot,
-- the report is the lock's confirmation of the new PIN.
local function is_set_confirmed(device, cmd)
local command_in_progress = device:get_field(consts.DRIVER_STATE.COMMAND_IN_PROGRESS)
if command_in_progress ~= consts.LOCK_CREDENTIALS.ADD and command_in_progress ~= consts.LOCK_CREDENTIALS.UPDATE then
return false
end
local credential_args = device:get_field(consts.DRIVER_STATE.CREDENTIAL_ARGS_IN_USE) or {}
return cmd.args.user_id_status == user_id_status.ENABLED_GRANT_ACCESS and
cmd.args.user_identifier == credential_args.credentialIndex
end

local function user_code_report_handler(self, device, cmd)
if device:get_field(consts.DRIVER_STATE.COMMAND_IN_PROGRESS) == consts.SYNC.CODES_FROM_LOCK then
zwave_handlers.user_code_report(self, device, cmd)
return
end
if is_set_confirmed(device, cmd) then
zwave_handlers.user_code_report(self, device, cmd)
return
end
local credential_index = cmd.args.user_identifier
if is_user_code_report_mfr_specific(device, cmd) then
local reported_user_id_status = cmd.args.user_id_status
Expand Down
117 changes: 117 additions & 0 deletions drivers/SmartThings/zwave-lock/src/test/test_schlage_lock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,121 @@ test.register_coroutine_test(
{ min_api_version = 15 }
)

-- The lock's PIN length is known, so an add is sent at once instead of after a Configuration Get.
local PIN_LENGTH_FROM_DEVICE = "pinLengthFromDevice"

local function add_credential(pin)
test.socket.capability:__queue_receive({
mock_device.id,
{ capability = capabilities.lockCredentials.ID, command = "addCredential", args = { 0, "guest", "pin", pin } },
})
end

local function expect_set_pin_code(user_identifier, user_code)
test.socket.zwave:__expect_send(
UserCode:Set({
user_identifier = user_identifier,
user_code = user_code,
user_id_status = UserCode.user_id_status.ENABLED_GRANT_ACCESS,
}):build_test_tx(mock_device.id)
)
end

local function queue_masked_occupied_report(user_identifier)
test.socket.zwave:__queue_receive({
mock_device.id,
UserCode:Report({
user_identifier = user_identifier,
user_id_status = UserCode.user_id_status.ENABLED_GRANT_ACCESS,
user_code = "**********",
}),
})
end

local function expect_command_result(command_name, status_code, extra)
local result = { commandName = command_name, statusCode = status_code }
for k, v in pairs(extra or {}) do result[k] = v end
test.socket.capability:__expect_send(
mock_device:generate_test_message("main",
capabilities.lockCredentials.commandResult(result, { state_change = true, visibility = { displayed = false } }))
)
end

local function expect_guest_1_added()
test.socket.capability:__expect_send(
mock_device:generate_test_message("main",
capabilities.lockCredentials.credentials(
{ { userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1" } },
{ visibility = { displayed = false } }
))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main",
capabilities.lockUsers.users(
{ { userIndex = 1, userName = "Guest 1", userType = "guest" } },
{ visibility = { displayed = false } }
))
)
expect_command_result("addCredential", "success", { userIndex = 1, credentialIndex = 1 })
end

test.register_coroutine_test(
"addCredential: a masked occupied report for the new slot confirms the add",
function()
mock_device:set_field(PIN_LENGTH_FROM_DEVICE, true, { persist = true })
add_credential("1234")
expect_set_pin_code(1, "1234")
test.wait_for_events()

queue_masked_occupied_report(1)
expect_guest_1_added()
test.wait_for_events()

-- slot 1 is now recorded, so the next add goes to slot 2 instead of overwriting it
add_credential("5678")
expect_set_pin_code(2, "5678")
test.wait_for_events()
end,
{ min_api_version = 15 }
)

test.register_coroutine_test(
"addCredential: a masked occupied report for another slot does not confirm the add",
function()
mock_device:set_field(PIN_LENGTH_FROM_DEVICE, true, { persist = true })
add_credential("1234")
expect_set_pin_code(1, "1234")
test.wait_for_events()

queue_masked_occupied_report(7)
test.wait_for_events()
end,
{ min_api_version = 15 }
)

test.register_coroutine_test(
"updateCredential: a masked occupied report for the slot confirms the update",
function()
mock_device:set_field(PIN_LENGTH_FROM_DEVICE, true, { persist = true })
add_credential("1234")
expect_set_pin_code(1, "1234")
test.wait_for_events()
queue_masked_occupied_report(1)
expect_guest_1_added()
test.wait_for_events()

test.socket.capability:__queue_receive({
mock_device.id,
{ capability = capabilities.lockCredentials.ID, command = "updateCredential", args = { 1, 1, "pin", "5678" } },
})
expect_set_pin_code(1, "5678")
test.wait_for_events()

queue_masked_occupied_report(1)
expect_command_result("updateCredential", "success", { userIndex = 1, credentialIndex = 1 })
test.wait_for_events()
end,
{ min_api_version = 15 }
)

test.run_registered_tests()
Loading