@@ -151,10 +151,6 @@ function utils.find_default_endpoint(device)
151151 return device .MATTER_DEFAULT_ENDPOINT
152152 end
153153
154- local onoff_ep_ids = device :get_endpoints (clusters .OnOff .ID )
155- local momentary_switch_ep_ids = device :get_endpoints (clusters .Switch .ID , {feature_bitmap = clusters .Switch .types .SwitchFeature .MOMENTARY_SWITCH })
156- local fan_endpoint_ids = utils .get_endpoints_by_device_type (device , fields .DEVICE_TYPE_ID .FAN )
157-
158154 local get_first_non_zero_endpoint = function (endpoints )
159155 table.sort (endpoints )
160156 for _ ,ep in ipairs (endpoints ) do
@@ -166,23 +162,24 @@ function utils.find_default_endpoint(device)
166162 end
167163
168164 -- Return the first fan endpoint as the default endpoint if any is found
165+ local fan_endpoint_ids = utils .get_endpoints_by_device_type (device , fields .DEVICE_TYPE_ID .FAN )
169166 if # fan_endpoint_ids > 0 then
170167 return get_first_non_zero_endpoint (fan_endpoint_ids )
171168 end
172169
173- -- Return the first onoff endpoint as the default endpoint if no momentary switch endpoints are present
174- if # momentary_switch_ep_ids == 0 and # onoff_ep_ids > 0 then
175- return get_first_non_zero_endpoint (onoff_ep_ids )
176- end
177-
178- -- Return the first momentary switch endpoint as the default endpoint if no onoff endpoints are present
179- if # onoff_ep_ids == 0 and # momentary_switch_ep_ids > 0 then
180- return get_first_non_zero_endpoint (momentary_switch_ep_ids )
170+ -- Return the first water valve endpoint as the default endpoint if any is found
171+ local water_valve_endpoint_ids = utils .get_endpoints_by_device_type (device , fields .DEVICE_TYPE_ID .WATER_VALVE )
172+ if # water_valve_endpoint_ids > 0 then
173+ return get_first_non_zero_endpoint (water_valve_endpoint_ids )
181174 end
182175
183176 -- If both onoff and momentary switch endpoints are present, check the device type on the first onoff
184177 -- endpoint. If it is not a supported device type, return the first momentary switch endpoint as the
185- -- default endpoint.
178+ -- default endpoint. Else return the first onoff endpoint as the default endpoint.
179+ --
180+ -- If only one of the two types of endpoints are present, return the first endpoint of the present type.
181+ local onoff_ep_ids = device :get_endpoints (clusters .OnOff .ID )
182+ local momentary_switch_ep_ids = device :get_endpoints (clusters .Switch .ID , {feature_bitmap = clusters .Switch .types .SwitchFeature .MOMENTARY_SWITCH })
186183 if # onoff_ep_ids > 0 and # momentary_switch_ep_ids > 0 then
187184 local default_endpoint_id = get_first_non_zero_endpoint (onoff_ep_ids )
188185 if utils .device_type_supports_button_switch_combination (device , default_endpoint_id ) then
@@ -191,6 +188,10 @@ function utils.find_default_endpoint(device)
191188 device .log .warn (" The main switch endpoint does not contain a supported device type for a component configuration with buttons" )
192189 return get_first_non_zero_endpoint (momentary_switch_ep_ids )
193190 end
191+ elseif # onoff_ep_ids > 0 then
192+ return get_first_non_zero_endpoint (onoff_ep_ids )
193+ elseif # momentary_switch_ep_ids > 0 then
194+ return get_first_non_zero_endpoint (momentary_switch_ep_ids )
194195 end
195196
196197 device .log .warn (string.format (" Did not find default endpoint, will use endpoint %d instead" , device .MATTER_DEFAULT_ENDPOINT ))
0 commit comments