@@ -71,11 +71,12 @@ function ZigbeeHandlers.clear_all_pin_codes_response(driver, device, zb_rx)
7171 -- apply result and identify command result statuses
7272 local user_status , credential_status
7373 if clear_pin_code_status == ResponseStatus .PASS then
74+ -- The lock keeps programming codes when clearing all PINs, so keep them locally too.
7475 -- Only clear the users table when this response is for a deleteAllUsers flow.
7576 if command_in_progress == consts .LOCK_USERS .DELETE_ALL then
76- user_status = tables . delete_all_entries (device , " users " )
77+ user_status = lock_utils . delete_all_non_programming_users (device )
7778 end
78- credential_status = tables . delete_all_entries (device , " credentials " )
79+ credential_status = lock_utils . delete_all_non_programming_credentials (device )
7980 elseif clear_pin_code_status == ResponseStatus .FAIL then
8081 user_status = consts .COMMAND_RESULT .FAILURE
8182 credential_status = consts .COMMAND_RESULT .FAILURE
@@ -143,14 +144,15 @@ function ZigbeeHandlers.get_pin_code_response(driver, device, zb_rx)
143144 if command_in_progress == consts .SYNC .CODES_FROM_LOCK then
144145 -- Only add entries for slots that are actually occupied on the lock.
145146 local occupied = zb_rx .body .zcl_body .user_status .value == clusters .DoorLock .types .DrlkUserStatus .OCCUPIED_ENABLED
147+ local is_programming_code = zb_rx .body .zcl_body .user_type .value == clusters .DoorLock .types .DrlkUserType .MASTER_USER
146148 if user_id < device :get_field (consts .SYNC .CODE_INDEX ) then
147149 device .log .warn_with ({ hub_logs = true }, " GetPINCodeResponse for index %s already processed, expected %s" , user_id , device :get_field (consts .SYNC .CODE_INDEX ))
148150 return -- return early if we've already processed this response.
149151 elseif user_id ~= device :get_field (consts .SYNC .CODE_INDEX ) then
150152 device .log .warn_with ({ hub_logs = true }, " Unexpected GetPINCodeResponse for index %s, expected %s" , user_id , device :get_field (consts .SYNC .CODE_INDEX ))
151153 occupied = false -- treat this as an unoccupied slot to continue the sync
152154 end
153- lock_utils .continue_code_sync (device , occupied , false )
155+ lock_utils .continue_code_sync (device , occupied , false , is_programming_code )
154156 end
155157end
156158
@@ -179,12 +181,6 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
179181 local user_id = tonumber (zb_rx .body .zcl_body .user_id .value )
180182 local event_code = tonumber (zb_rx .body .zcl_body .program_event_code .value )
181183
182- if device :get_manufacturer () == " ASSA ABLOY iRevo" or device :get_manufacturer () == " Yale" then
183- if user_id >= 256 then -- Index is incorrectly written on these devices. Attempt to shift it to get an actual value
184- user_id = user_id >> 8
185- end
186- end
187-
188184 local ProgramEventCode = clusters .DoorLock .types .ProgramEventCode
189185 -- MASTER_CODE_CHANGED = 1
190186 -- PIN_CODE_ADDED = 2
@@ -218,7 +214,8 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
218214 command_in_progress = consts .LOCK_CREDENTIALS .DELETE -- ensure the lockCredentials result is emitted after this with the correct command context
219215 end
220216 end
221- elseif event_code == ProgramEventCode .PIN_CODE_CHANGED and command_in_progress == consts .LOCK_CREDENTIALS .UPDATE then
217+ elseif (event_code == ProgramEventCode .PIN_CODE_CHANGED or event_code == ProgramEventCode .MASTER_CODE_CHANGED )
218+ and command_in_progress == consts .LOCK_CREDENTIALS .UPDATE then
222219 result_status , user_index = lock_utils .apply_commanded_credential (device , command_in_progress , credential_args_in_use )
223220 end
224221
@@ -235,9 +232,12 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
235232 end
236233
237234 -- handle the case where we receive a programming event notification for a code we didn't send a command for
238- if event_code == ProgramEventCode .PIN_CODE_ADDED then
235+ if event_code == ProgramEventCode .MASTER_CODE_CHANGED then
236+ -- ensure the credential is represented locally. no-op if it already exists and state is accurate.
237+ lock_utils .add_reported_credential (device , user_id , true )
238+ elseif event_code == ProgramEventCode .PIN_CODE_ADDED then
239239 -- if no "addCredential" command is in progress, ensure the credential is represented locally
240- lock_utils .add_reported_credential (device , user_id )
240+ lock_utils .add_reported_credential (device , user_id , false )
241241 elseif event_code == ProgramEventCode .PIN_CODE_DELETED then
242242 -- check if a credential exists locally for this user id, then
243243 -- try to delete the entries in our tables corresponding to this code.
@@ -349,24 +349,24 @@ function ZigbeeHandlers.lock_state(driver, device, value, zb_rx)
349349 end
350350end
351351
352- function ZigbeeHandlers .max_pin_code_length (driver , device , value )
353- device :emit_event (capabilities .lockCredentials .maxPinCodeLen (value .value , { visibility = { displayed = false } }))
352+ function ZigbeeHandlers .max_pin_code_length (driver , device , zb_rx )
353+ device :emit_event (capabilities .lockCredentials .maxPinCodeLen (zb_rx .value , { visibility = { displayed = false } }))
354354end
355355
356- function ZigbeeHandlers .min_pin_code_length (driver , device , value )
357- device :emit_event (capabilities .lockCredentials .minPinCodeLen (value .value , { visibility = { displayed = false } }))
356+ function ZigbeeHandlers .min_pin_code_length (driver , device , zb_rx )
357+ device :emit_event (capabilities .lockCredentials .minPinCodeLen (zb_rx .value , { visibility = { displayed = false } }))
358358end
359359
360- function ZigbeeHandlers .number_of_pin_users_supported (driver , device , value )
361- if not device :supports_capability_by_id (capabilities .lockCodes .ID ) and value .value > 0 then
360+ function ZigbeeHandlers .number_of_pin_users_supported (driver , device , zb_rx )
361+ if not device :supports_capability_by_id (capabilities .lockCodes .ID ) and zb_rx .value > 0 then
362362 -- this device was generically fingerprinted, but supports PIN users, so we should migrate it.
363363 device :try_update_metadata ({ profile = " base-lock" })
364364 end
365365 if device :supports_capability (capabilities .lockCredentials ) then
366- device :emit_event (capabilities .lockCredentials .pinUsersSupported (value .value , {visibility = {displayed = false }}))
366+ device :emit_event (capabilities .lockCredentials .pinUsersSupported (zb_rx .value , {visibility = {displayed = false }}))
367367 end
368368 if device :supports_capability (capabilities .lockUsers ) then
369- device :emit_event (capabilities .lockUsers .totalUsersSupported (value .value , {visibility = {displayed = false }}))
369+ device :emit_event (capabilities .lockUsers .totalUsersSupported (zb_rx .value , {visibility = {displayed = false }}))
370370 end
371371end
372372
0 commit comments