Skip to content

Commit

Permalink
update: function names to reflect new attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
hcarter-775 committed Dec 17, 2024
1 parent c0703af commit ce99c6d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
60 changes: 30 additions & 30 deletions drivers/SmartThings/matter-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ local child_device_profile_overrides = {
local detect_matter_thing

local CUMULATIVE_REPORTS_NOT_SUPPORTED = "__cumulative_reports_not_supported"
local FIRST_EXPORT_REPORT_TIMESTAMP = "__first_export_report_timestamp"
local EXPORT_POLL_TIMER_SETTING_ATTEMPTED = "__export_poll_timer_setting_attempted"
local EXPORT_REPORT_TIMEOUT = "__export_report_timeout"
local TOTAL_EXPORTED_ENERGY = "__total_imported_energy"
local LAST_EXPORTED_REPORT_TIMESTAMP = "__last_imported_report_timestamp"
local RECURRING_EXPORT_REPORT_POLL_TIMER = "__recurring_export_report_poll_timer"
local FIRST_IMPORT_REPORT_TIMESTAMP = "__first_import_report_timestamp"
local IMPORT_POLL_TIMER_SETTING_ATTEMPTED = "__import_poll_timer_setting_attempted"
local IMPORT_REPORT_TIMEOUT = "__import_report_timeout"
local TOTAL_IMPORTED_ENERGY = "__total_imported_energy"
local LAST_IMPORTED_REPORT_TIMESTAMP = "__last_imported_report_timestamp"
local RECURRING_IMPORT_REPORT_POLL_TIMER = "__recurring_import_report_poll_timer"
local MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60) -- 15 minutes, reported in seconds
local SUBSCRIPTION_REPORT_OCCURRED = "__subscription_report_occurred"
local CONVERSION_CONST_MILLIWATT_TO_WATT = 1000 -- A milliwatt is 1/1000th of a watt
Expand All @@ -183,19 +183,19 @@ local function iso8061Timestamp(time)
return os.date("!%Y-%m-%dT%H:%M:%SZ", time)
end

local function delete_export_poll_schedule(device)
local export_poll_timer = device:get_field(RECURRING_EXPORT_REPORT_POLL_TIMER)
if export_poll_timer then
device.thread:cancel_timer(export_poll_timer)
device:set_field(RECURRING_EXPORT_REPORT_POLL_TIMER, nil)
device:set_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED, nil)
local function delete_import_poll_schedule(device)
local import_poll_timer = device:get_field(RECURRING_IMPORT_REPORT_POLL_TIMER)
if import_poll_timer then
device.thread:cancel_timer(import_poll_timer)
device:set_field(RECURRING_IMPORT_REPORT_POLL_TIMER, nil)
device:set_field(IMPORT_POLL_TIMER_SETTING_ATTEMPTED, nil)
end
end

local function send_export_poll_report(device, latest_total_imported_energy_wh)
local function send_import_poll_report(device, latest_total_imported_energy_wh)
local current_time = os.time()
local last_time = device:get_field(LAST_EXPORTED_REPORT_TIMESTAMP) or 0
device:set_field(LAST_EXPORTED_REPORT_TIMESTAMP, current_time, { persist = true })
local last_time = device:get_field(LAST_IMPORTED_REPORT_TIMESTAMP) or 0
device:set_field(LAST_IMPORTED_REPORT_TIMESTAMP, current_time, { persist = true })

-- Calculate the energy delta between reports
local energy_delta_wh = 0.0
Expand All @@ -215,12 +215,12 @@ local function send_export_poll_report(device, latest_total_imported_energy_wh)
end

local function create_poll_report_schedule(device)
local export_timer = device.thread:call_on_schedule(
device:get_field(EXPORT_REPORT_TIMEOUT),
send_export_poll_report(device, device:get_field(TOTAL_EXPORTED_ENERGY)),
"polling_export_report_schedule_timer"
local import_timer = device.thread:call_on_schedule(
device:get_field(IMPORT_REPORT_TIMEOUT),
send_import_poll_report(device, device:get_field(TOTAL_IMPORTED_ENERGY)),
"polling_import_report_schedule_timer"
)
device:set_field(RECURRING_EXPORT_REPORT_POLL_TIMER, export_timer)
device:set_field(RECURRING_IMPORT_REPORT_POLL_TIMER, import_timer)
end

local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
Expand All @@ -234,18 +234,18 @@ local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
return
elseif not device:get_field(SUBSCRIPTION_REPORT_OCCURRED) then
device:set_field(SUBSCRIPTION_REPORT_OCCURRED, true)
elseif not device:get_field(FIRST_EXPORT_REPORT_TIMESTAMP) then
device:set_field(FIRST_EXPORT_REPORT_TIMESTAMP, os.time())
elseif not device:get_field(FIRST_IMPORT_REPORT_TIMESTAMP) then
device:set_field(FIRST_IMPORT_REPORT_TIMESTAMP, os.time())
else
local first_timestamp = device:get_field(FIRST_EXPORT_REPORT_TIMESTAMP)
local first_timestamp = device:get_field(FIRST_IMPORT_REPORT_TIMESTAMP)
local second_timestamp = os.time()
local report_interval_secs = second_timestamp - first_timestamp
device:set_field(EXPORT_REPORT_TIMEOUT, math.max(report_interval_secs, MINIMUM_ST_ENERGY_REPORT_INTERVAL))
device:set_field(IMPORT_REPORT_TIMEOUT, math.max(report_interval_secs, MINIMUM_ST_ENERGY_REPORT_INTERVAL))
-- the poll schedule is only needed for devices that support powerConsumption
if device:supports_capability(capabilities.powerConsumptionReport) then
create_poll_report_schedule(device)
end
device:set_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED, true)
device:set_field(IMPORT_POLL_TIMER_SETTING_ATTEMPTED, true)
end
end

Expand Down Expand Up @@ -700,7 +700,7 @@ end

local function device_removed(driver, device)
log.info("device removed")
delete_export_poll_schedule(device)
delete_import_poll_schedule(device)
end

local function handle_switch_on(driver, device, cmd)
Expand Down Expand Up @@ -982,24 +982,24 @@ end
local function cumul_energy_imported_handler(driver, device, ib, response)
if ib.data.elements.energy then
local watt_hour_value = ib.data.elements.energy.value / CONVERSION_CONST_MILLIWATT_TO_WATT
device:set_field(TOTAL_EXPORTED_ENERGY, watt_hour_value)
device:set_field(TOTAL_IMPORTED_ENERGY, watt_hour_value)
device:emit_event(capabilities.energyMeter.energy({ value = watt_hour_value, unit = "Wh" }))
end
end

local function per_energy_imported_handler(driver, device, ib, response)
if ib.data.elements.energy then
local watt_hour_value = ib.data.elements.energy.value / CONVERSION_CONST_MILLIWATT_TO_WATT
local latest_energy_report = device:get_field(TOTAL_EXPORTED_ENERGY) or 0
local latest_energy_report = device:get_field(TOTAL_IMPORTED_ENERGY) or 0
local summed_energy_report = latest_energy_report + watt_hour_value
device:set_field(TOTAL_EXPORTED_ENERGY, summed_energy_report)
device:set_field(TOTAL_IMPORTED_ENERGY, summed_energy_report)
device:emit_event(capabilities.energyMeter.energy({ value = summed_energy_report, unit = "Wh" }))
end
end

local function energy_report_handler_factory(is_cumulative_report)
return function(driver, device, ib, response)
if not device:get_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED) then
if not device:get_field(IMPORT_POLL_TIMER_SETTING_ATTEMPTED) then
set_poll_report_timer_and_schedule(device, is_cumulative_report)
end
if is_cumulative_report then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ test.register_coroutine_test(
mock_device:generate_test_message("main", capabilities.energyMeter.energy({ value = 29.0, unit = "Wh" }))
)
test.wait_for_events()
local report_export_poll_timer = mock_device:get_field("__recurring_export_report_poll_timer")
local export_timer_length = mock_device:get_field("__export_report_timeout")
assert(report_export_poll_timer ~= nil, "report_export_poll_timer should exist")
assert(export_timer_length ~= nil, "export_timer_length should exist")
assert(export_timer_length == MINIMUM_ST_ENERGY_REPORT_INTERVAL, "export_timer should min_interval")
local report_import_poll_timer = mock_device:get_field("__recurring_import_report_poll_timer")
local import_timer_length = mock_device:get_field("__import_report_timeout")
assert(report_import_poll_timer ~= nil, "report_import_poll_timer should exist")
assert(import_timer_length ~= nil, "import_timer_length should exist")
assert(import_timer_length == MINIMUM_ST_ENERGY_REPORT_INTERVAL, "import_timer should min_interval")
end
)

Expand Down Expand Up @@ -506,11 +506,11 @@ test.register_coroutine_test(
mock_device:generate_test_message("main", capabilities.energyMeter.energy({ value = 29.0, unit = "Wh" }))
)
test.wait_for_events()
local report_export_poll_timer = mock_device:get_field("__recurring_export_report_poll_timer")
local export_timer_length = mock_device:get_field("__export_report_timeout")
assert(report_export_poll_timer ~= nil, "report_export_poll_timer should exist")
assert(export_timer_length ~= nil, "export_timer_length should exist")
assert(export_timer_length == 2000, "export_timer should min_interval")
local report_import_poll_timer = mock_device:get_field("__recurring_import_report_poll_timer")
local import_timer_length = mock_device:get_field("__import_report_timeout")
assert(report_import_poll_timer ~= nil, "report_import_poll_timer should exist")
assert(import_timer_length ~= nil, "import_timer_length should exist")
assert(import_timer_length == 2000, "import_timer should min_interval")
end
)

Expand Down Expand Up @@ -561,24 +561,24 @@ test.register_coroutine_test(
mock_device:generate_test_message("main", capabilities.energyMeter.energy({ value = 29.0, unit = "Wh" }))
)
test.wait_for_events()
local report_export_poll_timer = mock_device:get_field("__recurring_export_report_poll_timer")
local export_timer_length = mock_device:get_field("__export_report_timeout")
assert(report_export_poll_timer ~= nil, "report_export_poll_timer should exist")
assert(export_timer_length ~= nil, "export_timer_length should exist")
assert(export_timer_length == 2000, "export_timer should min_interval")
local report_import_poll_timer = mock_device:get_field("__recurring_import_report_poll_timer")
local import_timer_length = mock_device:get_field("__import_report_timeout")
assert(report_import_poll_timer ~= nil, "report_import_poll_timer should exist")
assert(import_timer_length ~= nil, "import_timer_length should exist")
assert(import_timer_length == 2000, "import_timer should min_interval")


test.socket.device_lifecycle:__queue_receive({ mock_device.id, "removed" })
test.wait_for_events()
report_export_poll_timer = mock_device:get_field("__recurring_export_report_poll_timer")
export_timer_length = mock_device:get_field("__export_report_timeout")
assert(report_export_poll_timer == nil, "report_export_poll_timer should exist")
assert(export_timer_length == nil, "export_timer_length should exist")
report_import_poll_timer = mock_device:get_field("__recurring_import_report_poll_timer")
import_timer_length = mock_device:get_field("__import_report_timeout")
assert(report_import_poll_timer == nil, "report_import_poll_timer should exist")
assert(import_timer_length == nil, "import_timer_length should exist")
end
)

test.register_coroutine_test(
"Generated periodic export energy device poll timer (<15 minutes) gets correctly set", function()
"Generated periodic import energy device poll timer (<15 minutes) gets correctly set", function()
test.socket["matter"]:__queue_receive(
{
mock_device_periodic.id,
Expand Down Expand Up @@ -623,18 +623,18 @@ test.register_coroutine_test(
mock_device_periodic:generate_test_message("main", capabilities.energyMeter.energy({ value = 69.0, unit = "Wh" }))
)
test.wait_for_events()
local report_export_poll_timer = mock_device_periodic:get_field("__recurring_export_report_poll_timer")
local export_timer_length = mock_device_periodic:get_field("__export_report_timeout")
assert(report_export_poll_timer ~= nil, "report_export_poll_timer should exist")
assert(export_timer_length ~= nil, "export_timer_length should exist")
assert(export_timer_length == MINIMUM_ST_ENERGY_REPORT_INTERVAL, "export_timer should min_interval")
local report_import_poll_timer = mock_device_periodic:get_field("__recurring_import_report_poll_timer")
local import_timer_length = mock_device_periodic:get_field("__import_report_timeout")
assert(report_import_poll_timer ~= nil, "report_import_poll_timer should exist")
assert(import_timer_length ~= nil, "import_timer_length should exist")
assert(import_timer_length == MINIMUM_ST_ENERGY_REPORT_INTERVAL, "import_timer should min_interval")
end,
{ test_init = test_init_periodic }
)


test.register_coroutine_test(
"Generated periodic export energy device poll timer (>15 minutes) gets correctly set", function()
"Generated periodic import energy device poll timer (>15 minutes) gets correctly set", function()
test.socket["matter"]:__queue_receive(
{
mock_device_periodic.id,
Expand Down Expand Up @@ -679,11 +679,11 @@ test.register_coroutine_test(
mock_device_periodic:generate_test_message("main", capabilities.energyMeter.energy({ value = 69.0, unit = "Wh" }))
)
test.wait_for_events()
local report_export_poll_timer = mock_device_periodic:get_field("__recurring_export_report_poll_timer")
local export_timer_length = mock_device_periodic:get_field("__export_report_timeout")
assert(report_export_poll_timer ~= nil, "report_export_poll_timer should exist")
assert(export_timer_length ~= nil, "export_timer_length should exist")
assert(export_timer_length == 2000, "export_timer should min_interval")
local report_import_poll_timer = mock_device_periodic:get_field("__recurring_import_report_poll_timer")
local import_timer_length = mock_device_periodic:get_field("__import_report_timeout")
assert(report_import_poll_timer ~= nil, "report_import_poll_timer should exist")
assert(import_timer_length ~= nil, "import_timer_length should exist")
assert(import_timer_length == 2000, "import_timer should min_interval")
end,
{ test_init = test_init_periodic }
)
Expand Down

0 comments on commit ce99c6d

Please sign in to comment.