Skip to content

Commit 63574b2

Browse files
Make tests backwards compatible down to lua libs v10
Add backwards compatibility for matter switch test cases down to lua libs version v10. Most of the issues were from test cases expecting native handler registrations, which was added in v11. Also, running the test cases against lower lua libs caught a bug in the driver code for lua libs version v10 and lower, which prevented the energyMeter capability from being emitted. This is also fixed by this PR. It might be possible to extend support to even lower versions, but there are a few additional api changes that would need to be accounted for. Also note that to run these tests against lower versions, any capabilities that are not available in that version of the lua libs would need to be manually added to prevent test failures.
1 parent 064e1a1 commit 63574b2

10 files changed

+481
-556
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,9 @@ local function occupancy_attr_handler(driver, device, ib, response)
11141114
end
11151115

11161116
local function cumul_energy_imported_handler(driver, device, ib, response)
1117+
if ib.data and version.api < 11 then
1118+
clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct:augment_type(ib.data)
1119+
end
11171120
if ib.data.elements.energy then
11181121
local watt_hour_value = ib.data.elements.energy.value / CONVERSION_CONST_MILLIWATT_TO_WATT
11191122
device:set_field(TOTAL_IMPORTED_ENERGY, watt_hour_value, {persist = true})
@@ -1127,6 +1130,9 @@ local function cumul_energy_imported_handler(driver, device, ib, response)
11271130
end
11281131

11291132
local function per_energy_imported_handler(driver, device, ib, response)
1133+
if ib.data and version.api < 11 then
1134+
clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct:augment_type(ib.data)
1135+
end
11301136
if ib.data.elements.energy then
11311137
local watt_hour_value = ib.data.elements.energy.value / CONVERSION_CONST_MILLIWATT_TO_WATT
11321138
local latest_energy_report = device:get_field(TOTAL_IMPORTED_ENERGY) or 0

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright 2024 SmartThings
1+
-- Copyright 2025 SmartThings
22
--
33
-- Licensed under the Apache License, Version 2.0 (the "License");
44
-- you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@ local t_utils = require "integration_test.utils"
1717
local capabilities = require "st.capabilities"
1818
local utils = require "st.utils"
1919
local dkjson = require "dkjson"
20-
2120
local clusters = require "st.matter.clusters"
2221
local button_attr = capabilities.button.button
23-
24-
local DEFERRED_CONFIGURE = "__DEFERRED_CONFIGURE"
22+
local version = require "version"
23+
if version.api < 11 then
24+
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
25+
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
26+
end
2527

2628
local aqara_parent_ep = 4
2729
local aqara_child1_ep = 1
@@ -208,7 +210,6 @@ local function test_init()
208210
test.mock_devices_api._expected_device_updates[1] = {device_id = "00000000-1111-2222-3333-000000000001"}
209211
test.mock_devices_api._expected_device_updates[1].metadata = {deviceId="00000000-1111-2222-3333-000000000001", profileReference="4-button"}
210212

211-
aqara_mock_device:set_field(DEFERRED_CONFIGURE, true, opts)
212213
local device_info_copy = utils.deep_copy(aqara_mock_device.raw_st_data)
213214
device_info_copy.profile.id = "4-button"
214215
local device_info_json = dkjson.encode(device_info_copy)

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

Lines changed: 82 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright 2024 SmartThings
1+
-- Copyright 2025 SmartThings
22
--
33
-- Licensed under the Apache License, Version 2.0 (the "License");
44
-- you may not use this file except in compliance with the License.
@@ -15,11 +15,12 @@
1515
local test = require "integration_test"
1616
local capabilities = require "st.capabilities"
1717
local t_utils = require "integration_test.utils"
18-
1918
local clusters = require "st.matter.clusters"
20-
21-
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
22-
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
19+
local version = require "version"
20+
if version.api < 11 then
21+
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
22+
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
23+
end
2324

2425
local mock_device = test.mock_device.build_test_matter_device({
2526
profile = t_utils.get_profile_definition("plug-level-power-energy-powerConsumption.yml"),
@@ -183,64 +184,54 @@ test.register_coroutine_test(
183184
end
184185
)
185186

186-
test.register_message_test(
187-
"On command should send the appropriate commands",
188-
{
189-
channel = "devices",
190-
direction = "send",
191-
message = {
192-
"register_native_capability_cmd_handler",
193-
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "on" }
194-
}
195-
},
196-
{
197-
{
198-
channel = "capability",
199-
direction = "receive",
200-
message = {
201-
mock_device.id,
202-
{ capability = "switch", component = "main", command = "on", args = { } }
203-
}
204-
},
205-
{
206-
channel = "matter",
207-
direction = "send",
208-
message = {
209-
mock_device.id,
210-
clusters.OnOff.server.commands.On(mock_device, 2)
211-
}
212-
}
213-
}
187+
test.register_coroutine_test(
188+
"On command should send the appropriate commands", function()
189+
test.socket.capability:__queue_receive(
190+
{
191+
mock_device.id,
192+
{ capability = "switch", component = "main", command = "on", args = { } }
193+
}
194+
)
195+
if version.api >= 11 then
196+
test.socket.devices:__expect_send(
197+
{
198+
"register_native_capability_cmd_handler",
199+
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "on" }
200+
}
201+
)
202+
end
203+
test.socket.matter:__expect_send(
204+
{
205+
mock_device.id,
206+
clusters.OnOff.server.commands.On(mock_device, 2)
207+
}
208+
)
209+
end
214210
)
215211

216-
test.register_message_test(
217-
"Off command should send the appropriate commands",
218-
{
219-
channel = "devices",
220-
direction = "send",
221-
message = {
222-
"register_native_capability_cmd_handler",
223-
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "off" }
224-
}
225-
},
226-
{
227-
{
228-
channel = "capability",
229-
direction = "receive",
230-
message = {
212+
test.register_coroutine_test(
213+
"Off command should send the appropriate commands", function()
214+
test.socket.capability:__queue_receive(
215+
{
231216
mock_device.id,
232217
{ capability = "switch", component = "main", command = "off", args = { } }
233218
}
234-
},
235-
{
236-
channel = "matter",
237-
direction = "send",
238-
message = {
219+
)
220+
if version.api >= 11 then
221+
test.socket.devices:__expect_send(
222+
{
223+
"register_native_capability_cmd_handler",
224+
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "off" }
225+
}
226+
)
227+
end
228+
test.socket.matter:__expect_send(
229+
{
239230
mock_device.id,
240231
clusters.OnOff.server.commands.Off(mock_device, 2)
241232
}
242-
}
243-
}
233+
)
234+
end
244235
)
245236

246237
test.register_message_test(
@@ -681,68 +672,57 @@ test.register_coroutine_test(
681672
{ test_init = test_init_periodic }
682673
)
683674

684-
test.register_message_test(
685-
"Set level command should send the appropriate commands",
686-
{
687-
{
688-
channel = "capability",
689-
direction = "receive",
690-
message = {
675+
test.register_coroutine_test(
676+
"Set level command should send the appropriate commands", function()
677+
test.socket.capability:__queue_receive(
678+
{
691679
mock_device.id,
692680
{ capability = "switchLevel", component = "main", command = "setLevel", args = {20,20} }
693681
}
694-
},
695-
{
696-
channel = "devices",
697-
direction = "send",
698-
message = {
699-
"register_native_capability_cmd_handler",
700-
{ device_uuid = mock_device.id, capability_id = "switchLevel", capability_cmd_id = "setLevel" }
701-
}
702-
},
703-
{
704-
channel = "matter",
705-
direction = "send",
706-
message = {
682+
)
683+
if version.api >= 11 then
684+
test.socket.devices:__expect_send(
685+
{
686+
"register_native_capability_cmd_handler",
687+
{ device_uuid = mock_device.id, capability_id = "switchLevel", capability_cmd_id = "setLevel" }
688+
}
689+
)
690+
end
691+
test.socket.matter:__expect_send(
692+
{
707693
mock_device.id,
708694
clusters.LevelControl.server.commands.MoveToLevelWithOnOff(mock_device, 2, math.floor(20/100.0 * 254), 20, 0 ,0)
709695
}
710-
},
711-
{
712-
channel = "matter",
713-
direction = "receive",
714-
message = {
696+
)
697+
test.socket.matter:__queue_receive(
698+
{
715699
mock_device.id,
716700
clusters.LevelControl.server.commands.MoveToLevelWithOnOff:build_test_command_response(mock_device, 2)
717701
}
718-
},
719-
{
720-
channel = "matter",
721-
direction = "receive",
722-
message = {
702+
)
703+
test.socket.matter:__queue_receive(
704+
{
723705
mock_device.id,
724706
clusters.LevelControl.attributes.CurrentLevel:build_test_report_data(mock_device, 2, 50)
725707
}
726-
},
727-
{
728-
channel = "capability",
729-
direction = "send",
730-
message = mock_device:generate_test_message("main", capabilities.switchLevel.level(20))
731-
},
732-
{
733-
channel = "matter",
734-
direction = "receive",
735-
message = {
708+
)
709+
test.socket.capability:__expect_send(
710+
mock_device:generate_test_message(
711+
"main", capabilities.switchLevel.level(20)
712+
)
713+
)
714+
test.socket.matter:__queue_receive(
715+
{
736716
mock_device.id,
737717
clusters.OnOff.attributes.OnOff:build_test_report_data(mock_device, 2, true)
738718
}
739-
},
740-
{
741-
channel = "capability",
742-
direction = "send",
743-
message = mock_device:generate_test_message("main", capabilities.switch.switch.on())
744-
}
745-
}
719+
)
720+
test.socket.capability:__expect_send(
721+
mock_device:generate_test_message(
722+
"main", capabilities.switch.switch.on()
723+
)
724+
)
725+
end
746726
)
747727

748728
test.run_registered_tests()

0 commit comments

Comments
 (0)