diff --git a/drivers/SmartThings/matter-sensor/src/init.lua b/drivers/SmartThings/matter-sensor/src/init.lua index b05fb66a2b..8f1cf25034 100644 --- a/drivers/SmartThings/matter-sensor/src/init.lua +++ b/drivers/SmartThings/matter-sensor/src/init.lua @@ -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 = "" @@ -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) @@ -101,7 +107,6 @@ end local matter_driver_template = { lifecycle_handlers = { init = device_init, - doConfigure = do_configure, infoChanged = info_changed }, matter_handlers = { diff --git a/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor.lua b/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor.lua index 7605e55400..321b4f30c1 100644 --- a/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor.lua +++ b/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor.lua @@ -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) diff --git a/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor_featuremap.lua b/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor_featuremap.lua index ef177bcb6d..2c7bb7faef 100644 --- a/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor_featuremap.lua +++ b/drivers/SmartThings/matter-sensor/src/test/test_matter_sensor_featuremap.lua @@ -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() @@ -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() @@ -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 } )