Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ local fields = require "switch_utils.fields"
local switch_utils = require "switch_utils.utils"

return function(opts, driver, device)
local EVE_MANUFACTURER_ID = 0x130A
-- this sub driver does NOT support child devices, and ONLY supports Eve devices
-- that do NOT support the Electrical Sensor device type
local EVE_PRIVATE_CLUSTER_ID = 0x130AFC01
-- this sub driver loads for devices that:
-- 1. Contain the Eve Private Cluster (0x130AFC01)
-- 2. Do NOT have the Standard Electrical Sensor device type
if device.network_type == device_lib.NETWORK_TYPE_MATTER and
device.manufacturer_info.vendor_id == EVE_MANUFACTURER_ID and
#device:get_endpoints(EVE_PRIVATE_CLUSTER_ID) > 0 and
#switch_utils.get_endpoints_by_device_type(device, fields.DEVICE_TYPE_ID.ELECTRICAL_SENSOR) == 0 then
return true, require("sub_drivers.eve_energy")
end
Expand Down
84 changes: 84 additions & 0 deletions drivers/SmartThings/matter-switch/src/test/test_eve_energy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ local PRIVATE_ATTR_ID_WATT = 0x130A000A
local PRIVATE_ATTR_ID_WATT_ACCUMULATED = 0x130A000B
local PRIVATE_ATTR_ID_ACCUMULATED_CONTROL_POINT = 0x130A000E

-- Helper function to add get_endpoints method to mock devices
local function add_get_endpoints_to_mock(device)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: technically this does not match the lua library definition of the get_endpoints function, but I think it covers the test appropriately. The alternative would be to manually register each of the new mock devices using their own test init function so that the device function metatable is populated with functions like get_endpoints from the lua libs, but I think that is too much overhead for just doing a quick can_handle test for each of these mock devices, and this works well for the purposes of this test.

device.get_endpoints = function(self, cluster_id, opts)
opts = opts or {}
local eps = {}
for _, ep in ipairs(self.endpoints) do
for _, cluster in ipairs(ep.clusters or {}) do
if cluster.cluster_id == cluster_id then
-- Check feature_bitmap if specified
if opts.feature_bitmap == nil or
(cluster.feature_map and (cluster.feature_map & opts.feature_bitmap) == opts.feature_bitmap) then
table.insert(eps, ep.endpoint_id)
break
end
end
end
end
return eps
end
return device
end

local mock_device = test.mock_device.build_test_matter_device({
profile = t_utils.get_profile_definition("power-energy-powerConsumption.yml"),
manufacturer_info = {
Expand Down Expand Up @@ -53,6 +75,7 @@ local mock_device = test.mock_device.build_test_matter_device({
}
}
})
add_get_endpoints_to_mock(mock_device)

local mock_eve_device_using_electrical_sensor = test.mock_device.build_test_matter_device({
profile = t_utils.get_profile_definition("plug-energy-powerConsumption.yml"),
Expand Down Expand Up @@ -112,6 +135,42 @@ local mock_eve_device_using_electrical_sensor = test.mock_device.build_test_matt
}
}
})
add_get_endpoints_to_mock(mock_eve_device_using_electrical_sensor)

-- Mock device without Eve Private Cluster (should not match eve_energy sub-driver)
local mock_device_without_private_cluster = test.mock_device.build_test_matter_device({
profile = t_utils.get_profile_definition("plug-binary.yml"),
manufacturer_info = {
vendor_id = 0x130A,
product_id = 0x0051,
},
endpoints = {
{
endpoint_id = 0,
clusters = {
{ cluster_id = clusters.Basic.ID, cluster_type = "SERVER" },
},
device_types = {
{ device_type_id = 0x0016, device_type_revision = 1 } -- RootNode
}
},
{
endpoint_id = 1,
clusters = {
{
cluster_id = clusters.OnOff.ID,
cluster_type = "SERVER",
cluster_revision = 1,
feature_map = 0, --u32 bitmap
}
},
device_types = {
{ device_type_id = 0x010A, device_type_revision = 1 } -- On/Off Plug
}
}
}
})
add_get_endpoints_to_mock(mock_device_without_private_cluster)

local function test_init()
local cluster_subscribe_list = {
Expand All @@ -129,6 +188,31 @@ local function test_init()
end
test.set_test_init_function(test_init)

test.register_coroutine_test(
"Eve Energy sub-driver can_handle should return true for devices with Eve Private Cluster and no Electrical Sensor",
function()
local eve_energy_can_handle = require("sub_drivers.eve_energy.can_handle")
local result, sub_driver = eve_energy_can_handle(nil, nil, mock_device)
assert(result == true, "can_handle should return true for Eve device with private cluster and no electrical sensor")
assert(sub_driver ~= nil, "sub_driver should be returned")
end,
{
min_api_version = 17
}
)

test.register_coroutine_test(
"Eve Energy sub-driver can_handle should return false for devices without Eve Private Cluster",
function()
local eve_energy_can_handle = require("sub_drivers.eve_energy.can_handle")
local result = eve_energy_can_handle(nil, nil, mock_device_without_private_cluster)
assert(result == false, "can_handle should return false for device without Eve Private Cluster")
end,
{
min_api_version = 17
}
)

test.register_message_test(
"On command should send the appropriate commands",
{
Expand Down
Loading