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
22 changes: 21 additions & 1 deletion drivers/SmartThings/zwave-lock/src/schlage-lock/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local Association = (require "st.zwave.CommandClass.Association")({version=1})

local consts = require "lock_utils.constants"
local tables = require "lock_utils.tables"
local lock_utils = require "lock_utils.utils"
local zwave_handlers = require "lock_handlers.zwave_responses"
local cap_handlers = require "lock_handlers.capabilities"

Expand Down Expand Up @@ -70,11 +71,30 @@ local function is_user_code_report_mfr_specific(device, cmd)
end
end

-- Schlage refuses a PIN that is already in use by answering the Set with STATUS_NOT_AVAILABLE (and a code of
-- zeros) for the slot, and sends no notification. The slot keeps whatever it held before.
local function is_set_rejected(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.STATUS_NOT_AVAILABLE 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
local command_in_progress = device:get_field(consts.DRIVER_STATE.COMMAND_IN_PROGRESS)
if command_in_progress == consts.SYNC.CODES_FROM_LOCK then
zwave_handlers.user_code_report(self, device, cmd)
return
end
if is_set_rejected(device, cmd) then
lock_utils.emit_command_result(device, capabilities.lockCredentials, command_in_progress,
consts.COMMAND_RESULT.DUPLICATE)
lock_utils.clear_busy_state(device)
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
108 changes: 108 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,112 @@ 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 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

-- How the lock answers a Set it refuses because the PIN is already in use.
local function queue_set_rejected(user_identifier)
test.socket.zwave:__queue_receive({
mock_device.id,
UserCode:Report({
user_identifier = user_identifier,
user_id_status = UserCode.user_id_status.STATUS_NOT_AVAILABLE,
user_code = "0000\n\r",
}),
})
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 add_credential(pin)
test.socket.capability:__queue_receive({
mock_device.id,
{ capability = capabilities.lockCredentials.ID, command = "addCredential", args = { 0, "guest", "pin", pin } },
})
end

test.register_coroutine_test(
"addCredential: a STATUS_NOT_AVAILABLE answer for the new slot is a duplicate PIN, and nothing is added",
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_set_rejected(1)
expect_command_result("addCredential", "duplicate")
test.wait_for_events()

-- the driver is no longer busy, and slot 1 is still free
add_credential("5678")
expect_set_pin_code(1, "5678")
test.wait_for_events()
end,
{ min_api_version = 15 }
)

test.register_coroutine_test(
"updateCredential: a STATUS_NOT_AVAILABLE answer is a duplicate PIN, and the credential is unchanged",
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()

test.socket.zwave:__queue_receive({
mock_device.id,
UserCode:Report({
user_identifier = 1,
user_id_status = UserCode.user_id_status.ENABLED_GRANT_ACCESS,
user_code = "1234\n\r",
}),
})
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 })
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_set_rejected(1)
expect_command_result("updateCredential", "duplicate")
test.wait_for_events()
end,
{ min_api_version = 15 }
)

test.run_registered_tests()
Loading