Skip to content

Commit 63158a5

Browse files
Matter Switch: Lazy load subdrivers if possible (#2525)
Use the new `lazy_load_sub_driver_v2` api to lazy load subdrivers
1 parent 9b2d2bf commit 63158a5

File tree

11 files changed

+75
-52
lines changed

11 files changed

+75
-52
lines changed

drivers/SmartThings/matter-switch/src/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ local matter_driver_template = {
297297
},
298298
supported_capabilities = fields.supported_capabilities,
299299
sub_drivers = {
300-
require("sub_drivers.aqara_cube"),
300+
switch_utils.lazy_load_if_possible("sub_drivers.aqara_cube"),
301301
switch_utils.lazy_load("sub_drivers.camera"),
302-
require("sub_drivers.eve_energy"),
303-
require("sub_drivers.ikea_scroll"),
304-
require("sub_drivers.third_reality_mk1")
302+
switch_utils.lazy_load_if_possible("sub_drivers.eve_energy"),
303+
switch_utils.lazy_load_if_possible("sub_drivers.ikea_scroll"),
304+
switch_utils.lazy_load_if_possible("sub_drivers.third_reality_mk1")
305305
}
306306
}
307307

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local device_lib = require "st.device"
5+
6+
return function(opts, driver, device)
7+
if device.network_type == device_lib.NETWORK_TYPE_MATTER then
8+
local name = string.format("%s", device.manufacturer_info.product_name)
9+
if string.find(name, "Aqara Cube T1 Pro") then
10+
return true, require("sub_drivers.aqara_cube")
11+
end
12+
end
13+
return false
14+
end

drivers/SmartThings/matter-switch/src/sub_drivers/aqara_cube/init.lua

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ local INITIAL_PRESS_ONLY = "__initial_press_only" -- for devices that support MS
2121
local CUBEACTION_TIMER = "__cubeAction_timer"
2222
local CUBEACTION_TIME = 3
2323

24-
local function is_aqara_cube(opts, driver, device)
25-
if device.network_type == device_lib.NETWORK_TYPE_MATTER then
26-
local name = string.format("%s", device.manufacturer_info.product_name)
27-
if string.find(name, "Aqara Cube T1 Pro") then
28-
return true
29-
end
30-
end
31-
return false
32-
end
33-
3424
local callback_timer = function(device)
3525
return function()
3626
device:emit_event(cubeAction.cubeAction("noAction"))
@@ -240,7 +230,7 @@ local aqara_cube_handler = {
240230
}
241231
},
242232
},
243-
can_handle = is_aqara_cube
233+
can_handle = require("sub_drivers.aqara_cube.can_handle")
244234
}
245235

246236
return aqara_cube_handler
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local device_lib = require "st.device"
5+
local fields = require "switch_utils.fields"
6+
local switch_utils = require "switch_utils.utils"
7+
8+
return function(opts, driver, device)
9+
local EVE_MANUFACTURER_ID = 0x130A
10+
-- this sub driver does NOT support child devices, and ONLY supports Eve devices
11+
-- that do NOT support the Electrical Sensor device type
12+
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
13+
device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID and
14+
#switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.ELECTRICAL_SENSOR) == 0 then
15+
return true, require("sub_drivers.eve_energy")
16+
end
17+
return false
18+
end

drivers/SmartThings/matter-switch/src/sub_drivers/eve_energy/init.lua

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ local cluster_base = require "st.matter.cluster_base"
1111
local st_utils = require "st.utils"
1212
local data_types = require "st.matter.data_types"
1313
local device_lib = require "st.device"
14-
local switch_utils = require "switch_utils.utils"
15-
local fields = require "switch_utils.fields"
1614

1715
local SWITCH_INITIALIZED = "__switch_intialized"
1816
local COMPONENT_TO_ENDPOINT_MAP = "__component_to_endpoint_map"
1917
local ON_OFF_STATES = "ON_OFF_STATES"
2018

21-
local EVE_MANUFACTURER_ID = 0x130A
2219
local PRIVATE_CLUSTER_ID = 0x130AFC01
2320

2421
local PRIVATE_ATTR_ID_WATT = 0x130A000A
@@ -38,18 +35,6 @@ local MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60) -- 15 minutes, reported in s
3835
-- Eve specifics
3936
-------------------------------------------------------------------------------------
4037

41-
local function is_eve_energy_products(opts, driver, device)
42-
-- this sub driver does NOT support child devices, and ONLY supports Eve devices
43-
-- that do NOT support the Electrical Sensor device type
44-
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
45-
device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID and
46-
#switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.ELECTRICAL_SENSOR) == 0 then
47-
return true
48-
end
49-
50-
return false
51-
end
52-
5338
-- Return a ISO 8061 formatted timestamp in UTC (Z)
5439
-- @return e.g. 2022-02-02T08:00:00Z
5540
local function epoch_to_iso8601(time)
@@ -370,7 +355,7 @@ local eve_energy_handler = {
370355
capabilities.energyMeter,
371356
capabilities.powerConsumptionReport
372357
},
373-
can_handle = is_eve_energy_products
358+
can_handle = require("sub_drivers.eve_energy.can_handle")
374359
}
375360

376361
return eve_energy_handler
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local switch_utils = require "switch_utils.utils"
5+
6+
return function(opts, driver, device)
7+
if switch_utils.get_product_override_field(device, "is_ikea_scroll") then
8+
return true, require("sub_drivers.ikea_scroll")
9+
end
10+
return false
11+
end

drivers/SmartThings/matter-switch/src/sub_drivers/ikea_scroll/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ local ikea_scroll_handler = {
4444
infoChanged = IkeaScrollLifecycleHandlers.info_changed,
4545
init = IkeaScrollLifecycleHandlers.device_init,
4646
},
47-
can_handle = scroll_utils.is_ikea_scroll
47+
can_handle = require("sub_drivers.ikea_scroll.can_handle")
4848
}
4949

5050
return ikea_scroll_handler

drivers/SmartThings/matter-switch/src/sub_drivers/ikea_scroll/scroll_utils/utils.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
local im = require "st.matter.interaction_model"
55
local clusters = require "st.matter.clusters"
6-
local switch_utils = require "switch_utils.utils"
76
local scroll_fields = require "sub_drivers.ikea_scroll.scroll_utils.fields"
87

98
local IkeaScrollUtils = {}
109

11-
function IkeaScrollUtils.is_ikea_scroll(opts, driver, device)
12-
return switch_utils.get_product_override_field(device, "is_ikea_scroll")
13-
end
14-
1510
-- override subscribe function to prevent subscribing to additional events from the main driver
1611
function IkeaScrollUtils.subscribe(device)
1712
local subscribe_request = im.InteractionRequest(im.InteractionRequest.RequestType.SUBSCRIBE, {})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Copyright 2025 SmartThings, Inc.
2+
-- Licensed under the Apache License, Version 2.0
3+
4+
local device_lib = require "st.device"
5+
6+
return function(opts, driver, device)
7+
local THIRD_REALITY_MK1_FINGERPRINT = { vendor_id = 0x1407, product_id = 0x1388 }
8+
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
9+
device.manufacturer_info.vendor_id == THIRD_REALITY_MK1_FINGERPRINT.vendor_id and
10+
device.manufacturer_info.product_id == THIRD_REALITY_MK1_FINGERPRINT.product_id then
11+
return true, require("sub_drivers.third_reality_mk1")
12+
end
13+
return false
14+
end

drivers/SmartThings/matter-switch/src/sub_drivers/third_reality_mk1/init.lua

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,14 @@
33

44
local capabilities = require "st.capabilities"
55
local clusters = require "st.matter.clusters"
6-
local device_lib = require "st.device"
76
local im = require "st.matter.interaction_model"
8-
local log = require "log"
97

108
local COMPONENT_TO_ENDPOINT_MAP = "__component_to_endpoint_map"
119

1210
-------------------------------------------------------------------------------------
1311
-- Third Reality MK1 specifics
1412
-------------------------------------------------------------------------------------
1513

16-
local THIRD_REALITY_MK1_FINGERPRINT = { vendor_id = 0x1407, product_id = 0x1388 }
17-
18-
local function is_third_reality_mk1(opts, driver, device)
19-
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
20-
device.manufacturer_info.vendor_id == THIRD_REALITY_MK1_FINGERPRINT.vendor_id and
21-
device.manufacturer_info.product_id == THIRD_REALITY_MK1_FINGERPRINT.product_id then
22-
log.info("Using Third Reality MK1 sub driver")
23-
return true
24-
end
25-
return false
26-
end
27-
2814
local function endpoint_to_component(device, ep)
2915
local map = device:get_field(COMPONENT_TO_ENDPOINT_MAP) or {}
3016
for component, endpoint in pairs(map) do
@@ -121,7 +107,7 @@ local third_reality_mk1_handler = {
121107
supported_capabilities = {
122108
capabilities.button
123109
},
124-
can_handle = is_third_reality_mk1
110+
can_handle = require("sub_drivers.third_reality_mk1.can_handle")
125111
}
126112

127113
return third_reality_mk1_handler

0 commit comments

Comments
 (0)