Skip to content
16 changes: 13 additions & 3 deletions drivers/SmartThings/zigbee-lock/src/legacy-handlers/migrate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ local LockUsers = capabilities.lockUsers
local LockCredentials = capabilities.lockCredentials

-- Migrate lockCodes state to lockUsers/lockCredentials.
return function(driver, device, command)
-- `programming_code_slot` is the user_id, if any, known to hold a master code on this device. It is migrated
-- as a programmingUser user rather than a guest.
return function(driver, device, command, programming_code_slot)
-- set supported credentials
device:emit_event(LockCredentials.supportedCredentials({ post_migration_consts.CRED_TYPE_PIN }, { visibility = { displayed = false } }))

Expand Down Expand Up @@ -43,14 +45,21 @@ return function(driver, device, command)
for code_slot_str, _ in pairs(cached_lock_codes) do
local code_slot = tonumber(code_slot_str)
-- Slots that are not valid credential indices are dropped rather than migrated.
if math.type(code_slot) == "integer" and code_slot >= min_credential then
if code_slot and math.type(code_slot) == "integer" and code_slot >= min_credential then
table.insert(ordered_code_slots, code_slot)
if code_slot >= min_user then occupied_user_indices[code_slot] = true end
else
device.log.warn(string.format("migrate: dropping lockCodes slot %q, not a valid credential index", tostring(code_slot_str)))
end
end

-- A passed-in programming user code slot should always be migrated, even if it is not among the known codes.
if math.type(programming_code_slot) == "integer" and programming_code_slot >= min_credential
and cached_lock_codes[tostring(programming_code_slot)] == nil then
cached_lock_codes[tostring(programming_code_slot)] = post_migration_consts.PROGRAMMING_USER.DEFAULT_NAME
table.insert(ordered_code_slots, programming_code_slot)
if programming_code_slot >= min_user then occupied_user_indices[programming_code_slot] = true end
end
table.sort(ordered_code_slots)
local next_free_user_index = min_user
for _, code_slot in ipairs(ordered_code_slots) do
Expand All @@ -64,7 +73,8 @@ return function(driver, device, command)
occupied_user_indices[user_index] = true
end
-- cached_lock_codes is formatted as { ["1"] = "Guest 1", ["2"] = "Guest 2" }, etc
table.insert(users, { userIndex = user_index, userType = "guest", userName = code_name })
local user_type = code_slot == programming_code_slot and post_migration_consts.PROGRAMMING_USER.USER_TYPE or "guest"
table.insert(users, { userIndex = user_index, userType = user_type, userName = code_name })
table.insert(credentials, { userIndex = user_index, credentialIndex = code_slot, credentialType = post_migration_consts.CRED_TYPE_PIN, credentialName = code_name })
end
-- manually ensure user/cred state is persisted, then emit events to populate the new capabilities with the migrated data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- Licensed under the Apache License, Version 2.0

return function(opts, driver, device)
local fingerprints = require("yale-fingerprint-lock.fingerprints")
local fingerprints = require("yale.yale-fingerprint-lock.fingerprints")
for _, fingerprint in ipairs(fingerprints) do
if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then
local subdriver = require("legacy-handlers.yale-fingerprint-lock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ local get_pin_response_handler = function(driver, device, zb_mess)
end
end

local migrate = function(driver, device, command)
require("legacy-handlers.migrate")(driver, device, command, 0)
end

local yale_door_lock_driver = {
NAME = "Yale Door Lock",
zigbee_handlers = {
Expand All @@ -139,7 +143,8 @@ local yale_door_lock_driver = {
capability_handlers = {
[LockCodes.ID] = {
[LockCodes.commands.reloadAllCodes.NAME] = reload_all_codes,
[LockCodes.commands.setCode.NAME] = set_code
[LockCodes.commands.setCode.NAME] = set_code,
[LockCodes.commands.migrate.NAME] = migrate
}
},
can_handle = require("legacy-handlers.yale.can_handle")
Expand Down
39 changes: 37 additions & 2 deletions drivers/SmartThings/zigbee-lock/src/lock_handlers/capabilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function CapabilityHandlers.add_user(driver, device, command)
return
end

-- programming users cannot be created through the capability
if command.args.userType == consts.PROGRAMMING_USER.USER_TYPE then
lock_utils.emit_command_result(device, capabilities.lockUsers, consts.LOCK_USERS.ADD, consts.COMMAND_RESULT.INVALID_COMMAND)
return
end

-- Find the smallest positive userIndex not already in the table
local next_available_index = tables.next_index(device, "users")
local status = tables.add_entry(device, "users", {
Expand All @@ -52,6 +58,12 @@ function CapabilityHandlers.update_user(driver, device, command)
return
end

-- a user cannot be made a programming user, and a programming user cannot be changed to another type
if (command.args.userType == consts.PROGRAMMING_USER.USER_TYPE) ~= lock_utils.is_programming_user(device, command.args.userIndex) then
lock_utils.emit_command_result(device, capabilities.lockUsers, consts.LOCK_USERS.UPDATE, consts.COMMAND_RESULT.INVALID_COMMAND)
return
end

local result_status
if not tables.find_entry(device, "users", command.args.userIndex) then
result_status = tables.add_entry(device, "users", {
Expand Down Expand Up @@ -130,8 +142,11 @@ function CapabilityHandlers.add_credential(driver, device, command)
-- userIndex 0 means "create a new user for this credential" (its index is chosen when the lock confirms the
-- code). Any other userIndex must name an existing user that does not already have a credential (for now,
-- each user may have at most one credential).
-- A programming user cannot be created or given a credential through the capability.
local user_index = command.args.userIndex
if user_index ~= 0 and (not tables.find_entry(device, "users", user_index)
if command.args.userType == consts.PROGRAMMING_USER.USER_TYPE
or user_index ~= 0 and (not tables.find_entry(device, "users", user_index)
or lock_utils.is_programming_user(device, user_index)
or tables.find_entry_by(device, "credentials", "userIndex", user_index)) then
lock_utils.emit_command_result(device, capabilities.lockCredentials, consts.LOCK_CREDENTIALS.ADD, consts.COMMAND_RESULT.INVALID_COMMAND)
return
Expand Down Expand Up @@ -190,17 +205,33 @@ function CapabilityHandlers.update_credential(driver, device, command)
device:send(LockCluster.server.commands.SetPINCode(device,
command.args.credentialIndex,
UserStatusEnum.OCCUPIED_ENABLED,
UserTypeEnum.UNRESTRICTED,
-- the credential's stored owner decides its type, so a known programming code stays a programming code
lock_utils.is_programming_user(device, existing.userIndex) and UserTypeEnum.MASTER_USER or UserTypeEnum.UNRESTRICTED,
command.args.credentialData)
)
end

-- Some locks report a successful ClearPINCode for a programming code but do not actually delete it,
-- so refuse rather than dropping the credential from our tables.
local function refuse_programming_credential_delete(device, injected_by_delete_user)
device.log.warn("ignoring deleteCredential for a programming code, it cannot be deleted")
if injected_by_delete_user then
lock_utils.emit_command_result(device, capabilities.lockUsers, consts.LOCK_USERS.DELETE, consts.COMMAND_RESULT.FAILURE)
lock_utils.clear_busy_state(device)
end
lock_utils.emit_command_result(device, capabilities.lockCredentials, consts.LOCK_CREDENTIALS.DELETE, consts.COMMAND_RESULT.FAILURE)
end

function CapabilityHandlers.delete_credential(driver, device, command)
local cmd_in_progress = device:get_field(consts.DRIVER_STATE.COMMAND_IN_PROGRESS)

if cmd_in_progress == consts.LOCK_USERS.DELETE then
-- Injected by deleteUser; busy state was already set with the full LOCK_USERS.DELETE context.
local credential_args = device:get_field(consts.DRIVER_STATE.CREDENTIAL_ARGS_IN_USE) or {}
if lock_utils.is_programming_user(device, credential_args.userIndex) then
refuse_programming_credential_delete(device, true)
return
end
device:send(LockCluster.attributes.SendPINOverTheAir:write(device, true))
device:send(LockCluster.server.commands.ClearPINCode(device, credential_args.credentialIndex))
elseif lock_utils.is_device_busy(device) then
Expand All @@ -212,6 +243,10 @@ function CapabilityHandlers.delete_credential(driver, device, command)
lock_utils.emit_command_result(device, capabilities.lockCredentials, consts.LOCK_CREDENTIALS.DELETE, consts.COMMAND_RESULT.FAILURE)
return
end
if lock_utils.is_programming_user(device, found_cred.userIndex) then
refuse_programming_credential_delete(device, false)
return
end
lock_utils.set_busy_state(device, consts.LOCK_CREDENTIALS.DELETE, {
credentialIndex = command.args.credentialIndex,
credentialType = command.args.credentialType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ function ZigbeeHandlers.clear_all_pin_codes_response(driver, device, zb_rx)
-- apply result and identify command result statuses
local user_status, credential_status
if clear_pin_code_status == ResponseStatus.PASS then
-- The lock keeps programming codes when clearing all PINs, so keep them locally too.
-- Only clear the users table when this response is for a deleteAllUsers flow.
if command_in_progress == consts.LOCK_USERS.DELETE_ALL then
user_status = tables.delete_all_entries(device, "users")
user_status = lock_utils.delete_all_non_programming_users(device)
end
credential_status = tables.delete_all_entries(device, "credentials")
credential_status = lock_utils.delete_all_non_programming_credentials(device)
elseif clear_pin_code_status == ResponseStatus.FAIL then
user_status = consts.COMMAND_RESULT.FAILURE
credential_status = consts.COMMAND_RESULT.FAILURE
Expand Down Expand Up @@ -143,14 +144,15 @@ function ZigbeeHandlers.get_pin_code_response(driver, device, zb_rx)
if command_in_progress == consts.SYNC.CODES_FROM_LOCK then
-- Only add entries for slots that are actually occupied on the lock.
local occupied = zb_rx.body.zcl_body.user_status.value == clusters.DoorLock.types.DrlkUserStatus.OCCUPIED_ENABLED
local is_programming_code = zb_rx.body.zcl_body.user_type.value == clusters.DoorLock.types.DrlkUserType.MASTER_USER
if user_id < device:get_field(consts.SYNC.CODE_INDEX) then
device.log.warn_with({ hub_logs = true }, "GetPINCodeResponse for index %s already processed, expected %s", user_id, device:get_field(consts.SYNC.CODE_INDEX))
return -- return early if we've already processed this response.
elseif user_id ~= device:get_field(consts.SYNC.CODE_INDEX) then
device.log.warn_with({ hub_logs = true }, "Unexpected GetPINCodeResponse for index %s, expected %s", user_id, device:get_field(consts.SYNC.CODE_INDEX))
occupied = false -- treat this as an unoccupied slot to continue the sync
end
lock_utils.continue_code_sync(device, occupied, false)
lock_utils.continue_code_sync(device, occupied, false, is_programming_code)
end
end

Expand Down Expand Up @@ -179,12 +181,6 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
local user_id = tonumber(zb_rx.body.zcl_body.user_id.value)
local event_code = tonumber(zb_rx.body.zcl_body.program_event_code.value)

if device:get_manufacturer() == "ASSA ABLOY iRevo" or device:get_manufacturer() == "Yale" then
if user_id >= 256 then -- Index is incorrectly written on these devices. Attempt to shift it to get an actual value
user_id = user_id >> 8
end
end

local ProgramEventCode = clusters.DoorLock.types.ProgramEventCode
-- MASTER_CODE_CHANGED = 1
-- PIN_CODE_ADDED = 2
Expand Down Expand Up @@ -218,7 +214,8 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
command_in_progress = consts.LOCK_CREDENTIALS.DELETE -- ensure the lockCredentials result is emitted after this with the correct command context
end
end
elseif event_code == ProgramEventCode.PIN_CODE_CHANGED and command_in_progress == consts.LOCK_CREDENTIALS.UPDATE then
elseif (event_code == ProgramEventCode.PIN_CODE_CHANGED or event_code == ProgramEventCode.MASTER_CODE_CHANGED)
and command_in_progress == consts.LOCK_CREDENTIALS.UPDATE then
result_status, user_index = lock_utils.apply_commanded_credential(device, command_in_progress, credential_args_in_use)
end

Expand All @@ -235,9 +232,12 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
end

-- handle the case where we receive a programming event notification for a code we didn't send a command for
if event_code == ProgramEventCode.PIN_CODE_ADDED then
if event_code == ProgramEventCode.MASTER_CODE_CHANGED then
-- ensure the credential is represented locally. no-op if it already exists and state is accurate.
lock_utils.add_reported_credential(device, user_id, true)
elseif event_code == ProgramEventCode.PIN_CODE_ADDED then
-- if no "addCredential" command is in progress, ensure the credential is represented locally
lock_utils.add_reported_credential(device, user_id)
lock_utils.add_reported_credential(device, user_id, false)
elseif event_code == ProgramEventCode.PIN_CODE_DELETED then
-- check if a credential exists locally for this user id, then
-- try to delete the entries in our tables corresponding to this code.
Expand Down Expand Up @@ -349,24 +349,24 @@ function ZigbeeHandlers.lock_state(driver, device, value, zb_rx)
end
end

function ZigbeeHandlers.max_pin_code_length(driver, device, value)
device:emit_event(capabilities.lockCredentials.maxPinCodeLen(value.value, { visibility = { displayed = false } }))
function ZigbeeHandlers.max_pin_code_length(driver, device, zb_rx)
device:emit_event(capabilities.lockCredentials.maxPinCodeLen(zb_rx.value, { visibility = { displayed = false } }))
end

function ZigbeeHandlers.min_pin_code_length(driver, device, value)
device:emit_event(capabilities.lockCredentials.minPinCodeLen(value.value, { visibility = { displayed = false } }))
function ZigbeeHandlers.min_pin_code_length(driver, device, zb_rx)
device:emit_event(capabilities.lockCredentials.minPinCodeLen(zb_rx.value, { visibility = { displayed = false } }))
end

function ZigbeeHandlers.number_of_pin_users_supported(driver, device, value)
if not device:supports_capability_by_id(capabilities.lockCodes.ID) and value.value > 0 then
function ZigbeeHandlers.number_of_pin_users_supported(driver, device, zb_rx)
if not device:supports_capability_by_id(capabilities.lockCodes.ID) and zb_rx.value > 0 then
-- this device was generically fingerprinted, but supports PIN users, so we should migrate it.
device:try_update_metadata({ profile = "base-lock" })
end
if device:supports_capability(capabilities.lockCredentials) then
device:emit_event(capabilities.lockCredentials.pinUsersSupported(value.value, {visibility = {displayed = false}}))
device:emit_event(capabilities.lockCredentials.pinUsersSupported(zb_rx.value, {visibility = {displayed = false}}))
end
if device:supports_capability(capabilities.lockUsers) then
device:emit_event(capabilities.lockUsers.totalUsersSupported(value.value, {visibility = {displayed = false}}))
device:emit_event(capabilities.lockUsers.totalUsersSupported(zb_rx.value, {visibility = {displayed = false}}))
end
end

Expand Down
6 changes: 6 additions & 0 deletions drivers/SmartThings/zigbee-lock/src/lock_utils/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ lock_constants.SYNC = {
CODES_SYNC_VERSION = 1, -- bump to force every device to re-sync its codes (a mismatch with codesSynced triggers it; with syncCodeIndexVersion, restarts from index 0)
}

-- constants defining programming code representation in the lock's state tables.
lock_constants.PROGRAMMING_USER = {
USER_TYPE = "programmingUser",
DEFAULT_NAME = "Programming Code"
}

lock_constants.COMMAND_RESULT = {
SUCCESS = "success",
FAILURE = "failure",
Expand Down
17 changes: 17 additions & 0 deletions drivers/SmartThings/zigbee-lock/src/lock_utils/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ function table_utils.delete_all_entries(device, table_name)
return COMMAND_RESULT.SUCCESS
end

-- Delete every entry of a table for which `keep(entry)` is false, emitting a single update.
function table_utils.delete_entries_except(device, table_name, keep)
device.log.debug("table_helpers: attempting to delete all non-kept entries from " .. table_name)
local def = resolve_table_def(device, table_name)
if not def then return COMMAND_RESULT.FAILURE end
local t = table_utils.get_state(device, def)
if not t then return COMMAND_RESULT.FAILURE end

local kept = {}
for _, entry in ipairs(t) do
if keep(entry) then table.insert(kept, entry) end
end
device:emit_event(def.attribute(kept, {visibility = {displayed = false}}))
persist_table(device, def, kept)
return COMMAND_RESULT.SUCCESS
end

-- Restore capability state from the persistent field store.
-- Called during init to re-emit table events if the capability state cache
-- has been wiped (e.g. after a hub reboot). Only emits for tables that have
Expand Down
Loading
Loading