Skip to content

Commit

Permalink
Apply a review that generalizes the implementation related to AQARA S…
Browse files Browse the repository at this point in the history
…WITCH DEVICE
  • Loading branch information
DongHoon-Ryu committed Jan 16, 2025
1 parent b7c70d3 commit 35a7f08
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
36 changes: 18 additions & 18 deletions drivers/SmartThings/matter-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ local COMPONENT_TO_ENDPOINT_MAP = "__component_to_endpoint_map"
-- containing both button endpoints and switch endpoints will use this field
-- rather than COMPONENT_TO_ENDPOINT_MAP.
local COMPONENT_TO_ENDPOINT_MAP_BUTTON = "__component_to_endpoint_map_button"
local ENERGY_MANAGEMENT_ENDPOINT = "__energy_management_endpoint"
local IS_PARENT_CHILD_DEVICE = "__is_parent_child_device"
local IS_AQARA_SWITCH_DEVICE = "__is_aqara_switch_device"
local COLOR_TEMP_BOUND_RECEIVED = "__colorTemp_bound_received"
local COLOR_TEMP_MIN = "__color_temp_min"
local COLOR_TEMP_MAX = "__color_temp_max"
Expand Down Expand Up @@ -153,8 +153,7 @@ local device_type_attribute_map = {
[ELECTRICAL_SENSOR_ID] = {
clusters.ElectricalPowerMeasurement.attributes.ActivePower,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported,
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported
}
}

Expand Down Expand Up @@ -417,7 +416,7 @@ local function find_default_endpoint(device)
return device.MATTER_DEFAULT_ENDPOINT
end

local function assign_child_profile(device, child_ep, ep_sequence)
local function assign_child_profile(device, child_ep)
local profile

-- check if device has an overridden child profile that differs from the profile
Expand All @@ -426,15 +425,13 @@ local function assign_child_profile(device, child_ep, ep_sequence)
if device.manufacturer_info.vendor_id == fingerprint.vendor_id and
device.manufacturer_info.product_id == fingerprint.product_id then
if device.manufacturer_info.vendor_id == AQARA_MANUFACTURER_ID then
if ep_sequence == 1 then
if child_ep ~= 1 then
-- To add Electrical Sensor only to the first EDGE_CHILD(light-power-energy-powerConsumption)
-- The profile of the second EDGE_CHILD is determined in the "for" loop below (e.g., light-binary)
device:set_field(IS_AQARA_SWITCH_DEVICE, true, {persist = true})
return fingerprint.child_profile
break
end
else
return fingerprint.child_profile
end
return fingerprint.child_profile
end
end

Expand Down Expand Up @@ -561,14 +558,16 @@ local function initialize_switch(driver, device)
component_map_used = true
end

local ep_sequence = 0
for _, ep in ipairs(switch_eps) do
if _ == 1 then
-- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
device:set_field(ENERGY_MANAGEMENT_ENDPOINT, ep)
end
if device:supports_server_cluster(clusters.OnOff.ID, ep) then
num_switch_server_eps = num_switch_server_eps + 1
if ep ~= main_endpoint then -- don't create a child device that maps to the main endpoint
ep_sequence = ep_sequence + 1
local name = string.format("%s %d", device.label, num_switch_server_eps)
local child_profile = assign_child_profile(device, ep, ep_sequence)
local child_profile = assign_child_profile(device, ep)
driver:try_create_device(
{
type = "EDGE_CHILD",
Expand Down Expand Up @@ -708,9 +707,7 @@ local function device_init(driver, device)
end
local main_endpoint = find_default_endpoint(device)
for _, ep in ipairs(device.endpoints) do
if ep.endpoint_id ~= main_endpoint and
(device:get_field(IS_AQARA_SWITCH_DEVICE) or ep.endpoint_id ~= 0) then
-- insert energy management into InteractionRequest list when IS_AQARA_SWITCH_DEVICE
if ep.endpoint_id ~= main_endpoint then
local id = 0
for _, dt in ipairs(ep.device_types) do
id = math.max(id, dt.device_type_id)
Expand Down Expand Up @@ -1083,10 +1080,13 @@ end
local function active_power_handler(driver, device, ib, response)
if ib.data.value then
local watt_value = ib.data.value / CONVERSION_CONST_MILLIWATT_TO_WATT
if device:get_field(IS_AQARA_SWITCH_DEVICE) then
device:emit_event_for_endpoint(1, capabilities.powerMeter.power({ value = watt_value, unit = "W"}))
if ib.endpoint_id ~= 0 then
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.powerMeter.power({ value = watt_value, unit = "W"}))
else
device:emit_event(capabilities.powerMeter.power({ value = watt_value, unit = "W"}))
-- when energy management is defined in the root endpoint(0), replace it with the first switch endpoint and process it.
-- In other words, define the capability related to energy management in the first switch endpoint and process it.
-- ENERGY_MANAGEMENT_ENDPOINT = first switch endpoint
device:emit_event_for_endpoint(device:get_field(ENERGY_MANAGEMENT_ENDPOINT), capabilities.powerMeter.power({ value = watt_value, unit = "W"}))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ local clusters = require "st.matter.clusters"
local button_attr = capabilities.button.button

local DEFERRED_CONFIGURE = "__DEFERRED_CONFIGURE"
local TEST_CONFIGURE = "__test_configure"

local aqara_parent_ep = 4
local aqara_child1_ep = 1
Expand Down Expand Up @@ -147,7 +146,6 @@ local cumulative_report_val_39 = {

local function test_init()
local opts = { persist = true }
aqara_mock_device:set_field(TEST_CONFIGURE, true, opts)
local cluster_subscribe_list = {
clusters.OnOff.attributes.OnOff,
clusters.Switch.server.events.InitialPress,
Expand All @@ -156,8 +154,7 @@ local function test_init()
clusters.Switch.server.events.MultiPressComplete,
clusters.ElectricalPowerMeasurement.attributes.ActivePower,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported,
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported
}
local subscribe_request = cluster_subscribe_list[1]:subscribe(aqara_mock_device)
for i, cluster in ipairs(cluster_subscribe_list) do
Expand Down Expand Up @@ -270,18 +267,21 @@ test.register_coroutine_test(
function()
test.socket.matter:__queue_receive(
{
aqara_mock_children[aqara_child1_ep].id,
-- don't use "aqara_mock_children[aqara_child1_ep].id,"
-- because energy management is at the root endpoint.
aqara_mock_device.id,
clusters.ElectricalPowerMeasurement.attributes.ActivePower:build_test_report_data(aqara_mock_device, 1, 17000)
}
)

test.socket.capability:__expect_send(
-- when energy management is in the root endpoint, the event is sent to the first switch endpoint in CHILD_EDGE.
aqara_mock_children[aqara_child1_ep]:generate_test_message("main", capabilities.powerMeter.power({value = 17.0, unit="W"}))
)

test.socket.matter:__queue_receive(
{
aqara_mock_children[aqara_child1_ep].id,
aqara_mock_device.id,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data(aqara_mock_device, 1, cumulative_report_val_19)
}
)
Expand All @@ -294,7 +294,7 @@ test.register_coroutine_test(
-- This is because related variable settings are required in set_poll_report_timer_and_schedule().
test.socket.matter:__queue_receive(
{
aqara_mock_children[aqara_child1_ep].id,
aqara_mock_device.id,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data(aqara_mock_device, 1, cumulative_report_val_29)
}
)
Expand All @@ -304,6 +304,16 @@ test.register_coroutine_test(
)

test.mock_time.advance_time(2000)
test.socket.matter:__queue_receive(
{
aqara_mock_device.id,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data(
aqara_mock_device, 1, cumulative_report_val_39
)
}
)
--[[
-- To do : powerConsumptionReport
test.socket.capability:__expect_send(
aqara_mock_children[aqara_child1_ep]:generate_test_message("main", capabilities.powerConsumptionReport.powerConsumption({
start = "1970-01-01T00:00:00Z",
Expand All @@ -312,16 +322,7 @@ test.register_coroutine_test(
energy = 29.0
}))
)

test.socket.matter:__queue_receive(
{
aqara_mock_children[aqara_child1_ep].id,
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data(
aqara_mock_device, 1, cumulative_report_val_39
)
}
)

--]]
test.socket.capability:__expect_send(
aqara_mock_children[aqara_child1_ep]:generate_test_message("main", capabilities.energyMeter.energy({ value = 39.0, unit = "Wh" }))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local t_utils = require "integration_test.utils"

local clusters = require "st.matter.clusters"

clusters.ElectricalEnergyMeasurement = require "ElectricalEnergyMeasurement"
clusters.ElectricalPowerMeasurement = require "ElectricalPowerMeasurement"

local mock_device = test.mock_device.build_test_matter_device({
profile = t_utils.get_profile_definition("plug-power-energy-powerConsumption.yml"),
manufacturer_info = {
Expand Down

0 comments on commit 35a7f08

Please sign in to comment.