From 065a0921bbde0a84ac18b25db50c7ab144d585db Mon Sep 17 00:00:00 2001 From: Harrison Carter Date: Tue, 17 Dec 2024 10:03:47 -0600 Subject: [PATCH 1/3] initial commit --- .../attributes/CumulativeEnergyImported.lua | 68 +++++++++++++++++++ .../attributes/PeriodicEnergyImported.lua | 68 +++++++++++++++++++ .../SmartThings/matter-switch/src/init.lua | 30 ++++---- .../src/test/test_electrical_sensor.lua | 56 +++++++-------- 4 files changed, 179 insertions(+), 43 deletions(-) create mode 100644 drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyImported.lua create mode 100644 drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyImported.lua diff --git a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyImported.lua b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyImported.lua new file mode 100644 index 0000000000..c295bcab8b --- /dev/null +++ b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyImported.lua @@ -0,0 +1,68 @@ +local cluster_base = require "st.matter.cluster_base" +local data_types = require "st.matter.data_types" +local TLVParser = require "st.matter.TLV.TLVParser" + +local CumulativeEnergyImported = { + ID = 0x0001, + NAME = "CumulativeEnergyImported", + base_type = require "st.matter.generated.zap_clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct", +} + +function CumulativeEnergyImported:new_value(...) + local o = self.base_type(table.unpack({...})) + self:augment_type(o) + return o +end + +function CumulativeEnergyImported:read(device, endpoint_id) + return cluster_base.read( + device, + endpoint_id, + self._cluster.ID, + self.ID, + nil --event_id + ) +end + +function CumulativeEnergyImported:subscribe(device, endpoint_id) + return cluster_base.subscribe( + device, + endpoint_id, + self._cluster.ID, + self.ID, + nil --event_id + ) +end + +function CumulativeEnergyImported:set_parent_cluster(cluster) + self._cluster = cluster + return self +end + +function CumulativeEnergyImported:build_test_report_data( + device, + endpoint_id, + value, + status +) + local data = data_types.validate_or_build_type(value, self.base_type) + self:augment_type(data) + return cluster_base.build_test_report_data( + device, + endpoint_id, + self._cluster.ID, + self.ID, + data, + status + ) +end + +function CumulativeEnergyImported:deserialize(tlv_buf) + local data = TLVParser.decode_tlv(tlv_buf) + self:augment_type(data) + return data +end + +setmetatable(CumulativeEnergyImported, {__call = CumulativeEnergyImported.new_value, __index = CumulativeEnergyImported.base_type}) +return CumulativeEnergyImported + diff --git a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyImported.lua b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyImported.lua new file mode 100644 index 0000000000..7509c5a7de --- /dev/null +++ b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyImported.lua @@ -0,0 +1,68 @@ +local cluster_base = require "st.matter.cluster_base" +local data_types = require "st.matter.data_types" +local TLVParser = require "st.matter.TLV.TLVParser" + +local PeriodicEnergyImported = { + ID = 0x0003, + NAME = "PeriodicEnergyImported", + base_type = require "st.matter.generated.zap_clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct", +} + +function PeriodicEnergyImported:new_value(...) + local o = self.base_type(table.unpack({...})) + self:augment_type(o) + return o +end + +function PeriodicEnergyImported:read(device, endpoint_id) + return cluster_base.read( + device, + endpoint_id, + self._cluster.ID, + self.ID, + nil --event_id + ) +end + +function PeriodicEnergyImported:subscribe(device, endpoint_id) + return cluster_base.subscribe( + device, + endpoint_id, + self._cluster.ID, + self.ID, + nil --event_id + ) +end + +function PeriodicEnergyImported:set_parent_cluster(cluster) + self._cluster = cluster + return self +end + +function PeriodicEnergyImported:build_test_report_data( + device, + endpoint_id, + value, + status +) + local data = data_types.validate_or_build_type(value, self.base_type) + self:augment_type(data) + return cluster_base.build_test_report_data( + device, + endpoint_id, + self._cluster.ID, + self.ID, + data, + status + ) +end + +function PeriodicEnergyImported:deserialize(tlv_buf) + local data = TLVParser.decode_tlv(tlv_buf) + self:augment_type(data) + return data +end + +setmetatable(PeriodicEnergyImported, {__call = PeriodicEnergyImported.new_value, __index = PeriodicEnergyImported.base_type}) +return PeriodicEnergyImported + diff --git a/drivers/SmartThings/matter-switch/src/init.lua b/drivers/SmartThings/matter-switch/src/init.lua index 3fab77fa3a..15fd652b83 100644 --- a/drivers/SmartThings/matter-switch/src/init.lua +++ b/drivers/SmartThings/matter-switch/src/init.lua @@ -161,8 +161,8 @@ 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_exported_energy" -local LAST_EXPORTED_REPORT_TIMESTAMP = "__last_exported_report_timestamp" +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 MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60) -- 15 minutes, reported in seconds local SUBSCRIPTION_REPORT_OCCURRED = "__subscription_report_occurred" @@ -192,17 +192,17 @@ local function delete_export_poll_schedule(device) end end -local function send_export_poll_report(device, latest_total_exported_energy_wh) +local function send_export_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 }) -- Calculate the energy delta between reports local energy_delta_wh = 0.0 - local previous_exported_report = device:get_latest_state("main", capabilities.powerConsumptionReport.ID, + local previous_imported_report = device:get_latest_state("main", capabilities.powerConsumptionReport.ID, capabilities.powerConsumptionReport.powerConsumption.NAME) - if previous_exported_report and previous_exported_report.energy then - energy_delta_wh = math.max(latest_total_exported_energy_wh - previous_exported_report.energy, 0.0) + if previous_imported_report and previous_imported_report.energy then + energy_delta_wh = math.max(latest_total_imported_energy_wh - previous_imported_report.energy, 0.0) end -- Report the energy consumed during the time interval. The unit of these values should be 'Wh' @@ -210,7 +210,7 @@ local function send_export_poll_report(device, latest_total_exported_energy_wh) start = iso8061Timestamp(last_time), ["end"] = iso8061Timestamp(current_time - 1), deltaEnergy = energy_delta_wh, - energy = latest_total_exported_energy_wh + energy = latest_total_imported_energy_wh })) end @@ -979,7 +979,7 @@ local function occupancy_attr_handler(driver, device, ib, response) device:emit_event(ib.data.value == 0x01 and capabilities.motionSensor.motion.active() or capabilities.motionSensor.motion.inactive()) end -local function cumul_energy_exported_handler(driver, device, ib, response) +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) @@ -987,7 +987,7 @@ local function cumul_energy_exported_handler(driver, device, ib, response) end end -local function per_energy_exported_handler(driver, device, ib, response) +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 @@ -1003,9 +1003,9 @@ local function energy_report_handler_factory(is_cumulative_report) set_poll_report_timer_and_schedule(device, is_cumulative_report) end if is_cumulative_report then - cumul_energy_exported_handler(driver, device, ib, response) + cumul_energy_imported_handler(driver, device, ib, response) elseif device:get_field(CUMULATIVE_REPORTS_NOT_SUPPORTED) then - per_energy_exported_handler(driver, device, ib, response) + per_energy_imported_handler(driver, device, ib, response) end end end @@ -1174,8 +1174,8 @@ local matter_driver_template = { [clusters.ElectricalPowerMeasurement.attributes.ActivePower.ID] = active_power_handler, }, [clusters.ElectricalEnergyMeasurement.ID] = { - [clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported.ID] = energy_report_handler_factory(true), - [clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported.ID] = energy_report_handler_factory(false), + [clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported.ID] = energy_report_handler_factory(true), + [clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported.ID] = energy_report_handler_factory(false), }, [clusters.ValveConfigurationAndControl.ID] = { [clusters.ValveConfigurationAndControl.attributes.CurrentState.ID] = valve_state_attr_handler, @@ -1234,8 +1234,8 @@ local matter_driver_template = { clusters.PowerSource.attributes.BatPercentRemaining, }, [capabilities.energyMeter.ID] = { - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported, + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported }, [capabilities.powerMeter.ID] = { clusters.ElectricalPowerMeasurement.attributes.ActivePower 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 e702362997..fd7eb56efc 100644 --- a/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua +++ b/drivers/SmartThings/matter-switch/src/test/test_electrical_sensor.lua @@ -89,14 +89,14 @@ local mock_device_periodic = test.mock_device.build_test_matter_device({ }) local subscribed_attributes_periodic = { - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported, + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported, + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported, } local subscribed_attributes = { clusters.OnOff.attributes.OnOff, clusters.ElectricalPowerMeasurement.attributes.ActivePower, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported, + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported, + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported, } local cumulative_report_val_19 = { @@ -264,7 +264,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyExported:build_test_report_data(mock_device, 1, cumulative_report_val_19) + clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyImported:build_test_report_data(mock_device, 1, cumulative_report_val_19) } }, { @@ -277,7 +277,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyExported:build_test_report_data(mock_device, 1, cumulative_report_val_19) + clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyImported:build_test_report_data(mock_device, 1, cumulative_report_val_19) } }, { @@ -290,7 +290,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyExported:build_test_report_data(mock_device, 1, cumulative_report_val_29) + clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyImported:build_test_report_data(mock_device, 1, cumulative_report_val_29) } }, { @@ -313,7 +313,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyExported:build_test_report_data(mock_device, 1, cumulative_report_val_39) + clusters.ElectricalEnergyMeasurement.server.attributes.CumulativeEnergyImported:build_test_report_data(mock_device, 1, cumulative_report_val_39) } }, { @@ -332,7 +332,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyExported:build_test_report_data(mock_device, 1, periodic_report_val_23) + clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyImported:build_test_report_data(mock_device, 1, periodic_report_val_23) } }, { @@ -340,7 +340,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyExported:build_test_report_data(mock_device, 1, periodic_report_val_23) + clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyImported:build_test_report_data(mock_device, 1, periodic_report_val_23) } }, } @@ -354,7 +354,7 @@ test.register_message_test( direction = "receive", message = { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyExported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) + clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyImported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) } }, { @@ -367,7 +367,7 @@ test.register_message_test( direction = "receive", message = { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyExported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) + clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyImported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) } }, { @@ -380,7 +380,7 @@ test.register_message_test( direction = "receive", message = { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyExported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) + clusters.ElectricalEnergyMeasurement.server.attributes.PeriodicEnergyImported:build_test_report_data(mock_device_periodic, 1, periodic_report_val_23) } }, { @@ -410,7 +410,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -421,7 +421,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -434,7 +434,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_29 ) } @@ -465,7 +465,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -476,7 +476,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -489,7 +489,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_29 ) } @@ -520,7 +520,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -531,7 +531,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_19 ) } @@ -544,7 +544,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device.id, - clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:build_test_report_data( mock_device, 1, cumulative_report_val_29 ) } @@ -582,7 +582,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } @@ -593,7 +593,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } @@ -606,7 +606,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } @@ -638,7 +638,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } @@ -649,7 +649,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } @@ -662,7 +662,7 @@ test.register_coroutine_test( test.socket["matter"]:__queue_receive( { mock_device_periodic.id, - clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported:build_test_report_data( + clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported:build_test_report_data( mock_device_periodic, 1, periodic_report_val_23 ) } From 1100449dbb04e42e62691936cffc4646f8f99047 Mon Sep 17 00:00:00 2001 From: Harrison Carter Date: Tue, 17 Dec 2024 10:11:42 -0600 Subject: [PATCH 2/3] update: function names to reflect new attribute --- .../SmartThings/matter-switch/src/init.lua | 60 +++++++++--------- .../src/test/test_electrical_sensor.lua | 62 +++++++++---------- 2 files changed, 61 insertions(+), 61 deletions(-) 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 } ) From 438b6252f08fba1dce8f2b398bef8a95ca1f05ce Mon Sep 17 00:00:00 2001 From: Harrison Carter Date: Wed, 18 Dec 2024 11:55:56 -0600 Subject: [PATCH 3/3] remove: unused embedded attribute defs --- .../attributes/CumulativeEnergyExported.lua | 68 ------------------- .../attributes/PeriodicEnergyExported.lua | 68 ------------------- 2 files changed, 136 deletions(-) delete mode 100644 drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyExported.lua delete mode 100644 drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyExported.lua diff --git a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyExported.lua b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyExported.lua deleted file mode 100644 index 22befec642..0000000000 --- a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyExported.lua +++ /dev/null @@ -1,68 +0,0 @@ -local cluster_base = require "st.matter.cluster_base" -local data_types = require "st.matter.data_types" -local TLVParser = require "st.matter.TLV.TLVParser" - -local CumulativeEnergyExported = { - ID = 0x0002, - NAME = "CumulativeEnergyExported", - base_type = require "ElectricalEnergyMeasurement.types.EnergyMeasurementStruct", -} - -function CumulativeEnergyExported:new_value(...) - local o = self.base_type(table.unpack({...})) - self:augment_type(o) - return o -end - -function CumulativeEnergyExported:read(device, endpoint_id) - return cluster_base.read( - device, - endpoint_id, - self._cluster.ID, - self.ID, - nil - ) -end - -function CumulativeEnergyExported:subscribe(device, endpoint_id) - return cluster_base.subscribe( - device, - endpoint_id, - self._cluster.ID, - self.ID, - nil - ) -end - -function CumulativeEnergyExported:set_parent_cluster(cluster) - self._cluster = cluster - return self -end - -function CumulativeEnergyExported:build_test_report_data( - device, - endpoint_id, - value, - status -) - local data = data_types.validate_or_build_type(value, self.base_type) - self:augment_type(data) - return cluster_base.build_test_report_data( - device, - endpoint_id, - self._cluster.ID, - self.ID, - data, - status - ) -end - -function CumulativeEnergyExported:deserialize(tlv_buf) - local data = TLVParser.decode_tlv(tlv_buf) - self:augment_type(data) - return data -end - -setmetatable(CumulativeEnergyExported, {__call = CumulativeEnergyExported.new_value, __index = CumulativeEnergyExported.base_type}) -return CumulativeEnergyExported - diff --git a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyExported.lua b/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyExported.lua deleted file mode 100644 index 4c1ee29274..0000000000 --- a/drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyExported.lua +++ /dev/null @@ -1,68 +0,0 @@ -local cluster_base = require "st.matter.cluster_base" -local data_types = require "st.matter.data_types" -local TLVParser = require "st.matter.TLV.TLVParser" - -local PeriodicEnergyExported = { - ID = 0x0004, - NAME = "PeriodicEnergyExported", - base_type = require "ElectricalEnergyMeasurement.types.EnergyMeasurementStruct", -} - -function PeriodicEnergyExported:new_value(...) - local o = self.base_type(table.unpack({...})) - self:augment_type(o) - return o -end - -function PeriodicEnergyExported:read(device, endpoint_id) - return cluster_base.read( - device, - endpoint_id, - self._cluster.ID, - self.ID, - nil - ) -end - -function PeriodicEnergyExported:subscribe(device, endpoint_id) - return cluster_base.subscribe( - device, - endpoint_id, - self._cluster.ID, - self.ID, - nil - ) -end - -function PeriodicEnergyExported:set_parent_cluster(cluster) - self._cluster = cluster - return self -end - -function PeriodicEnergyExported:build_test_report_data( - device, - endpoint_id, - value, - status -) - local data = data_types.validate_or_build_type(value, self.base_type) - self:augment_type(data) - return cluster_base.build_test_report_data( - device, - endpoint_id, - self._cluster.ID, - self.ID, - data, - status - ) -end - -function PeriodicEnergyExported:deserialize(tlv_buf) - local data = TLVParser.decode_tlv(tlv_buf) - self:augment_type(data) - return data -end - -setmetatable(PeriodicEnergyExported, {__call = PeriodicEnergyExported.new_value, __index = PeriodicEnergyExported.base_type}) -return PeriodicEnergyExported -