Skip to content

Commit 2b181a2

Browse files
Separate find_default_endpoint for modular vs static setups
Move the else clause of common-utils/find_default_endpoint into static/profiles/init.lua rather than having two paths for static vs modular.
1 parent eaa9b79 commit 2b181a2

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

drivers/SmartThings/matter-switch/src/common-utils.lua

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function common_utils.device_type_supports_button_switch_combination(device, end
310310
return false
311311
end
312312

313-
local function get_first_non_zero_endpoint(endpoints)
313+
function common_utils.get_first_non_zero_endpoint(endpoints)
314314
table.sort(endpoints)
315315
for _,ep in ipairs(endpoints) do
316316
if ep ~= 0 then -- 0 is the matter RootNode endpoint
@@ -328,55 +328,18 @@ end
328328
--- Note that Water Valve is listed first because any additional switch
329329
--- endpoints would be added as child devices.
330330
function common_utils.find_default_endpoint(device)
331-
if common_utils.supports_modular_profile(device) then
332-
local valve_eps = device:get_endpoints(clusters.ValveConfigurationAndControl.ID)
333-
if #valve_eps > 0 then
334-
return get_first_non_zero_endpoint(valve_eps)
335-
end
336-
337-
local switch_eps = device:get_endpoints(clusters.OnOff.ID)
338-
if #switch_eps > 0 then
339-
return get_first_non_zero_endpoint(switch_eps)
340-
end
341-
342-
local button_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH})
343-
if #button_eps > 0 then
344-
return get_first_non_zero_endpoint(button_eps)
345-
end
346-
else
347-
if device.manufacturer_info.vendor_id == common_utils.AQARA_MANUFACTURER_ID and
348-
device.manufacturer_info.product_id == common_utils.AQARA_CLIMATE_SENSOR_W100_ID then
349-
-- In case of Aqara Climate Sensor W100, in order to sequentially set the button name to button 1, 2, 3
350-
return device.MATTER_DEFAULT_ENDPOINT
351-
end
352-
353-
local switch_eps = device:get_endpoints(clusters.OnOff.ID)
354-
local button_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH})
355-
356-
-- Return the first switch endpoint as the default endpoint if no button endpoints are present
357-
if #button_eps == 0 and #switch_eps > 0 then
358-
return get_first_non_zero_endpoint(switch_eps)
359-
end
360-
361-
-- Return the first button endpoint as the default endpoint if no switch endpoints are present
362-
if #switch_eps == 0 and #button_eps > 0 then
363-
return get_first_non_zero_endpoint(button_eps)
364-
end
365-
366-
-- If both switch and button endpoints are present, check the device type on the main switch
367-
-- endpoint. If it is not a supported device type, return the first button endpoint as the
368-
-- default endpoint.
369-
if #switch_eps > 0 and #button_eps > 0 then
370-
local main_endpoint = get_first_non_zero_endpoint(switch_eps)
371-
if common_utils.supports_modular_profile(device) or common_utils.device_type_supports_button_switch_combination(device, main_endpoint) then
372-
return get_first_non_zero_endpoint(switch_eps)
373-
else
374-
device.log.warn("The main switch endpoint does not contain a supported device type for a component configuration with buttons")
375-
return get_first_non_zero_endpoint(button_eps)
376-
end
377-
end
331+
local valve_eps = device:get_endpoints(clusters.ValveConfigurationAndControl.ID)
332+
if #valve_eps > 0 then
333+
return common_utils.get_first_non_zero_endpoint(valve_eps)
334+
end
335+
local switch_eps = device:get_endpoints(clusters.OnOff.ID)
336+
if #switch_eps > 0 then
337+
return common_utils.get_first_non_zero_endpoint(switch_eps)
338+
end
339+
local button_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH})
340+
if #button_eps > 0 then
341+
return common_utils.get_first_non_zero_endpoint(button_eps)
378342
end
379-
380343
device.log.warn(string.format("Did not find default endpoint, will use endpoint %d instead", device.MATTER_DEFAULT_ENDPOINT))
381344
return device.MATTER_DEFAULT_ENDPOINT
382345
end

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@ local embedded_cluster_utils = require "embedded-cluster-utils"
2323
-- Used for environments that don't support modular profiles.
2424
-------------------------------------------------------------------------------------
2525

26+
--- find_default_endpoint helper function to handle situations where the device
27+
--- does not have endpoint ids in sequential order from 1.
28+
local function find_default_endpoint(device)
29+
if device.manufacturer_info.vendor_id == common_utils.AQARA_MANUFACTURER_ID and
30+
device.manufacturer_info.product_id == common_utils.AQARA_CLIMATE_SENSOR_W100_ID then
31+
-- In case of Aqara Climate Sensor W100, in order to sequentially set the button name to button 1, 2, 3
32+
return device.MATTER_DEFAULT_ENDPOINT
33+
end
34+
local switch_eps = device:get_endpoints(clusters.OnOff.ID)
35+
local button_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH})
36+
-- Return the first switch endpoint as the default endpoint if no button endpoints are present
37+
if #button_eps == 0 and #switch_eps > 0 then
38+
return common_utils.get_first_non_zero_endpoint(switch_eps)
39+
end
40+
-- Return the first button endpoint as the default endpoint if no switch endpoints are present
41+
if #switch_eps == 0 and #button_eps > 0 then
42+
return common_utils.get_first_non_zero_endpoint(button_eps)
43+
end
44+
-- If both switch and button endpoints are present, check the device type on the main switch
45+
-- endpoint. If it is not a supported device type, return the first button endpoint as the
46+
-- default endpoint.
47+
if #switch_eps > 0 and #button_eps > 0 then
48+
local main_endpoint = common_utils.get_first_non_zero_endpoint(switch_eps)
49+
if common_utils.supports_modular_profile(device) or common_utils.device_type_supports_button_switch_combination(device, main_endpoint) then
50+
return common_utils.get_first_non_zero_endpoint(switch_eps)
51+
else
52+
device.log.warn("The main switch endpoint does not contain a supported device type for a component configuration with buttons")
53+
return common_utils.get_first_non_zero_endpoint(button_eps)
54+
end
55+
end
56+
57+
device.log.warn(string.format("Did not find default endpoint, will use endpoint %d instead", device.MATTER_DEFAULT_ENDPOINT))
58+
return device.MATTER_DEFAULT_ENDPOINT
59+
end
60+
2661
local function handle_light_switch_with_onOff_server_clusters(device, main_endpoint)
2762
local cluster_id = 0
2863
for _, ep in ipairs(device.endpoints) do
@@ -67,7 +102,7 @@ local function initialize_buttons_and_switches(driver, device, main_endpoint)
67102
end
68103

69104
local function match_profile(driver, device)
70-
local main_endpoint = common_utils.find_default_endpoint(device)
105+
local main_endpoint = find_default_endpoint(device)
71106
-- initialize the main device card with buttons if applicable, and create child devices as needed for multi-switch devices.
72107
local profile_found = initialize_buttons_and_switches(driver, device, main_endpoint)
73108
if device:get_field(common_utils.IS_PARENT_CHILD_DEVICE) then
@@ -114,7 +149,7 @@ local function device_init(driver, device)
114149
if device:get_field(common_utils.IS_PARENT_CHILD_DEVICE) then
115150
device:set_find_child(common_utils.find_child)
116151
end
117-
local main_endpoint = common_utils.find_default_endpoint(device)
152+
local main_endpoint = find_default_endpoint(device)
118153
-- ensure subscription to all endpoint attributes- including those mapped to child devices
119154
common_utils.add_subscribed_attributes_and_events(device, main_endpoint)
120155
device:subscribe()

0 commit comments

Comments
 (0)