@@ -23,6 +23,41 @@ local embedded_cluster_utils = require "embedded-cluster-utils"
23
23
-- Used for environments that don't support modular profiles.
24
24
---- ---------------------------------------------------------------------------------
25
25
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
+
26
61
local function handle_light_switch_with_onOff_server_clusters (device , main_endpoint )
27
62
local cluster_id = 0
28
63
for _ , ep in ipairs (device .endpoints ) do
@@ -67,7 +102,7 @@ local function initialize_buttons_and_switches(driver, device, main_endpoint)
67
102
end
68
103
69
104
local function match_profile (driver , device )
70
- local main_endpoint = common_utils . find_default_endpoint (device )
105
+ local main_endpoint = find_default_endpoint (device )
71
106
-- initialize the main device card with buttons if applicable, and create child devices as needed for multi-switch devices.
72
107
local profile_found = initialize_buttons_and_switches (driver , device , main_endpoint )
73
108
if device :get_field (common_utils .IS_PARENT_CHILD_DEVICE ) then
@@ -114,7 +149,7 @@ local function device_init(driver, device)
114
149
if device :get_field (common_utils .IS_PARENT_CHILD_DEVICE ) then
115
150
device :set_find_child (common_utils .find_child )
116
151
end
117
- local main_endpoint = common_utils . find_default_endpoint (device )
152
+ local main_endpoint = find_default_endpoint (device )
118
153
-- ensure subscription to all endpoint attributes- including those mapped to child devices
119
154
common_utils .add_subscribed_attributes_and_events (device , main_endpoint )
120
155
device :subscribe ()
0 commit comments