diff --git a/drivers/SmartThings/matter-switch/src/init.lua b/drivers/SmartThings/matter-switch/src/init.lua index 15fd652b83..e8196705fc 100644 --- a/drivers/SmartThings/matter-switch/src/init.lua +++ b/drivers/SmartThings/matter-switch/src/init.lua @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -982,7 +982,7 @@ 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 @@ -990,16 +990,16 @@ 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 diff --git a/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua b/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua index fd7eb56efc..9dfaa15348 100644 --- a/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua +++ b/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua @@ -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 ) @@ -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 ) @@ -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, @@ -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, @@ -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 } )