Skip to content

Commit a8e635c

Browse files
committed
Update the system to manage credentialInfoTable within the persist area
1 parent 3444bc3 commit a8e635c

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: 'Aqara Lock'
22
packageKey: 'aqara-lock'
33
permissions:
4-
aqara_provisioning: {}
4+
# aqara_provisioning: {}
55
zigbee: {}
66
description: "SmartThings driver for Aqara Doorlock"
77
vendorSupportInformation: "https://www.aqara.com/en/support/"

drivers/Aqara/aqara-lock/src/credential_utils.lua

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require "log"
12
local capabilities = require "st.capabilities"
23
local utils = require "st.utils"
34

@@ -6,6 +7,28 @@ local lockCredentialInfo = capabilities["stse.lockCredentialInfo"]
67

78
local credential_utils = {}
89
local HOST_COUNT = "__host_count"
10+
local PERSIST_DATA = "__persist_area"
11+
12+
credential_utils.attrCopy = function(table)
13+
local copy = {}
14+
for key, value in pairs(table) do
15+
copy[key] = utils.deep_copy(value)
16+
end
17+
return copy
18+
end
19+
20+
credential_utils.backup_data = function(device)-- Back up data the persistent
21+
local credentialInfoTable = utils.deep_copy(device:get_latest_state("main", lockCredentialInfo.ID,
22+
lockCredentialInfo.credentialInfo.NAME, {}))
23+
local backupData = credential_utils.attrCopy(credentialInfoTable)
24+
device:set_field(PERSIST_DATA, backupData, { persist = true })
25+
end
26+
27+
credential_utils.sync = function(driver, device)
28+
log.warn("credential_utils.sync entry")
29+
local credentialInfoTable = device:get_field(PERSIST_DATA)
30+
device:emit_event(lockCredentialInfo.credentialInfo(credentialInfoTable, { visibility = { displayed = false } }))
31+
end
932

1033
credential_utils.save_data = function(driver)
1134
driver.datastore:save()
@@ -29,6 +52,7 @@ credential_utils.update_remote_control_status = function(driver, device, added)
2952

3053
device:set_field(HOST_COUNT, host_cnt, { persist = true })
3154
credential_utils.save_data(driver)
55+
credential_utils.backup_data(device)
3256
end
3357

3458
credential_utils.sync_all_credential_info = function(driver, device, command)
@@ -39,6 +63,7 @@ credential_utils.sync_all_credential_info = function(driver, device, command)
3963
end
4064
device:emit_event(lockCredentialInfo.credentialInfo(command.args.credentialInfo, { visibility = { displayed = false } }))
4165
credential_utils.save_data(driver)
66+
credential_utils.backup_data(device)
4267
end
4368

4469
credential_utils.upsert_credential_info = function(driver, device, command)
@@ -71,9 +96,10 @@ credential_utils.upsert_credential_info = function(driver, device, command)
7196
if credential_utils.is_exist_host(device) == false then
7297
credential_utils.update_remote_control_status(driver, device, true)
7398
end
74-
99+
75100
device:emit_event(lockCredentialInfo.credentialInfo(credentialInfoTable, { visibility = { displayed = false } }))
76101
credential_utils.save_data(driver)
102+
credential_utils.backup_data(device)
77103
end
78104

79105
credential_utils.delete_user = function(driver, device, command)
@@ -96,6 +122,7 @@ credential_utils.delete_user = function(driver, device, command)
96122

97123
device:emit_event(lockCredentialInfo.credentialInfo(credentialInfoTable, { visibility = { displayed = false } }))
98124
credential_utils.save_data(driver)
125+
credential_utils.backup_data(device)
99126
end
100127

101128
credential_utils.delete_credential = function(driver, device, command)
@@ -117,6 +144,7 @@ credential_utils.delete_credential = function(driver, device, command)
117144

118145
device:emit_event(lockCredentialInfo.credentialInfo(credentialInfoTable, { visibility = { displayed = false } }))
119146
credential_utils.save_data(driver)
147+
credential_utils.backup_data(device)
120148
end
121149

122150
credential_utils.find_userLabel = function(driver, device, value)

drivers/Aqara/aqara-lock/src/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ local function device_added(self, device)
9898
credential_utils.save_data(self)
9999
end
100100

101+
local function device_driverSwitched(self, device)
102+
credential_utils.sync(self, device)
103+
end
104+
101105
local function toValue(payload, start, length)
102106
return utils.deserialize_int(string.sub(payload, start, start + length - 1), length, false, false)
103107
end
@@ -338,7 +342,8 @@ local aqara_locks_handler = {
338342
},
339343
lifecycle_handlers = {
340344
init = device_init,
341-
added = device_added
345+
added = device_added,
346+
driverSwitched = device_driverSwitched
342347
},
343348
secret_data_handlers = {
344349
[security.SECRET_KIND_AQARA] = my_secret_data_handler

0 commit comments

Comments
 (0)