Skip to content

Commit

Permalink
Merge pull request #1059 from SmartThingsCommunity/bugfix/CHAD-12031
Browse files Browse the repository at this point in the history
CHAD-12031 Fixes incorrectly-ported logic for Schlage user codes
  • Loading branch information
greens authored Nov 7, 2023
2 parents 4c8dd06 + ed5f962 commit ba33680
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions drivers/SmartThings/zwave-lock/src/schlage-lock/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ local function set_code(self, device, cmd)
)
end
local current_code_length = device:get_latest_state("main", capabilities.lockCodes.ID, capabilities.lockCodes.codeLength.NAME)
if current_code_length ~= nil then
if current_code_length == nil then
device:send(Configuration:Get({parameter_number = SCHLAGE_LOCK_CODE_LENGTH_PARAM.number}))
device.thread:call_with_delay(DEFAULT_COMMANDS_DELAY, send_set_user_code)
else
Expand Down Expand Up @@ -119,7 +119,7 @@ local function is_user_code_report_mfr_specific(device, cmd)
local code_id = cmd.args.user_identifier

if reported_user_id_status == user_id_status.ENABLED_GRANT_ACCESS or -- OCCUPIED in UserCodeV1
(user_code == user_id_status.STATUS_NOT_AVAILABLE and user_code ~= nil) then
(reported_user_id_status == user_id_status.STATUS_NOT_AVAILABLE and user_code ~= nil) then
local code_state = device:get_field(constants.CODE_STATE)
return user_code == "**********" or user_code == nil or (code_state ~= nil and code_state["setName"..cmd.args.user_identifier] ~= nil)
else
Expand All @@ -136,7 +136,7 @@ local function user_code_report_handler(self, device, cmd)
local event

if reported_user_id_status == user_id_status.ENABLED_GRANT_ACCESS or -- OCCUPIED in UserCodeV1
(user_code == user_id_status.STATUS_NOT_AVAILABLE and user_code ~= nil) then
(reported_user_id_status == user_id_status.STATUS_NOT_AVAILABLE and user_code ~= nil) then
local code_name = LockCodesDefaults.get_code_name(device, code_id)
local change_type = LockCodesDefaults.get_change_type(device, code_id)
event = capabilities.lockCodes.codeChanged(code_id..""..change_type, { state_change = true })
Expand Down
60 changes: 57 additions & 3 deletions drivers/SmartThings/zwave-lock/src/test/test_schlage_lock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ local mock_device = test.mock_device.build_test_zwave_device(
}
)

local SCHLAGE_LOCK_CODE_LENGTH_PARAM = {number = 16, size = 1}

local function test_init()
test.mock_device.add_test_device(mock_device)
end
Expand All @@ -58,15 +60,24 @@ test.set_test_init_function(test_init)
test.register_coroutine_test(
"Setting a user code should result in the named code changed event firing",
function()
test.timer.__create_and_queue_test_time_advance_timer(4.2, "oneshot")
test.socket.capability:__queue_receive({ mock_device.id, { capability = capabilities.lockCodes.ID, command = "setCode", args = { 1, "1234", "test" } } })
test.socket.zwave:__expect_send(
zw_test_utils.zwave_test_build_send_command(
mock_device,
Configuration:Get({parameter_number = SCHLAGE_LOCK_CODE_LENGTH_PARAM.number})
)
)
test.wait_for_events()
test.mock_time.advance_time(4.2)
test.socket.zwave:__expect_send(
zw_test_utils.zwave_test_build_send_command(
mock_device,
UserCode:Set({user_identifier = 1, user_code = "1234", user_id_status = UserCode.user_id_status.ENABLED_GRANT_ACCESS})
)
)
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}) })
test.socket.zwave:__queue_receive({mock_device.id, UserCode:Report({user_identifier = 1, user_id_status = UserCode.user_id_status.STATUS_NOT_AVAILABLE, user_code="0000\n\r"}) })
test.socket.capability:__set_channel_ordering("relaxed")
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
capabilities.lockCodes.lockCodes(json.encode({["1"] = "test"}), { visibility = { displayed = false } })
Expand All @@ -77,8 +88,6 @@ test.register_coroutine_test(
end
)

local SCHLAGE_LOCK_CODE_LENGTH_PARAM = {number = 16, size = 1}

test.register_coroutine_test(
"Setting a code length should be handled",
function()
Expand Down Expand Up @@ -112,6 +121,51 @@ test.register_coroutine_test(
end
)

test.register_coroutine_test(
"Configuration report indicating code deletion should be handled",
function()
test.socket.zwave:__queue_receive({
mock_device.id,
Configuration:Report({
parameter_number = SCHLAGE_LOCK_CODE_LENGTH_PARAM.number,
configuration_value = 6
})
})
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", capabilities.lockCodes.codeLength(6))
)
test.socket.zwave:__queue_receive({
mock_device.id,
Configuration:Report({
parameter_number = SCHLAGE_LOCK_CODE_LENGTH_PARAM.number,
configuration_value = 4
})
})
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", capabilities.lockCodes.lockCodes(json.encode({}), {visibility = {displayed = false}}))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", capabilities.lockCodes.codeLength(4))
)
end
)

test.register_coroutine_test(
"User code report indicating master code is available should indicate code deletion",
function ()
test.socket.zwave:__queue_receive({
mock_device.id,
UserCode:Report({
user_identifier = 0,
user_id_status = UserCode.user_id_status.AVAILABLE
})
})
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", capabilities.lockCodes.lockCodes(json.encode({}), {visibility = {displayed = false}}))
)
end
)

local expect_reload_all_codes_messages = function()
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
capabilities.lockCodes.lockCodes(json.encode({} ), { visibility = { displayed = false } })
Expand Down

0 comments on commit ba33680

Please sign in to comment.