Skip to content

Commit 6b5515d

Browse files
authored
Merge pull request #2625 from SmartThingsCommunity/zigbee-presence-sensor-lazy-load-subdrivers
CHAD-17071: zigbee-presence-sensor lazy loading of subdrivers
2 parents fb1b858 + 1224494 commit 6b5515d

File tree

10 files changed

+67
-81
lines changed

10 files changed

+67
-81
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local is_aqara_products = function(opts, driver, device, ...)
5+
local FINGERPRINTS = { mfr = "aqara", model = "lumi.motion.ac01" }
6+
7+
if device:get_manufacturer() == FINGERPRINTS.mfr and device:get_model() == FINGERPRINTS.model then
8+
return true, require("aqara")
9+
end
10+
return false
11+
end
12+
13+
return is_aqara_products

drivers/SmartThings/zigbee-presence-sensor/src/aqara/init.lua

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
14
local capabilities = require "st.capabilities"
25
local cluster_base = require "st.zigbee.cluster_base"
36
local data_types = require "st.zigbee.data_types"
@@ -19,12 +22,6 @@ local SENSITIVITY = "stse.sensitivity"
1922
local RESET_PRESENCE = "stse.resetPresence"
2023
local APP_DISTANCE = "stse.approachDistance"
2124

22-
local FINGERPRINTS = { mfr = "aqara", model = "lumi.motion.ac01" }
23-
24-
local is_aqara_products = function(opts, driver, device, ...)
25-
return device:get_manufacturer() == FINGERPRINTS.mfr and device:get_model() == FINGERPRINTS.model
26-
end
27-
2825
local function device_init(driver, device)
2926
-- no action
3027
end
@@ -104,7 +101,7 @@ local aqara_fp1_handler = {
104101
doConfigure = do_configure,
105102
infoChanged = device_info_changed
106103
},
107-
can_handle = is_aqara_products
104+
can_handle = require("aqara.can_handle"),
108105
}
109106

110107
return aqara_fp1_handler
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local function arrival_sensor_v1_can_handle(opts, driver, device, ...)
5+
-- excluding Aqara device and tagv4
6+
if device:get_manufacturer() ~= "aqara" and device:get_model() ~= "tagv4" then
7+
return true, require("arrival-sensor-v1")
8+
end
9+
return false
10+
end
11+
12+
return arrival_sensor_v1_can_handle

drivers/SmartThings/zigbee-presence-sensor/src/arrival-sensor-v1/init.lua

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
-- Copyright 2022 SmartThings
2-
--
3-
-- Licensed under the Apache License, Version 2.0 (the "License");
4-
-- you may not use this file except in compliance with the License.
5-
-- You may obtain a copy of the License at
6-
--
7-
-- http://www.apache.org/licenses/LICENSE-2.0
8-
--
9-
-- Unless required by applicable law or agreed to in writing, software
10-
-- distributed under the License is distributed on an "AS IS" BASIS,
11-
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
-- See the License for the specific language governing permissions and
13-
-- limitations under the License.
1+
-- Copyright 2022 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
144

155
-- Zigbee Spec Utils
166
local zcl_messages = require "st.zigbee.zcl"
@@ -41,10 +31,6 @@ local presence_utils = require "presence_utils"
4131

4232
local CHECKIN_INTERVAL = 20 -- seconds
4333

44-
local function arrival_sensor_v1_can_handle(opts, driver, device, ...)
45-
-- excluding Aqara device and tagv4
46-
return device:get_manufacturer() ~= "aqara" and device:get_model() ~= "tagv4"
47-
end
4834

4935
local function legacy_battery_handler(self, device, zb_rx)
5036
local battery_value = string.byte(zb_rx.body.zcl_body.body_bytes)
@@ -144,7 +130,7 @@ local arrival_sensor_v1 = {
144130
added = added_handler,
145131
init = init_handler
146132
},
147-
can_handle = arrival_sensor_v1_can_handle
133+
can_handle = require("arrival-sensor-v1.can_handle"),
148134
}
149135

150136
return arrival_sensor_v1

drivers/SmartThings/zigbee-presence-sensor/src/init.lua

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
-- Copyright 2022 SmartThings
2-
--
3-
-- Licensed under the Apache License, Version 2.0 (the "License");
4-
-- you may not use this file except in compliance with the License.
5-
-- You may obtain a copy of the License at
6-
--
7-
-- http://www.apache.org/licenses/LICENSE-2.0
8-
--
9-
-- Unless required by applicable law or agreed to in writing, software
10-
-- distributed under the License is distributed on an "AS IS" BASIS,
11-
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
-- See the License for the specific language governing permissions and
13-
-- limitations under the License.
1+
-- Copyright 2022 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
144

155
local ZigbeeDriver = require "st.zigbee"
166
local defaults = require "st.zigbee.defaults"
@@ -204,10 +194,7 @@ local zigbee_presence_driver = {
204194
},
205195
-- Custom handler for every Zigbee message
206196
zigbee_message_handler = all_zigbee_message_handler,
207-
sub_drivers = {
208-
require("aqara"),
209-
require("arrival-sensor-v1")
210-
},
197+
sub_drivers = require("sub_drivers"),
211198
health_check = false,
212199
}
213200

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
return function(sub_driver_name)
5+
-- gets the current lua libs api version
6+
local version = require "version"
7+
local ZigbeeDriver = require "st.zigbee"
8+
if version.api >= 16 then
9+
return ZigbeeDriver.lazy_load_sub_driver_v2(sub_driver_name)
10+
elseif version.api >= 9 then
11+
return ZigbeeDriver.lazy_load_sub_driver(require(sub_driver_name))
12+
else
13+
return require(sub_driver_name)
14+
end
15+
end

drivers/SmartThings/zigbee-presence-sensor/src/presence_utils.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
-- Copyright 2022 SmartThings
2-
--
3-
-- Licensed under the Apache License, Version 2.0 (the "License");
4-
-- you may not use this file except in compliance with the License.
5-
-- You may obtain a copy of the License at
6-
--
7-
-- http://www.apache.org/licenses/LICENSE-2.0
8-
--
9-
-- Unless required by applicable law or agreed to in writing, software
10-
-- distributed under the License is distributed on an "AS IS" BASIS,
11-
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
-- See the License for the specific language governing permissions and
13-
-- limitations under the License.
1+
-- Copyright 2022 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
143

154
local presence_utils = {}
165

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local lazy_load_if_possible = require "lazy_load_subdriver"
5+
local sub_drivers = {
6+
lazy_load_if_possible("aqara"),
7+
lazy_load_if_possible("arrival-sensor-v1"),
8+
}
9+
return sub_drivers

drivers/SmartThings/zigbee-presence-sensor/src/test/test_st_arrival_sensor_v1.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
-- Copyright 2022 SmartThings
2-
--
3-
-- Licensed under the Apache License, Version 2.0 (the "License");
4-
-- you may not use this file except in compliance with the License.
5-
-- You may obtain a copy of the License at
6-
--
7-
-- http://www.apache.org/licenses/LICENSE-2.0
8-
--
9-
-- Unless required by applicable law or agreed to in writing, software
10-
-- distributed under the License is distributed on an "AS IS" BASIS,
11-
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
-- See the License for the specific language governing permissions and
13-
-- limitations under the License.
1+
-- Copyright 2022 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
143

154
-- Mock out globals
165
local test = require "integration_test"

drivers/SmartThings/zigbee-presence-sensor/src/test/test_zigbee_presence_sensor.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
-- Copyright 2022 SmartThings
2-
--
3-
-- Licensed under the Apache License, Version 2.0 (the "License");
4-
-- you may not use this file except in compliance with the License.
5-
-- You may obtain a copy of the License at
6-
--
7-
-- http://www.apache.org/licenses/LICENSE-2.0
8-
--
9-
-- Unless required by applicable law or agreed to in writing, software
10-
-- distributed under the License is distributed on an "AS IS" BASIS,
11-
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
-- See the License for the specific language governing permissions and
13-
-- limitations under the License.
1+
-- Copyright 2022 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
143

154
-- Mock out globals
165
local test = require "integration_test"

0 commit comments

Comments
 (0)