Skip to content

Commit a23af85

Browse files
authored
Merge pull request #2900 from SmartThingsCommunity/support/ensure-default-ep-for-water-valves
2 parents 23b6134 + d04ecd9 commit a23af85

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

drivers/SmartThings/matter-switch/src/switch_utils/device_configuration.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local FanDeviceConfiguration = {}
2323

2424
function ChildConfiguration.create_or_update_child_devices(driver, device, server_cluster_ep_ids, default_endpoint_id, assign_profile_fn)
2525
if #server_cluster_ep_ids == 1 and server_cluster_ep_ids[1] == default_endpoint_id then -- no children will be created
26-
return
26+
return
2727
end
2828

2929
table.sort(server_cluster_ep_ids)
@@ -209,14 +209,6 @@ function DeviceConfiguration.match_profile(driver, device)
209209
local optional_component_capabilities
210210
local updated_profile
211211

212-
if #embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID) > 0 then
213-
updated_profile = "water-valve"
214-
if #embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID,
215-
{feature_bitmap = clusters.ValveConfigurationAndControl.types.Feature.LEVEL}) > 0 then
216-
updated_profile = updated_profile .. "-level"
217-
end
218-
end
219-
220212
local server_onoff_ep_ids = device:get_endpoints(clusters.OnOff.ID) -- get_endpoints defaults to return EPs supporting SERVER or BOTH
221213
if #server_onoff_ep_ids > 0 then
222214
ChildConfiguration.create_or_update_child_devices(driver, device, server_onoff_ep_ids, default_endpoint_id, SwitchDeviceConfiguration.assign_profile_for_onoff_ep)
@@ -238,8 +230,15 @@ function DeviceConfiguration.match_profile(driver, device)
238230
end
239231
end
240232

241-
local fan_device_type_ep_ids = switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.FAN)
242-
if #fan_device_type_ep_ids > 0 then
233+
if #switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.WATER_VALVE) > 0 then
234+
updated_profile = "water-valve"
235+
if #embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID,
236+
{feature_bitmap = clusters.ValveConfigurationAndControl.types.Feature.LEVEL}) > 0 then
237+
updated_profile = updated_profile .. "-level"
238+
end
239+
end
240+
241+
if #switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.FAN) > 0 then
243242
updated_profile, optional_component_capabilities = FanDeviceConfiguration.assign_profile_for_fan_ep(device, default_endpoint_id)
244243
end
245244

drivers/SmartThings/matter-switch/src/switch_utils/fields.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ SwitchFields.DEVICE_TYPE_ID = {
5252
DIMMER = 0x0104,
5353
COLOR_DIMMER = 0x0105,
5454
},
55+
WATER_VALVE = 0x0042,
5556
}
5657

5758
SwitchFields.device_type_profile_map = {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)