Skip to content

Commit

Permalink
Matter Sensor: check for battery on init
Browse files Browse the repository at this point in the history
This moves the battery-checking logic from do_configure to init
so that device will not have to be removed and re-added in order
to join the correct profile.
  • Loading branch information
ctowns committed Sep 26, 2023
1 parent 211c9b1 commit 386353a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
17 changes: 11 additions & 6 deletions drivers/SmartThings/matter-sensor/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ local clusters = require "st.matter.clusters"
local MatterDriver = require "st.matter.driver"
local utils = require "st.utils"

local function device_init(driver, device)
log.info("device init")
device:subscribe()
end
local BATTERY_CHECKED = "__battery_checked"

local function do_configure(driver, device)
local function check_for_battery(device)
local battery_eps = device:get_endpoints(clusters.PowerSource.ID, {feature_bitmap = clusters.PowerSource.types.PowerSourceFeature.BATTERY})
local profile_name = ""

Expand Down Expand Up @@ -55,6 +52,15 @@ local function do_configure(driver, device)
profile_name = string.sub(profile_name, 2)

device:try_update_metadata({profile = profile_name})
device:set_field(BATTERY_CHECKED, 1, {persist = true})
end

local function device_init(driver, device)
log.info("device init")
if not device:get_field(BATTERY_CHECKED) then
check_for_battery(device)
end
device:subscribe()
end

local function info_changed(driver, device, event, args)
Expand Down Expand Up @@ -101,7 +107,6 @@ end
local matter_driver_template = {
lifecycle_handlers = {
init = device_init,
doConfigure = do_configure,
infoChanged = info_changed
},
matter_handlers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ end
local function test_init()
test.socket.matter:__expect_send({mock_device.id, subscribe_on_init(mock_device)})
test.mock_device.add_test_device(mock_device)
-- don't check the battery for this device because we are using the catch-all "sensor.yml" profile just for testing
mock_device:set_field("__battery_checked", 1, {persist = true})
end
test.set_test_init_function(test_init)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ local function test_init_humidity_battery()

test.socket.matter:__expect_send({mock_device_humidity_battery.id, subscribe_request_humidity_battery})
test.mock_device.add_test_device(mock_device_humidity_battery)
mock_device_humidity_battery:expect_metadata_update({ profile = "humidity-battery" })
end

local function test_init_humidity_no_battery()
Expand All @@ -139,6 +140,7 @@ local function test_init_humidity_no_battery()

test.socket.matter:__expect_send({mock_device_humidity_no_battery.id, subscribe_request_humidity_no_battery})
test.mock_device.add_test_device(mock_device_humidity_no_battery)
mock_device_humidity_no_battery:expect_metadata_update({ profile = "humidity" })
end

local function test_init_temp_humidity()
Expand All @@ -151,34 +153,26 @@ local function test_init_temp_humidity()

test.socket.matter:__expect_send({mock_device_temp_humidity.id, subscribe_request_temp_humidity})
test.mock_device.add_test_device(mock_device_temp_humidity)
mock_device_temp_humidity:expect_metadata_update({ profile = "temperature-humidity" })
end

test.register_coroutine_test(
"Profile remains the same for battery-supported devices on doConfigure lifecycle event due to cluster feature map",
"Test profile change on init for humidity sensor with battery",
function()
test.socket.device_lifecycle:__queue_receive({ mock_device_humidity_battery.id, "doConfigure" })
mock_device_humidity_battery:expect_metadata_update({ profile = "humidity-battery" })
mock_device_humidity_battery:expect_metadata_update({ provisioning_state = "PROVISIONED" })
end,
{ test_init = test_init_humidity_battery }
)

test.register_coroutine_test(
"Profile change to non-battery profile on doConfigure lifecycle event due to cluster feature map",
"Test profile change on init for humidity sensor without battery",
function()
test.socket.device_lifecycle:__queue_receive({ mock_device_humidity_no_battery.id, "doConfigure" })
mock_device_humidity_no_battery:expect_metadata_update({ profile = "humidity" })
mock_device_humidity_no_battery:expect_metadata_update({ provisioning_state = "PROVISIONED" })
end,
{ test_init = test_init_humidity_no_battery }
)

test.register_coroutine_test(
"Profile change to non-battery profile on doConfigure lifecycle event due to cluster feature map",
"Test profile change on init for temperature-humidity sensor",
function()
test.socket.device_lifecycle:__queue_receive({ mock_device_temp_humidity.id, "doConfigure" })
mock_device_temp_humidity:expect_metadata_update({ profile = "temperature-humidity" })
mock_device_temp_humidity:expect_metadata_update({ provisioning_state = "PROVISIONED" })
end,
{ test_init = test_init_temp_humidity }
)
Expand Down

0 comments on commit 386353a

Please sign in to comment.