Skip to content

Commit

Permalink
Merge pull request #1300 from MaXinjian/reonboard
Browse files Browse the repository at this point in the history
Add TC to improve motion sensor code test coverage
  • Loading branch information
greens authored Apr 22, 2024
2 parents 02ece0e + 8778a28 commit 09589f5
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ local detectionFrequency = capabilities["stse.detectionFrequency"]

local PowerConfiguration = clusters.PowerConfiguration
local OccupancySensing = clusters.OccupancySensing
local PREF_FREQUENCY_KEY = "prefFrequency"
local PREF_FREQUENCY_VALUE_DEFAULT = 60
local PRIVATE_CLUSTER_ID = 0xFCC0
local PRIVATE_ATTRIBUTE_ID = 0x0009
local PREF_CHANGED_KEY = "prefChangedKey"
local PREF_CHANGED_VALUE = "prefChangedValue"
local PREF_SENSITIVITY_KEY = "prefSensitivity"
local PREF_SENSITIVITY_VALUE_HIGH = 3
local PREF_SENSITIVITY_VALUE_MEDIUM = 2
local PREF_SENSITIVITY_VALUE_LOW = 1
local SENSITIVITY_ATTRIBUTE_ID = 0x010C
local MFG_CODE = 0x115F

-- Needed for building ConfigureReportingResponse msg
local messages = require "st.zigbee.messages"
local zb_const = require "st.zigbee.constants"
local write_attribute_response = require "st.zigbee.zcl.global_commands.write_attribute_response"
local zcl_messages = require "st.zigbee.zcl"

local mock_device = test.mock_device.build_test_zigbee_device(
{
profile = t_utils.get_profile_definition("motion-battery-aqara.yml"),
Expand Down Expand Up @@ -113,4 +127,143 @@ test.register_coroutine_test(
end
)

test.register_coroutine_test(
"Handle occupancy attr",
function()
local detect_duration = mock_device:get_field(0x0102) or 120
test.timer.__create_and_queue_test_time_advance_timer(detect_duration, "oneshot")
local attr_report_data = {
{ OccupancySensing.attributes.Occupancy.ID, data_types.Int32.ID, 0x0001 }
}
test.socket.zigbee:__queue_receive({
mock_device.id,
zigbee_test_utils.build_attribute_report(mock_device, OccupancySensing.ID, attr_report_data, MFG_CODE)
})
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", capabilities.motionSensor.motion.active())
)

test.mock_time.advance_time(detect_duration)
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
capabilities.motionSensor.motion.inactive()))
end
)

local function build_write_attr_res(cluster, status)
local addr_header = messages.AddressHeader(
mock_device:get_short_address(),
mock_device.fingerprinted_endpoint_id,
zb_const.HUB.ADDR,
zb_const.HUB.ENDPOINT,
zb_const.HA_PROFILE_ID,
cluster
)

local write_attribute_body = write_attribute_response.WriteAttributeResponse(status, {})
local zcl_header = zcl_messages.ZclHeader({
cmd = data_types.ZCLCommandId(write_attribute_body.ID)
})

local message_body = zcl_messages.ZclMessageBody({
zcl_header = zcl_header,
zcl_body = write_attribute_body
})
return messages.ZigbeeMessageRx({
address_header = addr_header,
body = message_body
})
end

test.register_coroutine_test(
"Handle write attr res: detectionFrequency",
function()
test.socket.zigbee:__queue_receive({
mock_device.id,
build_write_attr_res(PRIVATE_CLUSTER_ID, 0x00)
})
mock_device:set_field(PREF_CHANGED_KEY, PREF_FREQUENCY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_FREQUENCY_VALUE_DEFAULT)
local value = mock_device:get_field(PREF_CHANGED_VALUE) or 0
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
detectionFrequency.detectionFrequency(value, {visibility = {displayed = false}})))
end
)

test.register_coroutine_test(
"Handle write attr res: sensitivityAdjustment High",
function()
test.socket.zigbee:__queue_receive({
mock_device.id,
build_write_attr_res(PRIVATE_CLUSTER_ID, 0x00)
})
mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_HIGH)
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
sensitivityAdjustment.sensitivityAdjustment.High()))
end
)

test.register_coroutine_test(
"Handle write attr res: sensitivityAdjustment Medium",
function()
test.socket.zigbee:__queue_receive({
mock_device.id,
build_write_attr_res(PRIVATE_CLUSTER_ID, 0x00)
})
mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_MEDIUM)
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
sensitivityAdjustment.sensitivityAdjustment.Medium()))
end
)

test.register_coroutine_test(
"Handle write attr res: sensitivityAdjustment Low",
function()
test.socket.zigbee:__queue_receive({
mock_device.id,
build_write_attr_res(PRIVATE_CLUSTER_ID, 0x00)
})
mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_LOW)
test.socket.capability:__expect_send(mock_device:generate_test_message("main",
sensitivityAdjustment.sensitivityAdjustment.Low()))
end
)

test.register_coroutine_test(
"Handle sensitivity adjustment capability",
function()
mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_HIGH)
test.socket.capability:__queue_receive({ mock_device.id,
{ capability = "stse.sensitivityAdjustment", component = "main", command = "setSensitivityAdjustment", args = {"High"} } })

test.socket.zigbee:__expect_send({ mock_device.id,
cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, SENSITIVITY_ATTRIBUTE_ID, MFG_CODE
, data_types.Uint8, PREF_SENSITIVITY_VALUE_HIGH)
})

mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_MEDIUM)
test.socket.capability:__queue_receive({ mock_device.id,
{ capability = "stse.sensitivityAdjustment", component = "main", command = "setSensitivityAdjustment", args = {"Medium"} } })

test.socket.zigbee:__expect_send({ mock_device.id,
cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, SENSITIVITY_ATTRIBUTE_ID, MFG_CODE
, data_types.Uint8, PREF_SENSITIVITY_VALUE_MEDIUM)
})

mock_device:set_field(PREF_CHANGED_KEY, PREF_SENSITIVITY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_SENSITIVITY_VALUE_LOW)
test.socket.capability:__queue_receive({ mock_device.id,
{ capability = "stse.sensitivityAdjustment", component = "main", command = "setSensitivityAdjustment", args = {"Low"} } })

test.socket.zigbee:__expect_send({ mock_device.id,
cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, SENSITIVITY_ATTRIBUTE_ID, MFG_CODE
, data_types.Uint8, PREF_SENSITIVITY_VALUE_LOW)
})
end
)

test.run_registered_tests()
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ test.add_package_capability("detectionFrequency.yaml")
local detectionFrequency = capabilities["stse.detectionFrequency"]

local PowerConfiguration = clusters.PowerConfiguration
local PREF_FREQUENCY_KEY = "prefFrequency"
local PREF_FREQUENCY_VALUE_DEFAULT = 60
local PREF_CHANGED_KEY = "prefChangedKey"
local PREF_CHANGED_VALUE = "prefChangedValue"
local FREQUENCY_ATTRIBUTE_ID = 0x0102
local PRIVATE_CLUSTER_ID = 0xFCC0
local PRIVATE_ATTRIBUTE_ID = 0x0009
local MOTION_ILLUMINANCE_ATTRIBUTE_ID = 0x0112
Expand Down Expand Up @@ -115,4 +119,20 @@ test.register_coroutine_test(
end
)

test.register_coroutine_test(
"Handle detection frequency capability",
function()
test.socket.capability:__queue_receive({ mock_device.id,
{ capability = "stse.detectionFrequency", component = "main", command = "setDetectionFrequency", args = {60} } })

mock_device:set_field(PREF_CHANGED_KEY, PREF_FREQUENCY_KEY)
mock_device:set_field(PREF_CHANGED_VALUE, PREF_FREQUENCY_VALUE_DEFAULT)

test.socket.zigbee:__expect_send({ mock_device.id,
cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, FREQUENCY_ATTRIBUTE_ID, MFG_CODE
, data_types.Uint8, PREF_FREQUENCY_VALUE_DEFAULT)
})
end
)

test.run_registered_tests()

0 comments on commit 09589f5

Please sign in to comment.