@@ -70,17 +70,23 @@ function SwitchDeviceConfiguration.assign_child_profile(device, child_ep)
7070 return profile or " switch-binary"
7171end
7272
73- function SwitchDeviceConfiguration .create_child_switch_devices (driver , device , main_endpoint )
74- local num_switch_server_eps = 0
73+ function SwitchDeviceConfiguration .create_child_switch_devices (driver , device , switch_eps , main_endpoint )
74+ local switch_server_ep = 0
7575 local parent_child_device = false
76- local switch_eps = device : get_endpoints ( clusters . OnOff . ID )
76+
7777 table.sort (switch_eps )
7878 for idx , ep in ipairs (switch_eps ) do
79- if device :supports_server_cluster (clusters .OnOff .ID , ep ) then
80- num_switch_server_eps = num_switch_server_eps + 1
81- if ep ~= main_endpoint then -- don't create a child device that maps to the main endpoint
82- local name = string.format (" %s %d" , device .label , num_switch_server_eps )
83- local child_profile = SwitchDeviceConfiguration .assign_child_profile (device , ep )
79+ switch_server_ep = switch_server_ep + 1
80+ if ep ~= main_endpoint then -- don't create a child device that maps to the main endpoint
81+ local name = string.format (" %s %d" , device .label , switch_server_ep )
82+ local child_profile = SwitchDeviceConfiguration .assign_child_profile (device , ep )
83+ local child_device = nil
84+ if device :get_field (fields .IS_PARENT_CHILD_DEVICE ) then
85+ child_device = switch_utils .find_child (device , ep )
86+ end
87+ if child_device then
88+ child_device :try_update_metadata ({profile = child_profile })
89+ else
8490 driver :try_create_device (
8591 {
8692 type = " EDGE_CHILD" ,
@@ -91,11 +97,11 @@ function SwitchDeviceConfiguration.create_child_switch_devices(driver, device, m
9197 vendor_provided_label = name
9298 }
9399 )
94- parent_child_device = true
95- if idx == 1 and string.find ( child_profile , " energy " ) then
96- -- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
97- device : set_field ( fields . ENERGY_MANAGEMENT_ENDPOINT , ep , { persist = true })
98- end
100+ end
101+ parent_child_device = true
102+ if idx == 1 and string.find ( child_profile , " energy" ) then
103+ -- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
104+ device : set_field ( fields . ENERGY_MANAGEMENT_ENDPOINT , ep , { persist = true })
99105 end
100106 end
101107 end
@@ -105,29 +111,22 @@ function SwitchDeviceConfiguration.create_child_switch_devices(driver, device, m
105111 device :set_field (fields .IS_PARENT_CHILD_DEVICE , true , {persist = true })
106112 device :set_find_child (switch_utils .find_child )
107113 end
108-
109- -- this is needed in initialize_buttons_and_switches
110- return num_switch_server_eps
111114end
112115
113- function SwitchDeviceConfiguration .update_devices_with_onOff_server_clusters (device , main_endpoint )
116+ function SwitchDeviceConfiguration .match_light_switch_device_profile (device , main_endpoint )
114117 local cluster_id = 0
115118 for _ , ep in ipairs (device .endpoints ) do
116119 -- main_endpoint only supports server cluster by definition of get_endpoints()
117120 if main_endpoint == ep .endpoint_id then
118121 for _ , dt in ipairs (ep .device_types ) do
119122 -- no device type that is not in the switch subset should be considered.
120- if (fields .ON_OFF_SWITCH_ID <= dt .device_type_id and dt .device_type_id <= fields .ON_OFF_COLOR_DIMMER_SWITCH_ID ) then
123+ if (fields .ON_OFF_LIGHT_SWITCH_ID <= dt .device_type_id and dt .device_type_id <= fields .COLOR_DIMMER_SWITCH_ID ) then
121124 cluster_id = math.max (cluster_id , dt .device_type_id )
122125 end
123126 end
124- break
127+ return fields . device_type_profile_map [ cluster_id ]
125128 end
126129 end
127-
128- if fields .device_type_profile_map [cluster_id ] then
129- device :try_update_metadata ({profile = fields .device_type_profile_map [cluster_id ]})
130- end
131130end
132131
133132function ButtonDeviceConfiguration .update_button_profile (device , main_endpoint , num_button_eps )
@@ -201,32 +200,25 @@ end
201200
202201function DeviceConfiguration .match_profile (driver , device )
203202 local main_endpoint = switch_utils .find_default_endpoint (device )
203+ local profile_name = nil
204+
205+ local server_onoff_eps = device :get_endpoints (clusters .OnOff .ID , { cluster_type = " SERVER" })
206+ if # server_onoff_eps > 0 then
207+ SwitchDeviceConfiguration .create_child_switch_devices (driver , device , server_onoff_eps , main_endpoint )
208+ -- workaround: finds a profile for devices of the Light Switch device type set that break spec and implement OnOff as 'server' instead of 'client'.
209+ -- note: since the Light Switch device set isn't supported, these devices join as a matter-thing.
210+ if switch_utils .detect_matter_thing (device ) then
211+ profile_name = SwitchDeviceConfiguration .match_light_switch_device_profile (device , main_endpoint )
212+ end
213+ end
204214
205215 -- initialize the main device card with buttons if applicable
206- local profile_found = false
207216 local button_eps = device :get_endpoints (clusters .Switch .ID , {feature_bitmap = clusters .Switch .types .SwitchFeature .MOMENTARY_SWITCH })
208217 if switch_utils .tbl_contains (fields .STATIC_BUTTON_PROFILE_SUPPORTED , # button_eps ) then
209218 ButtonDeviceConfiguration .update_button_profile (device , main_endpoint , # button_eps )
210219 -- All button endpoints found will be added as additional components in the profile containing the main_endpoint.
211- -- The resulting endpoint to component map is saved in the COMPONENT_TO_ENDPOINT_MAP field
212220 ButtonDeviceConfiguration .update_button_component_map (device , main_endpoint , button_eps )
213221 ButtonDeviceConfiguration .configure_buttons (device )
214- profile_found = true
215- end
216-
217- -- Without support for bindings, only clusters that are implemented as server are counted. This count is handled
218- -- while building switch child profiles
219- local num_switch_server_eps = SwitchDeviceConfiguration .create_child_switch_devices (driver , device , main_endpoint )
220-
221- -- We do not support the Light Switch device types because they require OnOff to be implemented as 'client', which requires us to support bindings.
222- -- However, this workaround profiles devices that claim to be Light Switches, but that break spec and implement OnOff as 'server'.
223- -- Note: since their device type isn't supported, these devices join as a matter-thing.
224- if num_switch_server_eps > 0 and switch_utils .detect_matter_thing (device ) then
225- SwitchDeviceConfiguration .update_devices_with_onOff_server_clusters (device , main_endpoint )
226- profile_found = true
227- end
228-
229- if profile_found then
230222 return
231223 end
232224
@@ -235,7 +227,6 @@ function DeviceConfiguration.match_profile(driver, device)
235227 local energy_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalEnergyMeasurement .ID )
236228 local power_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalPowerMeasurement .ID )
237229 local valve_eps = embedded_cluster_utils .get_endpoints (device , clusters .ValveConfigurationAndControl .ID )
238- local profile_name = nil
239230 local level_support = " "
240231 if # level_eps > 0 then
241232 level_support = " -level"
0 commit comments