@@ -158,12 +158,12 @@ local child_device_profile_overrides = {
158158local detect_matter_thing
159159
160160local CUMULATIVE_REPORTS_NOT_SUPPORTED = " __cumulative_reports_not_supported"
161- local FIRST_EXPORT_REPORT_TIMESTAMP = " __first_export_report_timestamp "
162- local EXPORT_POLL_TIMER_SETTING_ATTEMPTED = " __export_poll_timer_setting_attempted "
163- local EXPORT_REPORT_TIMEOUT = " __export_report_timeout "
164- local TOTAL_EXPORTED_ENERGY = " __total_exported_energy "
165- local LAST_EXPORTED_REPORT_TIMESTAMP = " __last_exported_report_timestamp "
166- local RECURRING_EXPORT_REPORT_POLL_TIMER = " __recurring_export_report_poll_timer "
161+ local FIRST_IMPORT_REPORT_TIMESTAMP = " __first_import_report_timestamp "
162+ local IMPORT_POLL_TIMER_SETTING_ATTEMPTED = " __import_poll_timer_setting_attempted "
163+ local IMPORT_REPORT_TIMEOUT = " __import_report_timeout "
164+ local TOTAL_IMPORTED_ENERGY = " __total_imported_energy "
165+ local LAST_IMPORTED_REPORT_TIMESTAMP = " __last_imported_report_timestamp "
166+ local RECURRING_IMPORT_REPORT_POLL_TIMER = " __recurring_import_report_poll_timer "
167167local MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60 ) -- 15 minutes, reported in seconds
168168local SUBSCRIPTION_REPORT_OCCURRED = " __subscription_report_occurred"
169169local CONVERSION_CONST_MILLIWATT_TO_WATT = 1000 -- A milliwatt is 1/1000th of a watt
@@ -183,44 +183,44 @@ local function iso8061Timestamp(time)
183183 return os.date (" !%Y-%m-%dT%H:%M:%SZ" , time )
184184end
185185
186- local function delete_export_poll_schedule (device )
187- local export_poll_timer = device :get_field (RECURRING_EXPORT_REPORT_POLL_TIMER )
188- if export_poll_timer then
189- device .thread :cancel_timer (export_poll_timer )
190- device :set_field (RECURRING_EXPORT_REPORT_POLL_TIMER , nil )
191- device :set_field (EXPORT_POLL_TIMER_SETTING_ATTEMPTED , nil )
186+ local function delete_import_poll_schedule (device )
187+ local import_poll_timer = device :get_field (RECURRING_IMPORT_REPORT_POLL_TIMER )
188+ if import_poll_timer then
189+ device .thread :cancel_timer (import_poll_timer )
190+ device :set_field (RECURRING_IMPORT_REPORT_POLL_TIMER , nil )
191+ device :set_field (IMPORT_POLL_TIMER_SETTING_ATTEMPTED , nil )
192192 end
193193end
194194
195- local function send_export_poll_report (device , latest_total_exported_energy_wh )
195+ local function send_import_poll_report (device , latest_total_imported_energy_wh )
196196 local current_time = os.time ()
197- local last_time = device :get_field (LAST_EXPORTED_REPORT_TIMESTAMP ) or 0
198- device :set_field (LAST_EXPORTED_REPORT_TIMESTAMP , current_time , { persist = true })
197+ local last_time = device :get_field (LAST_IMPORTED_REPORT_TIMESTAMP ) or 0
198+ device :set_field (LAST_IMPORTED_REPORT_TIMESTAMP , current_time , { persist = true })
199199
200200 -- Calculate the energy delta between reports
201201 local energy_delta_wh = 0.0
202- local previous_exported_report = device :get_latest_state (" main" , capabilities .powerConsumptionReport .ID ,
202+ local previous_imported_report = device :get_latest_state (" main" , capabilities .powerConsumptionReport .ID ,
203203 capabilities .powerConsumptionReport .powerConsumption .NAME )
204- if previous_exported_report and previous_exported_report .energy then
205- energy_delta_wh = math.max (latest_total_exported_energy_wh - previous_exported_report .energy , 0.0 )
204+ if previous_imported_report and previous_imported_report .energy then
205+ energy_delta_wh = math.max (latest_total_imported_energy_wh - previous_imported_report .energy , 0.0 )
206206 end
207207
208208 -- Report the energy consumed during the time interval. The unit of these values should be 'Wh'
209209 device :emit_event (capabilities .powerConsumptionReport .powerConsumption ({
210210 start = iso8061Timestamp (last_time ),
211211 [" end" ] = iso8061Timestamp (current_time - 1 ),
212212 deltaEnergy = energy_delta_wh ,
213- energy = latest_total_exported_energy_wh
213+ energy = latest_total_imported_energy_wh
214214 }))
215215end
216216
217217local function create_poll_report_schedule (device )
218- local export_timer = device .thread :call_on_schedule (
219- device :get_field (EXPORT_REPORT_TIMEOUT ),
220- send_export_poll_report (device , device :get_field (TOTAL_EXPORTED_ENERGY )),
221- " polling_export_report_schedule_timer "
218+ local import_timer = device .thread :call_on_schedule (
219+ device :get_field (IMPORT_REPORT_TIMEOUT ),
220+ send_import_poll_report (device , device :get_field (TOTAL_IMPORTED_ENERGY )),
221+ " polling_import_report_schedule_timer "
222222 )
223- device :set_field (RECURRING_EXPORT_REPORT_POLL_TIMER , export_timer )
223+ device :set_field (RECURRING_IMPORT_REPORT_POLL_TIMER , import_timer )
224224end
225225
226226local 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)
234234 return
235235 elseif not device :get_field (SUBSCRIPTION_REPORT_OCCURRED ) then
236236 device :set_field (SUBSCRIPTION_REPORT_OCCURRED , true )
237- elseif not device :get_field (FIRST_EXPORT_REPORT_TIMESTAMP ) then
238- device :set_field (FIRST_EXPORT_REPORT_TIMESTAMP , os.time ())
237+ elseif not device :get_field (FIRST_IMPORT_REPORT_TIMESTAMP ) then
238+ device :set_field (FIRST_IMPORT_REPORT_TIMESTAMP , os.time ())
239239 else
240- local first_timestamp = device :get_field (FIRST_EXPORT_REPORT_TIMESTAMP )
240+ local first_timestamp = device :get_field (FIRST_IMPORT_REPORT_TIMESTAMP )
241241 local second_timestamp = os.time ()
242242 local report_interval_secs = second_timestamp - first_timestamp
243- device :set_field (EXPORT_REPORT_TIMEOUT , math.max (report_interval_secs , MINIMUM_ST_ENERGY_REPORT_INTERVAL ))
243+ device :set_field (IMPORT_REPORT_TIMEOUT , math.max (report_interval_secs , MINIMUM_ST_ENERGY_REPORT_INTERVAL ))
244244 -- the poll schedule is only needed for devices that support powerConsumption
245245 if device :supports_capability (capabilities .powerConsumptionReport ) then
246246 create_poll_report_schedule (device )
247247 end
248- device :set_field (EXPORT_POLL_TIMER_SETTING_ATTEMPTED , true )
248+ device :set_field (IMPORT_POLL_TIMER_SETTING_ATTEMPTED , true )
249249 end
250250end
251251
700700
701701local function device_removed (driver , device )
702702 log .info (" device removed" )
703- delete_export_poll_schedule (device )
703+ delete_import_poll_schedule (device )
704704end
705705
706706local function handle_switch_on (driver , device , cmd )
@@ -979,33 +979,33 @@ local function occupancy_attr_handler(driver, device, ib, response)
979979 device :emit_event (ib .data .value == 0x01 and capabilities .motionSensor .motion .active () or capabilities .motionSensor .motion .inactive ())
980980end
981981
982- local function cumul_energy_exported_handler (driver , device , ib , response )
982+ local function cumul_energy_imported_handler (driver , device , ib , response )
983983 if ib .data .elements .energy then
984984 local watt_hour_value = ib .data .elements .energy .value / CONVERSION_CONST_MILLIWATT_TO_WATT
985- device :set_field (TOTAL_EXPORTED_ENERGY , watt_hour_value )
985+ device :set_field (TOTAL_IMPORTED_ENERGY , watt_hour_value )
986986 device :emit_event (capabilities .energyMeter .energy ({ value = watt_hour_value , unit = " Wh" }))
987987 end
988988end
989989
990- local function per_energy_exported_handler (driver , device , ib , response )
990+ local function per_energy_imported_handler (driver , device , ib , response )
991991 if ib .data .elements .energy then
992992 local watt_hour_value = ib .data .elements .energy .value / CONVERSION_CONST_MILLIWATT_TO_WATT
993- local latest_energy_report = device :get_field (TOTAL_EXPORTED_ENERGY ) or 0
993+ local latest_energy_report = device :get_field (TOTAL_IMPORTED_ENERGY ) or 0
994994 local summed_energy_report = latest_energy_report + watt_hour_value
995- device :set_field (TOTAL_EXPORTED_ENERGY , summed_energy_report )
995+ device :set_field (TOTAL_IMPORTED_ENERGY , summed_energy_report )
996996 device :emit_event (capabilities .energyMeter .energy ({ value = summed_energy_report , unit = " Wh" }))
997997 end
998998end
999999
10001000local function energy_report_handler_factory (is_cumulative_report )
10011001 return function (driver , device , ib , response )
1002- if not device :get_field (EXPORT_POLL_TIMER_SETTING_ATTEMPTED ) then
1002+ if not device :get_field (IMPORT_POLL_TIMER_SETTING_ATTEMPTED ) then
10031003 set_poll_report_timer_and_schedule (device , is_cumulative_report )
10041004 end
10051005 if is_cumulative_report then
1006- cumul_energy_exported_handler (driver , device , ib , response )
1006+ cumul_energy_imported_handler (driver , device , ib , response )
10071007 elseif device :get_field (CUMULATIVE_REPORTS_NOT_SUPPORTED ) then
1008- per_energy_exported_handler (driver , device , ib , response )
1008+ per_energy_imported_handler (driver , device , ib , response )
10091009 end
10101010 end
10111011end
@@ -1174,8 +1174,8 @@ local matter_driver_template = {
11741174 [clusters .ElectricalPowerMeasurement .attributes .ActivePower .ID ] = active_power_handler ,
11751175 },
11761176 [clusters .ElectricalEnergyMeasurement .ID ] = {
1177- [clusters .ElectricalEnergyMeasurement .attributes .CumulativeEnergyExported .ID ] = energy_report_handler_factory (true ),
1178- [clusters .ElectricalEnergyMeasurement .attributes .PeriodicEnergyExported .ID ] = energy_report_handler_factory (false ),
1177+ [clusters .ElectricalEnergyMeasurement .attributes .CumulativeEnergyImported .ID ] = energy_report_handler_factory (true ),
1178+ [clusters .ElectricalEnergyMeasurement .attributes .PeriodicEnergyImported .ID ] = energy_report_handler_factory (false ),
11791179 },
11801180 [clusters .ValveConfigurationAndControl .ID ] = {
11811181 [clusters .ValveConfigurationAndControl .attributes .CurrentState .ID ] = valve_state_attr_handler ,
@@ -1234,8 +1234,8 @@ local matter_driver_template = {
12341234 clusters .PowerSource .attributes .BatPercentRemaining ,
12351235 },
12361236 [capabilities .energyMeter .ID ] = {
1237- clusters .ElectricalEnergyMeasurement .attributes .CumulativeEnergyExported ,
1238- clusters .ElectricalEnergyMeasurement .attributes .PeriodicEnergyExported
1237+ clusters .ElectricalEnergyMeasurement .attributes .CumulativeEnergyImported ,
1238+ clusters .ElectricalEnergyMeasurement .attributes .PeriodicEnergyImported
12391239 },
12401240 [capabilities .powerMeter .ID ] = {
12411241 clusters .ElectricalPowerMeasurement .attributes .ActivePower
0 commit comments