Skip to content

Commit 08ea8eb

Browse files
authored
Add greater dynamic profiling to matter switch (#2481)
1 parent b53f7bb commit 08ea8eb

10 files changed

+326
-190
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ function SwitchLifecycleHandlers.device_init(driver, device)
8585
if device:get_field(fields.IS_PARENT_CHILD_DEVICE) then
8686
device:set_find_child(switch_utils.find_child)
8787
end
88-
local main_endpoint = switch_utils.find_default_endpoint(device)
88+
local default_endpoint_id = switch_utils.find_default_endpoint(device)
8989
-- ensure subscription to all endpoint attributes- including those mapped to child devices
9090
for idx, ep in ipairs(device.endpoints) do
91-
if ep.endpoint_id ~= main_endpoint then
91+
if ep.endpoint_id ~= default_endpoint_id then
9292
if device:supports_server_cluster(clusters.OnOff.ID, ep) then
9393
local child_profile = switch_cfg.assign_child_profile(device, ep)
9494
if idx == 1 and string.find(child_profile, "energy") then
@@ -101,7 +101,7 @@ function SwitchLifecycleHandlers.device_init(driver, device)
101101
id = math.max(id, dt.device_type_id)
102102
end
103103
for _, attr in pairs(fields.device_type_attribute_map[id] or {}) do
104-
if id == fields.GENERIC_SWITCH_ID and
104+
if id == fields.DEVICE_TYPE_ID.GENERIC_SWITCH and
105105
attr ~= clusters.PowerSource.attributes.BatPercentRemaining and
106106
attr ~= clusters.PowerSource.attributes.BatChargeLevel then
107107
device:add_subscribed_event(attr)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ if version.api < 11 then
2727
clusters.PowerTopology = require "embedded_clusters.PowerTopology"
2828
end
2929

30-
if version.api < 16 then
31-
clusters.Descriptor = require "embedded_clusters.Descriptor"
32-
end
33-
3430
local aqara_parent_ep = 4
3531
local aqara_child1_ep = 1
3632
local aqara_child2_ep = 2

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ local mock_device = test.mock_device.build_test_matter_device({
5656
{cluster_id = clusters.LevelControl.ID, cluster_type = "SERVER", feature_map = 2}
5757
},
5858
device_types = {
59-
{ device_type_id = 0x010A, device_type_revision = 1 } -- OnOff Plug
59+
{ device_type_id = 0x010B, device_type_revision = 1 }, -- Dimmable Plug In Unit
6060
}
6161
}
6262
},
@@ -88,10 +88,20 @@ local mock_device_periodic = test.mock_device.build_test_matter_device({
8888
{ device_type_id = 0x0510, device_type_revision = 1 } -- Electrical Sensor
8989
}
9090
},
91+
{
92+
endpoint_id = 2,
93+
clusters = {
94+
{ cluster_id = clusters.OnOff.ID, cluster_type = "SERVER", cluster_revision = 1, feature_map = 0, },
95+
},
96+
device_types = {
97+
{ device_type_id = 0x010A, device_type_revision = 1 }, -- On Off Plug In Unit
98+
}
99+
}
91100
},
92101
})
93102

94103
local subscribed_attributes_periodic = {
104+
clusters.OnOff.attributes.OnOff,
95105
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported,
96106
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported,
97107
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,52 @@ local mock_device_no_hue_sat = test.mock_device.build_test_matter_device({
7777
}
7878
})
7979

80+
local mock_device_color_temp = test.mock_device.build_test_matter_device({
81+
profile = t_utils.get_profile_definition("light-level-colorTemperature.yml"),
82+
manufacturer_info = {
83+
vendor_id = 0x0000,
84+
product_id = 0x0000,
85+
},
86+
endpoints = {
87+
{
88+
endpoint_id = 1,
89+
clusters = {
90+
{cluster_id = clusters.OnOff.ID, cluster_type = "SERVER"},
91+
{cluster_id = clusters.ColorControl.ID, cluster_type = "BOTH", feature_map = 30},
92+
{cluster_id = clusters.LevelControl.ID, cluster_type = "SERVER"}
93+
},
94+
device_types = {
95+
{device_type_id = 0x0100, device_type_revision = 1}, -- On/Off Light
96+
{device_type_id = 0x010C, device_type_revision = 1} -- Color Temperature Light
97+
}
98+
}
99+
}
100+
})
101+
102+
local mock_device_extended_color = test.mock_device.build_test_matter_device({
103+
profile = t_utils.get_profile_definition("light-color-level.yml"),
104+
manufacturer_info = {
105+
vendor_id = 0x0000,
106+
product_id = 0x0000,
107+
},
108+
endpoints = {
109+
{
110+
endpoint_id = 1,
111+
clusters = {
112+
{cluster_id = clusters.OnOff.ID, cluster_type = "SERVER"},
113+
{cluster_id = clusters.ColorControl.ID, cluster_type = "BOTH", feature_map = 30},
114+
{cluster_id = clusters.LevelControl.ID, cluster_type = "SERVER", feature_map = 2}
115+
},
116+
device_types = {
117+
{device_type_id = 0x0100, device_type_revision = 1}, -- On/Off Light
118+
{device_type_id = 0x0101, device_type_revision = 1}, -- Dimmable Light
119+
{device_type_id = 0x010C, device_type_revision = 1}, -- Color Temperature Light
120+
{device_type_id = 0x010D, device_type_revision = 1}, -- Extended Color Light
121+
}
122+
}
123+
}
124+
})
125+
80126
local cluster_subscribe_list = {
81127
clusters.OnOff.attributes.OnOff,
82128
clusters.LevelControl.attributes.CurrentLevel,
@@ -146,6 +192,67 @@ local function test_init_no_hue_sat()
146192
set_color_mode(mock_device_no_hue_sat, 1, clusters.ColorControl.types.ColorMode.CURRENTX_AND_CURRENTY)
147193
end
148194

195+
196+
local cluster_subscribe_list_color_temp = {
197+
clusters.OnOff.attributes.OnOff,
198+
clusters.LevelControl.attributes.CurrentLevel,
199+
clusters.LevelControl.attributes.MaxLevel,
200+
clusters.LevelControl.attributes.MinLevel,
201+
clusters.ColorControl.attributes.ColorTemperatureMireds,
202+
clusters.ColorControl.attributes.ColorTempPhysicalMaxMireds,
203+
clusters.ColorControl.attributes.ColorTempPhysicalMinMireds
204+
}
205+
206+
local function test_init_color_temp()
207+
test.mock_device.add_test_device(mock_device_color_temp)
208+
local subscribe_request = cluster_subscribe_list_color_temp[1]:subscribe(mock_device_color_temp)
209+
for i, cluster in ipairs(cluster_subscribe_list_color_temp) do
210+
if i > 1 then
211+
subscribe_request:merge(cluster:subscribe(mock_device_color_temp))
212+
end
213+
end
214+
test.socket.matter:__expect_send({mock_device_color_temp.id, subscribe_request})
215+
test.socket.device_lifecycle:__queue_receive({ mock_device_color_temp.id, "added" })
216+
test.socket.matter:__expect_send({mock_device_color_temp.id, subscribe_request})
217+
218+
test.socket.device_lifecycle:__queue_receive({ mock_device_color_temp.id, "init" })
219+
test.socket.matter:__expect_send({mock_device_color_temp.id, subscribe_request})
220+
221+
test.socket.device_lifecycle:__queue_receive({ mock_device_color_temp.id, "doConfigure" })
222+
mock_device_color_temp:expect_metadata_update({ provisioning_state = "PROVISIONED" })
223+
end
224+
225+
local function test_init_extended_color()
226+
test.mock_device.add_test_device(mock_device_extended_color)
227+
local subscribe_request = cluster_subscribe_list[1]:subscribe(mock_device_extended_color)
228+
for i, cluster in ipairs(cluster_subscribe_list) do
229+
if i > 1 then
230+
subscribe_request:merge(cluster:subscribe(mock_device_extended_color))
231+
end
232+
end
233+
test.socket.matter:__expect_send({mock_device_extended_color.id, subscribe_request})
234+
test.socket.device_lifecycle:__queue_receive({ mock_device_extended_color.id, "added" })
235+
test.socket.matter:__expect_send({mock_device_extended_color.id, subscribe_request})
236+
237+
test.socket.device_lifecycle:__queue_receive({ mock_device_extended_color.id, "init" })
238+
test.socket.matter:__expect_send({mock_device_extended_color.id, subscribe_request})
239+
240+
test.socket.device_lifecycle:__queue_receive({ mock_device_extended_color.id, "doConfigure" })
241+
mock_device_extended_color:expect_metadata_update({ provisioning_state = "PROVISIONED" })
242+
end
243+
244+
test.register_message_test(
245+
"Test that Color Temperature Light device does not switch profiles",
246+
{},
247+
{ test_init = test_init_color_temp }
248+
)
249+
250+
test.register_message_test(
251+
"Test that Extended Color Light device does not switch profiles",
252+
{},
253+
{ test_init = test_init_extended_color }
254+
)
255+
149256
test.register_message_test(
150257
"On command should send the appropriate commands",
151258
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ local function test_init_mounted_on_off_control()
505505
test.socket.matter:__expect_send({mock_device_mounted_on_off_control.id, subscribe_request})
506506

507507
test.socket.device_lifecycle:__queue_receive({ mock_device_mounted_on_off_control.id, "doConfigure" })
508+
mock_device_mounted_on_off_control:expect_metadata_update({ profile = "switch-binary" })
508509
mock_device_mounted_on_off_control:expect_metadata_update({ provisioning_state = "PROVISIONED" })
509510
end
510511

@@ -526,6 +527,7 @@ local function test_init_mounted_dimmable_load_control()
526527
test.socket.matter:__expect_send({mock_device_mounted_dimmable_load_control.id, subscribe_request})
527528

528529
test.socket.device_lifecycle:__queue_receive({ mock_device_mounted_dimmable_load_control.id, "doConfigure" })
530+
mock_device_mounted_dimmable_load_control:expect_metadata_update({ profile = "switch-level" })
529531
mock_device_mounted_dimmable_load_control:expect_metadata_update({ provisioning_state = "PROVISIONED" })
530532
end
531533

@@ -566,6 +568,7 @@ local function test_init_parent_child_different_types()
566568
test.socket.matter:__expect_send({mock_device_parent_child_different_types.id, subscribe_request})
567569

568570
test.socket.device_lifecycle:__queue_receive({ mock_device_parent_child_different_types.id, "doConfigure" })
571+
mock_device_parent_child_different_types:expect_metadata_update({ profile = "switch-binary" })
569572
mock_device_parent_child_different_types:expect_metadata_update({ provisioning_state = "PROVISIONED" })
570573

571574
mock_device_parent_child_different_types:expect_device_create({
@@ -617,6 +620,7 @@ local function test_init_light_level_motion()
617620
test.socket.matter:__expect_send({mock_device_light_level_motion.id, subscribe_request})
618621

619622
test.socket.device_lifecycle:__queue_receive({ mock_device_light_level_motion.id, "doConfigure" })
623+
mock_device_light_level_motion:expect_metadata_update({ profile = "light-level-motion" })
620624
mock_device_light_level_motion:expect_metadata_update({ provisioning_state = "PROVISIONED" })
621625
end
622626

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ local function test_init()
189189
test.socket.matter:__expect_send({mock_device.id, subscribe_request})
190190

191191
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" })
192+
mock_device:expect_metadata_update({ profile = "light-binary" })
192193
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
193194

194195
for _, child in pairs(mock_children) do
@@ -260,6 +261,7 @@ local function test_init_parent_child_endpoints_non_sequential()
260261
test.socket.matter:__expect_send({mock_device_parent_child_endpoints_non_sequential.id, subscribe_request})
261262

262263
test.socket.device_lifecycle:__queue_receive({ mock_device_parent_child_endpoints_non_sequential.id, "doConfigure" })
264+
mock_device_parent_child_endpoints_non_sequential:expect_metadata_update({ profile = "light-binary" })
263265
mock_device_parent_child_endpoints_non_sequential:expect_metadata_update({ provisioning_state = "PROVISIONED" })
264266

265267
for _, child in pairs(mock_children_non_sequential) do

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ local function test_init()
146146
test.socket.matter:__expect_send({mock_device.id, subscribe_request})
147147

148148
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" })
149+
mock_device:expect_metadata_update({ profile = "plug-binary" })
149150
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
150151

151152
for _, child in pairs(mock_children) do
@@ -196,6 +197,7 @@ local function test_init_child_profile_override()
196197
test.socket.matter:__expect_send({mock_device_child_profile_override.id, subscribe_request})
197198

198199
test.socket.device_lifecycle:__queue_receive({ mock_device_child_profile_override.id, "doConfigure" })
200+
mock_device_child_profile_override:expect_metadata_update({ profile = "switch-binary" })
199201
mock_device_child_profile_override:expect_metadata_update({ provisioning_state = "PROVISIONED" })
200202

201203
for _, child in pairs(mock_children_child_profile_override) do

0 commit comments

Comments
 (0)