Skip to content

Commit 4ea4089

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 4ea4089

17 files changed

+539
-568
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_climate_sensor_w100.lua

Lines changed: 2 additions & 2 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.
@@ -18,8 +18,8 @@ local capabilities = require "st.capabilities"
1818
local utils = require "st.utils"
1919
local dkjson = require "dkjson"
2020
local uint32 = require "st.matter.data_types.Uint32"
21-
2221
local clusters = require "st.matter.generated.zap_clusters"
22+
2323
local button_attr = capabilities.button.button
2424

2525
-- Mock a 3-button device with temperature and humidity sensor

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
-- Copyright 2025 SmartThings
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
115
local test = require "integration_test"
216
test.add_package_capability("cubeAction.yml")
317
test.add_package_capability("cubeFace.yml")
418
local capabilities = require "st.capabilities"
519
local cubeAction = capabilities["stse.cubeAction"]
620
local cubeFace = capabilities["stse.cubeFace"]
7-
821
local t_utils = require "integration_test.utils"
922
local clusters = require "st.matter.clusters"
1023

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

Lines changed: 7 additions & 6 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,12 +17,15 @@ 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"
22-
local button_attr = capabilities.button.button
21+
local version = require "version"
2322

24-
local DEFERRED_CONFIGURE = "__DEFERRED_CONFIGURE"
23+
if version.api < 11 then
24+
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
25+
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
26+
end
2527

28+
local button_attr = capabilities.button.button
2629
local aqara_parent_ep = 4
2730
local aqara_child1_ep = 1
2831
local aqara_child2_ep = 2
@@ -159,7 +162,6 @@ local function configure_buttons()
159162
end
160163

161164
local function test_init()
162-
local opts = { persist = true }
163165
local cluster_subscribe_list = {
164166
clusters.OnOff.attributes.OnOff,
165167
clusters.Switch.server.events.InitialPress,
@@ -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 & 101 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,13 @@
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"
19+
local version = require "version"
2020

21-
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
22-
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
21+
if version.api < 11 then
22+
clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
23+
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"
24+
end
2325

2426
local mock_device = test.mock_device.build_test_matter_device({
2527
profile = t_utils.get_profile_definition("plug-level-power-energy-powerConsumption.yml"),
@@ -183,64 +185,54 @@ test.register_coroutine_test(
183185
end
184186
)
185187

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-
}
188+
test.register_coroutine_test(
189+
"On command should send the appropriate commands", function()
190+
test.socket.capability:__queue_receive(
191+
{
192+
mock_device.id,
193+
{ capability = "switch", component = "main", command = "on", args = { } }
194+
}
195+
)
196+
if version.api >= 11 then
197+
test.socket.devices:__expect_send(
198+
{
199+
"register_native_capability_cmd_handler",
200+
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "on" }
201+
}
202+
)
203+
end
204+
test.socket.matter:__expect_send(
205+
{
206+
mock_device.id,
207+
clusters.OnOff.server.commands.On(mock_device, 2)
208+
}
209+
)
210+
end
214211
)
215212

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 = {
213+
test.register_coroutine_test(
214+
"Off command should send the appropriate commands", function()
215+
test.socket.capability:__queue_receive(
216+
{
231217
mock_device.id,
232218
{ capability = "switch", component = "main", command = "off", args = { } }
233219
}
234-
},
235-
{
236-
channel = "matter",
237-
direction = "send",
238-
message = {
220+
)
221+
if version.api >= 11 then
222+
test.socket.devices:__expect_send(
223+
{
224+
"register_native_capability_cmd_handler",
225+
{ device_uuid = mock_device.id, capability_id = "switch", capability_cmd_id = "off" }
226+
}
227+
)
228+
end
229+
test.socket.matter:__expect_send(
230+
{
239231
mock_device.id,
240232
clusters.OnOff.server.commands.Off(mock_device, 2)
241233
}
242-
}
243-
}
234+
)
235+
end
244236
)
245237

246238
test.register_message_test(
@@ -681,68 +673,57 @@ test.register_coroutine_test(
681673
{ test_init = test_init_periodic }
682674
)
683675

684-
test.register_message_test(
685-
"Set level command should send the appropriate commands",
686-
{
687-
{
688-
channel = "capability",
689-
direction = "receive",
690-
message = {
676+
test.register_coroutine_test(
677+
"Set level command should send the appropriate commands", function()
678+
test.socket.capability:__queue_receive(
679+
{
691680
mock_device.id,
692681
{ capability = "switchLevel", component = "main", command = "setLevel", args = {20,20} }
693682
}
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 = {
683+
)
684+
if version.api >= 11 then
685+
test.socket.devices:__expect_send(
686+
{
687+
"register_native_capability_cmd_handler",
688+
{ device_uuid = mock_device.id, capability_id = "switchLevel", capability_cmd_id = "setLevel" }
689+
}
690+
)
691+
end
692+
test.socket.matter:__expect_send(
693+
{
707694
mock_device.id,
708695
clusters.LevelControl.server.commands.MoveToLevelWithOnOff(mock_device, 2, math.floor(20/100.0 * 254), 20, 0 ,0)
709696
}
710-
},
711-
{
712-
channel = "matter",
713-
direction = "receive",
714-
message = {
697+
)
698+
test.socket.matter:__queue_receive(
699+
{
715700
mock_device.id,
716701
clusters.LevelControl.server.commands.MoveToLevelWithOnOff:build_test_command_response(mock_device, 2)
717702
}
718-
},
719-
{
720-
channel = "matter",
721-
direction = "receive",
722-
message = {
703+
)
704+
test.socket.matter:__queue_receive(
705+
{
723706
mock_device.id,
724707
clusters.LevelControl.attributes.CurrentLevel:build_test_report_data(mock_device, 2, 50)
725708
}
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 = {
709+
)
710+
test.socket.capability:__expect_send(
711+
mock_device:generate_test_message(
712+
"main", capabilities.switchLevel.level(20)
713+
)
714+
)
715+
test.socket.matter:__queue_receive(
716+
{
736717
mock_device.id,
737718
clusters.OnOff.attributes.OnOff:build_test_report_data(mock_device, 2, true)
738719
}
739-
},
740-
{
741-
channel = "capability",
742-
direction = "send",
743-
message = mock_device:generate_test_message("main", capabilities.switch.switch.on())
744-
}
745-
}
720+
)
721+
test.socket.capability:__expect_send(
722+
mock_device:generate_test_message(
723+
"main", capabilities.switch.switch.on()
724+
)
725+
)
726+
end
746727
)
747728

748729
test.run_registered_tests()

0 commit comments

Comments
 (0)