Skip to content

Commit

Permalink
Change read request location to avoid race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolas-deboom committed Dec 10, 2024
1 parent 60e1d59 commit 061a1d0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
13 changes: 6 additions & 7 deletions drivers/SmartThings/matter-sensor/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,6 @@ end

local function device_added(driver, device)
set_boolean_device_type_per_endpoint(driver, device)

local battery_feature_eps = device:get_endpoints(clusters.PowerSource.ID, {feature_bitmap = clusters.PowerSource.types.PowerSourceFeature.BATTERY})
if #battery_feature_eps > 0 and device:get_field(SUPPORT_BATTERY_PERCENTAGE) == nil then
local attribute_list_read = im.InteractionRequest(im.InteractionRequest.RequestType.READ, {})
attribute_list_read:merge(clusters.PowerSource.attributes.AttributeList:read())
device:send(attribute_list_read)
end
end

local function match_profile(driver, device)
Expand Down Expand Up @@ -305,6 +298,12 @@ end

local function do_configure(driver, device)
match_profile(driver, device)
local battery_feature_eps = device:get_endpoints(clusters.PowerSource.ID, {feature_bitmap = clusters.PowerSource.types.PowerSourceFeature.BATTERY})
if #battery_feature_eps > 0 then
local attribute_list_read = im.InteractionRequest(im.InteractionRequest.RequestType.READ, {})
attribute_list_read:merge(clusters.PowerSource.attributes.AttributeList:read())
device:send(attribute_list_read)
end
end

local function device_init(driver, device)
Expand Down
4 changes: 0 additions & 4 deletions drivers/SmartThings/matter-thermostat/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,6 @@ end

local function do_configure(driver, device)
match_profile(driver, device)
end

local function device_added(driver, device)
local req = im.InteractionRequest(im.InteractionRequest.RequestType.READ, {})
req:merge(clusters.Thermostat.attributes.ControlSequenceOfOperation:read(device))
req:merge(clusters.FanControl.attributes.FanModeSequence:read(device))
Expand Down Expand Up @@ -1366,7 +1363,6 @@ end
local matter_driver_template = {
lifecycle_handlers = {
init = device_init,
added = device_added,
doConfigure = do_configure,
infoChanged = info_changed,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device_ap_aqs.id, "doConfigure" })
mock_device_ap_aqs:expect_metadata_update({ profile = "air-purifier-hepa-ac-aqs-co2-tvoc-meas-co2-radon-level" })
mock_device_ap_aqs:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device_ap_aqs.id, read_req})
end,
{ test_init = test_init_ap_aqs }
)
Expand All @@ -448,6 +453,11 @@ test.register_coroutine_test(
mock_device_ap_thermo_aqs:expect_metadata_update({ profile = "air-purifier-hepa-ac-rock-wind-thermostat-humidity-fan-heating-only-nostate-nobattery-aqs-pm10-pm25-ch2o-meas-pm10-pm25-ch2o-no2-tvoc-level" })
mock_device_ap_thermo_aqs:expect_metadata_update({ provisioning_state = "PROVISIONED" })
print(mock_device_ap_thermo_aqs.profile)
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device_ap_thermo_aqs.id, read_req})
end,
{ test_init = test_init_ap_thermo_aqs }
)
Expand Down
10 changes: 10 additions & 0 deletions drivers/SmartThings/matter-thermostat/src/test/test_matter_fan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" })
mock_device:expect_metadata_update({ profile = "fan-rock-wind" })
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device.id, read_req})
end,
{ test_init = test_init }
)
Expand All @@ -125,6 +130,11 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device_generic.id, "doConfigure" })
mock_device_generic:expect_metadata_update({ profile = "fan-generic" })
mock_device_generic:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device_generic.id, read_req})
end,
{ test_init = test_init_generic }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device_configure.id, "doConfigure" })
mock_device_configure:expect_metadata_update({ profile = "room-air-conditioner" })
mock_device_configure:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device_configure.id, read_req})

end,
{ test_init = test_init_configure }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ test.register_coroutine_test(
--TODO why does provisiong state get added in the do configure event handle, but not the refres?
mock_device:expect_metadata_update({ profile = "thermostat-humidity-fan-heating-only-nostate-batteryLevel" })
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
read_req:merge(clusters.PowerSource.attributes.AttributeList:read())
test.socket.matter:__expect_send({mock_device.id, read_req})
end
)

Expand All @@ -239,6 +245,12 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device_simple.id, "doConfigure" })
mock_device_simple:expect_metadata_update({ profile = "thermostat-cooling-only-nostate-batteryLevel" })
mock_device_simple:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
read_req:merge(clusters.PowerSource.attributes.AttributeList:read())
test.socket.matter:__expect_send({mock_device_simple.id, read_req})
end
)

Expand All @@ -248,6 +260,11 @@ test.register_coroutine_test(
test.socket.device_lifecycle:__queue_receive({ mock_device_no_battery.id, "doConfigure" })
mock_device_no_battery:expect_metadata_update({ profile = "thermostat-cooling-only-nostate-nobattery" })
mock_device_no_battery:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
test.socket.matter:__expect_send({mock_device_no_battery.id, read_req})
end
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ local function configure(device)
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" })
mock_device:expect_metadata_update({ profile = "thermostat-nostate-batteryLevel" })
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })

local read_req = clusters.Thermostat.attributes.ControlSequenceOfOperation:read()
read_req:merge(clusters.FanControl.attributes.FanModeSequence:read())
read_req:merge(clusters.FanControl.attributes.WindSupport:read())
read_req:merge(clusters.FanControl.attributes.RockSupport:read())
read_req:merge(clusters.PowerSource.attributes.AttributeList:read())
test.socket.matter:__expect_send({mock_device.id, read_req})

test.wait_for_events()

--populate cached setpoint values. This would normally happen due to subscription setup.
Expand Down

0 comments on commit 061a1d0

Please sign in to comment.