Skip to content

Commit 4bfaf18

Browse files
Conditionally override child profiles (#1874)
Improve logic for overriding child device profiles by specifying the starting and target profiles that should be overridden rather than overriding in all cases.
1 parent 9fc44b4 commit 4bfaf18

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

drivers/SmartThings/matter-switch/src/init.lua

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ local device_type_profile_map = {
7979
[ON_OFF_SWITCH_ID] = "switch-binary",
8080
[ON_OFF_DIMMER_SWITCH_ID] = "switch-level",
8181
[ON_OFF_COLOR_DIMMER_SWITCH_ID] = "switch-color-level",
82-
[GENERIC_SWITCH_ID] = "button"
8382
}
8483

8584
local device_type_attribute_map = {
@@ -160,10 +159,10 @@ local device_type_attribute_map = {
160159
}
161160

162161
local child_device_profile_overrides = {
163-
{ vendor_id = 0x1321, product_id = 0x000C, child_profile = "switch-binary" },
164-
{ vendor_id = 0x1321, product_id = 0x000D, child_profile = "switch-binary" },
165-
{ vendor_id = 0x115F, product_id = 0x1008, child_profile = "light-power-energy-powerConsumption" }, -- 2 switch
166-
{ vendor_id = 0x115F, product_id = 0x1009, child_profile = "light-power-energy-powerConsumption" }, -- 4 switch
162+
{ vendor_id = 0x1321, product_id = 0x000C, target_profile = "switch-binary", initial_profile = "plug-binary" },
163+
{ vendor_id = 0x1321, product_id = 0x000D, target_profile = "switch-binary", initial_profile = "plug-binary" },
164+
{ vendor_id = 0x115F, product_id = 0x1008, target_profile = "light-power-energy-powerConsumption" }, -- 2 switch
165+
{ vendor_id = 0x115F, product_id = 0x1009, target_profile = "light-power-energy-powerConsumption" }, -- 4 switch
167166
}
168167

169168
local detect_matter_thing
@@ -436,22 +435,6 @@ end
436435
local function assign_child_profile(device, child_ep)
437436
local profile
438437

439-
-- check if device has an overridden child profile that differs from the profile
440-
-- that would match the child's device type
441-
for _, fingerprint in ipairs(child_device_profile_overrides) do
442-
if device.manufacturer_info.vendor_id == fingerprint.vendor_id and
443-
device.manufacturer_info.product_id == fingerprint.product_id then
444-
if device.manufacturer_info.vendor_id == AQARA_MANUFACTURER_ID then
445-
if child_ep ~= 1 then
446-
-- To add Electrical Sensor only to the first EDGE_CHILD(light-power-energy-powerConsumption)
447-
-- The profile of the second EDGE_CHILD is determined in the "for" loop below (e.g., light-binary)
448-
break
449-
end
450-
end
451-
return fingerprint.child_profile
452-
end
453-
end
454-
455438
for _, ep in ipairs(device.endpoints) do
456439
if ep.endpoint_id == child_ep then
457440
-- Some devices report multiple device types which are a subset of
@@ -464,8 +447,26 @@ local function assign_child_profile(device, child_ep)
464447
id = math.max(id, dt.device_type_id)
465448
end
466449
profile = device_type_profile_map[id]
450+
break
451+
end
452+
end
453+
454+
-- Check if device has an overridden child profile that differs from the profile that would match
455+
-- the child's device type for the following two cases:
456+
-- 1. To add Electrical Sensor only to the first EDGE_CHILD (light-power-energy-powerConsumption)
457+
-- for the Aqara Light Switch H2. The profile of the second EDGE_CHILD for this device is
458+
-- determined in the "for" loop above (e.g., light-binary)
459+
-- 2. The selected profile for the child device matches the initial profile defined in
460+
-- child_device_profile_overrides
461+
for _, fingerprint in ipairs(child_device_profile_overrides) do
462+
if device.manufacturer_info.vendor_id == fingerprint.vendor_id and
463+
device.manufacturer_info.product_id == fingerprint.product_id and
464+
((device.manufacturer_info.vendor_id == AQARA_MANUFACTURER_ID and child_ep == 1) or profile == fingerprint.initial_profile) then
465+
profile = fingerprint.target_profile
466+
break
467467
end
468468
end
469+
469470
-- default to "switch-binary" if no profile is found
470471
return profile or "switch-binary"
471472
end

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ local mock_device_parent_child_endpoints_non_sequential = test.mock_device.build
8484
label = "Matter Switch",
8585
profile = t_utils.get_profile_definition("light-level-colorTemperature.yml"),
8686
manufacturer_info = {
87-
vendor_id = 0x0000,
88-
product_id = 0x0000,
87+
vendor_id = 0x1321,
88+
product_id = 0x000C,
8989
},
9090
endpoints = {
9191
{
@@ -132,11 +132,9 @@ local mock_device_parent_child_endpoints_non_sequential = test.mock_device.build
132132
endpoint_id = child3_ep_non_sequential,
133133
clusters = {
134134
{cluster_id = clusters.OnOff.ID, cluster_type = "SERVER"},
135-
{cluster_id = clusters.LevelControl.ID, cluster_type = "SERVER", feature_map = 2},
136-
{cluster_id = clusters.ColorControl.ID, cluster_type = "BOTH", feature_map = 30},
137135
},
138136
device_types = {
139-
{device_type_id = 0x010D, device_type_revision = 2} -- Extended Color Light
137+
{device_type_id = 0x010A, device_type_revision = 2} -- On/Off Plug
140138
}
141139
},
142140
}
@@ -258,10 +256,11 @@ local function test_init_parent_child_endpoints_non_sequential()
258256
parent_assigned_child_key = string.format("%d", child2_ep_non_sequential)
259257
})
260258

259+
-- switch-binary will be selected as an overridden child device profile
261260
mock_device_parent_child_endpoints_non_sequential:expect_device_create({
262261
type = "EDGE_CHILD",
263262
label = "Matter Switch 3",
264-
profile = "light-color-level",
263+
profile = "switch-binary",
265264
parent_device_id = mock_device_parent_child_endpoints_non_sequential.id,
266265
parent_assigned_child_key = string.format("%d", child3_ep_non_sequential)
267266
})

0 commit comments

Comments
 (0)