Skip to content

Commit 519d573

Browse files
committed
update default onoff profile logic
1 parent 9cca67f commit 519d573

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

drivers/SmartThings/matter-switch/src/test/test_matter_switch_device_types.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,14 @@ local function test_init_parent_child_unsupported_device_type()
587587
test.socket.device_lifecycle:__queue_receive({ mock_device_parent_child_unsupported_device_type.id, "doConfigure" })
588588
mock_device_parent_child_unsupported_device_type:expect_metadata_update({ profile = "switch-binary" })
589589
mock_device_parent_child_unsupported_device_type:expect_metadata_update({ provisioning_state = "PROVISIONED" })
590+
591+
mock_device_parent_child_unsupported_device_type:expect_device_create({
592+
type = "EDGE_CHILD",
593+
label = "Matter Switch 2",
594+
profile = "switch-binary",
595+
parent_device_id = mock_device_parent_child_unsupported_device_type.id,
596+
parent_assigned_child_key = string.format("%d", 10)
597+
})
590598
end
591599

592600
local function test_init_light_level_motion()

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

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,14 @@ local DeviceConfiguration = {}
3131
local SwitchDeviceConfiguration = {}
3232
local ButtonDeviceConfiguration = {}
3333

34-
local function server_onoff_supported(ep_info)
35-
for _, cluster in pairs(ep_info.clusters) do
36-
if cluster.cluster_id == clusters.OnOff.ID then
37-
if cluster.cluster_type == "SERVER" or cluster.cluster_type == "BOTH" then
38-
return true
39-
else
40-
return false
41-
end
42-
end
43-
end
44-
end
45-
4634
function SwitchDeviceConfiguration.assign_profile_for_onoff_ep(device, onoff_ep_id, is_child_device)
4735
local ep_info = switch_utils.get_endpoint_info(device, onoff_ep_id)
4836

4937
local primary_dt_id = switch_utils.find_max_subset_device_type(ep_info, fields.DEVICE_TYPE_ID.LIGHT)
38+
or switch_utils.find_max_subset_device_type(ep_info, fields.DEVICE_TYPE_ID.SWITCH)
5039
or ep_info.device_types[1] and ep_info.device_types[1].device_type_id
5140

52-
-- workaround: for 'Light Switch' Device Type EPs that break spec and implement their OnOff cluster as 'server'.
53-
-- For this, check server is supported before attempting to profile a device according to a Switch device type
54-
if switch_utils.tbl_contains(fields.DEVICE_TYPE_ID.SWITCH, primary_dt_id) and server_onoff_supported(ep_info) then
55-
primary_dt_id = switch_utils.find_max_subset_device_type(ep_info, fields.DEVICE_TYPE_ID.SWITCH)
56-
end
57-
58-
local profile = fields.device_type_profile_map[primary_dt_id]
41+
local generic_profile = fields.device_type_profile_map[primary_dt_id]
5942

6043
if is_child_device then
6144
-- Check if device has an overridden child profile that differs from the profile that would match
@@ -68,14 +51,15 @@ function SwitchDeviceConfiguration.assign_profile_for_onoff_ep(device, onoff_ep_
6851
for _, vendor in pairs(fields.child_device_profile_overrides_per_vendor_id) do
6952
for _, fingerprint in ipairs(vendor) do
7053
if device.manufacturer_info.product_id == fingerprint.product_id and
71-
((device.manufacturer_info.vendor_id == fields.AQARA_MANUFACTURER_ID and onoff_ep_id == 1) or profile == fingerprint.initial_profile) then
54+
((device.manufacturer_info.vendor_id == fields.AQARA_MANUFACTURER_ID and onoff_ep_id == 1) or generic_profile == fingerprint.initial_profile) then
7255
return fingerprint.target_profile
7356
end
7457
end
7558
end
7659
end
7760

78-
return profile
61+
-- if no supported device type is found, return switch-binary as the generic profile
62+
return generic_profile or "switch-binary"
7963
end
8064

8165
function SwitchDeviceConfiguration.create_child_devices(driver, device, onoff_ep_ids, main_endpoint_id)
@@ -90,21 +74,17 @@ function SwitchDeviceConfiguration.create_child_devices(driver, device, onoff_ep
9074
if ep_id ~= main_endpoint_id then -- don't create a child device that maps to the main endpoint
9175
local name = string.format("%s %d", device.label, device_num)
9276
local child_profile = SwitchDeviceConfiguration.assign_profile_for_onoff_ep(device, ep_id, true)
93-
if child_profile then -- a supported child profile was found
94-
driver:try_create_device(
95-
{
96-
type = "EDGE_CHILD",
97-
label = name,
98-
profile = child_profile,
99-
parent_device_id = device.id,
100-
parent_assigned_child_key = string.format("%d", ep_id),
101-
vendor_provided_label = name
102-
}
103-
)
104-
if idx == 1 and string.find(child_profile, "energy") then
105-
-- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
106-
device:set_field(fields.ENERGY_MANAGEMENT_ENDPOINT, ep_id, {persist = true})
107-
end
77+
driver:try_create_device({
78+
type = "EDGE_CHILD",
79+
label = name,
80+
profile = child_profile,
81+
parent_device_id = device.id,
82+
parent_assigned_child_key = string.format("%d", ep_id),
83+
vendor_provided_label = name
84+
})
85+
if idx == 1 and string.find(child_profile, "energy") then
86+
-- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
87+
device:set_field(fields.ENERGY_MANAGEMENT_ENDPOINT, ep_id, {persist = true})
10888
end
10989
end
11090
end
@@ -187,21 +167,22 @@ function DeviceConfiguration.match_profile(driver, device)
187167
local main_endpoint_id = switch_utils.find_default_endpoint(device)
188168
local updated_profile = nil
189169

190-
local valve_eps = embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID)
191-
if #valve_eps > 0 then
170+
if #embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID) > 0 then
192171
updated_profile = "water-valve"
193172
if #embedded_cluster_utils.get_endpoints(device, clusters.ValveConfigurationAndControl.ID,
194173
{feature_bitmap = clusters.ValveConfigurationAndControl.types.Feature.LEVEL}) > 0 then
195174
updated_profile = updated_profile .. "-level"
196175
end
197176
end
198177

199-
local onoff_ep_ids = device:get_endpoints(clusters.OnOff.ID)
200-
if #onoff_ep_ids > 0 then
201-
SwitchDeviceConfiguration.create_child_devices(driver, device, onoff_ep_ids, main_endpoint_id)
178+
local server_onoff_ep_ids = device:get_endpoints(clusters.OnOff.ID) -- get_endpoints defaults to return EPs supporting SERVER or BOTH
179+
if #server_onoff_ep_ids > 0 then
180+
SwitchDeviceConfiguration.create_child_devices(driver, device, server_onoff_ep_ids, main_endpoint_id)
181+
end
182+
183+
if switch_utils.tbl_contains(server_onoff_ep_ids, main_endpoint_id) then
202184
updated_profile = SwitchDeviceConfiguration.assign_profile_for_onoff_ep(device, main_endpoint_id)
203185
local generic_profile = function(s) return string.find(updated_profile or "", s, 1, true) end
204-
205186
if generic_profile("plug-binary") or generic_profile("plug-level") then
206187
if switch_utils.check_switch_category_vendor_overrides(device) then
207188
updated_profile = string.gsub(updated_profile, "plug", "switch")

0 commit comments

Comments
 (0)