Skip to content

Commit f4cefe9

Browse files
authored
Change use of the EnergyExported attribute to EnergyImported in matter switch (#1830)
1 parent aebfa31 commit f4cefe9

File tree

4 files changed

+123
-123
lines changed

4 files changed

+123
-123
lines changed

drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyExported.lua renamed to drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/CumulativeEnergyImported.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ local cluster_base = require "st.matter.cluster_base"
22
local data_types = require "st.matter.data_types"
33
local TLVParser = require "st.matter.TLV.TLVParser"
44

5-
local CumulativeEnergyExported = {
6-
ID = 0x0002,
7-
NAME = "CumulativeEnergyExported",
5+
local CumulativeEnergyImported = {
6+
ID = 0x0001,
7+
NAME = "CumulativeEnergyImported",
88
base_type = require "ElectricalEnergyMeasurement.types.EnergyMeasurementStruct",
99
}
1010

11-
function CumulativeEnergyExported:new_value(...)
11+
function CumulativeEnergyImported:new_value(...)
1212
local o = self.base_type(table.unpack({...}))
1313
self:augment_type(o)
1414
return o
1515
end
1616

17-
function CumulativeEnergyExported:read(device, endpoint_id)
17+
function CumulativeEnergyImported:read(device, endpoint_id)
1818
return cluster_base.read(
1919
device,
2020
endpoint_id,
@@ -24,7 +24,7 @@ function CumulativeEnergyExported:read(device, endpoint_id)
2424
)
2525
end
2626

27-
function CumulativeEnergyExported:subscribe(device, endpoint_id)
27+
function CumulativeEnergyImported:subscribe(device, endpoint_id)
2828
return cluster_base.subscribe(
2929
device,
3030
endpoint_id,
@@ -34,12 +34,12 @@ function CumulativeEnergyExported:subscribe(device, endpoint_id)
3434
)
3535
end
3636

37-
function CumulativeEnergyExported:set_parent_cluster(cluster)
37+
function CumulativeEnergyImported:set_parent_cluster(cluster)
3838
self._cluster = cluster
3939
return self
4040
end
4141

42-
function CumulativeEnergyExported:build_test_report_data(
42+
function CumulativeEnergyImported:build_test_report_data(
4343
device,
4444
endpoint_id,
4545
value,
@@ -57,12 +57,12 @@ function CumulativeEnergyExported:build_test_report_data(
5757
)
5858
end
5959

60-
function CumulativeEnergyExported:deserialize(tlv_buf)
60+
function CumulativeEnergyImported:deserialize(tlv_buf)
6161
local data = TLVParser.decode_tlv(tlv_buf)
6262
self:augment_type(data)
6363
return data
6464
end
6565

66-
setmetatable(CumulativeEnergyExported, {__call = CumulativeEnergyExported.new_value, __index = CumulativeEnergyExported.base_type})
67-
return CumulativeEnergyExported
66+
setmetatable(CumulativeEnergyImported, {__call = CumulativeEnergyImported.new_value, __index = CumulativeEnergyImported.base_type})
67+
return CumulativeEnergyImported
6868

drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyExported.lua renamed to drivers/SmartThings/matter-switch/src/ElectricalEnergyMeasurement/server/attributes/PeriodicEnergyImported.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ local cluster_base = require "st.matter.cluster_base"
22
local data_types = require "st.matter.data_types"
33
local TLVParser = require "st.matter.TLV.TLVParser"
44

5-
local PeriodicEnergyExported = {
6-
ID = 0x0004,
7-
NAME = "PeriodicEnergyExported",
5+
local PeriodicEnergyImported = {
6+
ID = 0x0003,
7+
NAME = "PeriodicEnergyImported",
88
base_type = require "ElectricalEnergyMeasurement.types.EnergyMeasurementStruct",
99
}
1010

11-
function PeriodicEnergyExported:new_value(...)
11+
function PeriodicEnergyImported:new_value(...)
1212
local o = self.base_type(table.unpack({...}))
1313
self:augment_type(o)
1414
return o
1515
end
1616

17-
function PeriodicEnergyExported:read(device, endpoint_id)
17+
function PeriodicEnergyImported:read(device, endpoint_id)
1818
return cluster_base.read(
1919
device,
2020
endpoint_id,
@@ -24,7 +24,7 @@ function PeriodicEnergyExported:read(device, endpoint_id)
2424
)
2525
end
2626

27-
function PeriodicEnergyExported:subscribe(device, endpoint_id)
27+
function PeriodicEnergyImported:subscribe(device, endpoint_id)
2828
return cluster_base.subscribe(
2929
device,
3030
endpoint_id,
@@ -34,12 +34,12 @@ function PeriodicEnergyExported:subscribe(device, endpoint_id)
3434
)
3535
end
3636

37-
function PeriodicEnergyExported:set_parent_cluster(cluster)
37+
function PeriodicEnergyImported:set_parent_cluster(cluster)
3838
self._cluster = cluster
3939
return self
4040
end
4141

42-
function PeriodicEnergyExported:build_test_report_data(
42+
function PeriodicEnergyImported:build_test_report_data(
4343
device,
4444
endpoint_id,
4545
value,
@@ -57,12 +57,12 @@ function PeriodicEnergyExported:build_test_report_data(
5757
)
5858
end
5959

60-
function PeriodicEnergyExported:deserialize(tlv_buf)
60+
function PeriodicEnergyImported:deserialize(tlv_buf)
6161
local data = TLVParser.decode_tlv(tlv_buf)
6262
self:augment_type(data)
6363
return data
6464
end
6565

66-
setmetatable(PeriodicEnergyExported, {__call = PeriodicEnergyExported.new_value, __index = PeriodicEnergyExported.base_type})
67-
return PeriodicEnergyExported
66+
setmetatable(PeriodicEnergyImported, {__call = PeriodicEnergyImported.new_value, __index = PeriodicEnergyImported.base_type})
67+
return PeriodicEnergyImported
6868

drivers/SmartThings/matter-switch/src/init.lua

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ local child_device_profile_overrides = {
158158
local detect_matter_thing
159159

160160
local 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"
167167
local MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60) -- 15 minutes, reported in seconds
168168
local SUBSCRIPTION_REPORT_OCCURRED = "__subscription_report_occurred"
169169
local 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)
184184
end
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
193193
end
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
}))
215215
end
216216

217217
local 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)
224224
end
225225

226226
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)
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
250250
end
251251

@@ -700,7 +700,7 @@ end
700700

701701
local function device_removed(driver, device)
702702
log.info("device removed")
703-
delete_export_poll_schedule(device)
703+
delete_import_poll_schedule(device)
704704
end
705705

706706
local 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())
980980
end
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
988988
end
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
998998
end
999999

10001000
local 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
10111011
end
@@ -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

Comments
 (0)