Skip to content

Commit 2450afd

Browse files
Matter Energy: Fix non- JSON serializable fields
Keys in datastore tables need to be strings in order to be JSON serializable. SUPPORTED_EVSE_MODES_MAP and SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP need to be updated.
1 parent 4d3ae9a commit 2450afd

File tree

1 file changed

+33
-21
lines changed
  • drivers/SmartThings/matter-energy/src

1 file changed

+33
-21
lines changed

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ if version.api < 12 then
3434
end
3535

3636
local COMPONENT_TO_ENDPOINT_MAP = "__component_to_endpoint_map"
37-
local SUPPORTED_EVSE_MODES_MAP = "__supported_evse_modes_map"
38-
local SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP = "__supported_device_energy_management_modes_map"
37+
local SUPPORTED_EVSE_MODES = "__supported_evse_modes"
38+
local SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES = "__supported_device_energy_management_modes"
3939

4040
local CUMULATIVE_REPORTS_NOT_SUPPORTED = "__cumulative_reports_not_supported"
4141
local LAST_IMPORTED_REPORT_TIMESTAMP = "__last_imported_report_timestamp"
@@ -47,6 +47,11 @@ local TOTAL_CUMULATIVE_ENERGY_IMPORTED = "__total_cumulative_energy_imported"
4747
local TOTAL_CUMULATIVE_ENERGY_EXPORTED = "__total_cumulative_energy_exported"
4848
local TOTAL_ACTIVE_POWER = "__total_active_power"
4949

50+
local updated_fields = {
51+
{ current_field_name = "__supported_evse_modes_map", updated_field_name = nil },
52+
{ current_field_name = "__supported_device_energy_management_modes_map", updated_field_name = nil }
53+
}
54+
5055
local MAX_CHARGING_CURRENT_CONSTRAINT = 80000 -- In v1.3 release of stack, this check for 80 A is performed.
5156

5257
local EVSE_DEVICE_TYPE_ID = 0x050C
@@ -55,7 +60,6 @@ local BATTERY_STORAGE_DEVICE_TYPE_ID = 0x0018
5560
local ELECTRICAL_SENSOR_DEVICE_TYPE_ID = 0x0510
5661
local DEVICE_ENERGY_MANAGEMENT_DEVICE_TYPE_ID = 0x050D
5762

58-
5963
local function get_endpoints_for_dt(device, device_type)
6064
local endpoints = {}
6165
for _, ep in ipairs(device.endpoints) do
@@ -100,6 +104,25 @@ local function component_to_endpoint(device, component)
100104
end
101105
end
102106

107+
local function get_field_for_endpoint(device, field, endpoint)
108+
return device:get_field(string.format("%s_%d", field, endpoint))
109+
end
110+
111+
local function set_field_for_endpoint(device, field, endpoint, value, additional_params)
112+
device:set_field(string.format("%s_%d", field, endpoint), value, additional_params)
113+
end
114+
115+
local function check_field_name_updates(device)
116+
for _, field in ipairs(updated_fields) do
117+
if device:get_field(field.current_field_name) then
118+
if field.updated_field_name ~= nil then
119+
device:set_field(field.updated_field_name, device:get_field(field.current_field_name), {persist = true})
120+
end
121+
device:set_field(field.current_field_name, nil)
122+
end
123+
end
124+
end
125+
103126
local function time_zone_offset()
104127
return os.difftime(os.time(), os.time(os.date("!*t", os.time())))
105128
end
@@ -193,6 +216,7 @@ local BATTERY_CHARGING_STATE_MAP = {
193216

194217
-- Lifecycle Handlers --
195218
local function device_init(driver, device)
219+
check_field_name_updates(device)
196220
device:subscribe()
197221
device:set_endpoint_to_component_fn(endpoint_to_component)
198222
device:set_component_to_endpoint_fn(component_to_endpoint)
@@ -386,27 +410,22 @@ local function power_mode_handler(driver, device, ib, response)
386410
end
387411

388412
local function energy_evse_supported_modes_attr_handler(driver, device, ib, response)
389-
local supportedEvseModesMap = device:get_field(SUPPORTED_EVSE_MODES_MAP) or {}
390413
local supportedEvseModes = {}
391414
for _, mode in ipairs(ib.data.elements) do
392415
if version.api < 11 then
393416
clusters.EnergyEvseMode.types.ModeOptionStruct:augment_type(mode)
394417
end
395418
table.insert(supportedEvseModes, mode.elements.label.value)
396419
end
397-
supportedEvseModesMap[ib.endpoint_id] = supportedEvseModes
398-
device:set_field(SUPPORTED_EVSE_MODES_MAP, supportedEvseModesMap, { persist = true })
420+
set_field_for_endpoint(device, SUPPORTED_EVSE_MODES, ib.endpoint_id, supportedEvseModes, { persist = true })
399421
local event = capabilities.mode.supportedModes(supportedEvseModes, { visibility = { displayed = false } })
400422
device:emit_event_for_endpoint(ib.endpoint_id, event)
401423
event = capabilities.mode.supportedArguments(supportedEvseModes, { visibility = { displayed = false } })
402424
device:emit_event_for_endpoint(ib.endpoint_id, event)
403425
end
404426

405427
local function energy_evse_mode_attr_handler(driver, device, ib, response)
406-
device.log.info(string.format("energy_evse_modes_attr_handler currentMode: %s", ib.data.value))
407-
408-
local supportedEvseModesMap = device:get_field(SUPPORTED_EVSE_MODES_MAP) or {}
409-
local supportedEvseModes = supportedEvseModesMap[ib.endpoint_id] or {}
428+
local supportedEvseModes = get_field_for_endpoint(device, SUPPORTED_EVSE_MODES, ib.endpoint_id) or {}
410429
local currentMode = ib.data.value
411430
for i, mode in ipairs(supportedEvseModes) do
412431
if i - 1 == currentMode then
@@ -417,27 +436,22 @@ local function energy_evse_mode_attr_handler(driver, device, ib, response)
417436
end
418437

419438
local function device_energy_mgmt_supported_modes_attr_handler(driver, device, ib, response)
420-
local supportedDeviceEnergyMgmtModesMap = device:get_field(SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP) or {}
421439
local supportedDeviceEnergyMgmtModes = {}
422440
for _, mode in ipairs(ib.data.elements) do
423441
if version.api < 12 then
424442
clusters.DeviceEnergyManagementMode.types.ModeOptionStruct:augment_type(mode)
425443
end
426444
table.insert(supportedDeviceEnergyMgmtModes, mode.elements.label.value)
427445
end
428-
supportedDeviceEnergyMgmtModesMap[ib.endpoint_id] = supportedDeviceEnergyMgmtModes
429-
device:set_field(SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP, supportedDeviceEnergyMgmtModesMap, { persist = true })
446+
set_field_for_endpoint(device, SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES, ib.endpoint_id, supportedDeviceEnergyMgmtModes, { persist = true })
430447
local event = capabilities.mode.supportedModes(supportedDeviceEnergyMgmtModes, { visibility = { displayed = false } })
431448
device:emit_event_for_endpoint(ib.endpoint_id, event)
432449
event = capabilities.mode.supportedArguments(supportedDeviceEnergyMgmtModes, { visibility = { displayed = false } })
433450
device:emit_event_for_endpoint(ib.endpoint_id, event)
434451
end
435452

436453
local function device_energy_mgmt_mode_attr_handler(driver, device, ib, response)
437-
device.log.info(string.format("device_energy_mgmt_mode_attr_handler currentMode: %s", ib.data.value))
438-
439-
local supportedDeviceEnergyMgmtModesMap = device:get_field(SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP) or {}
440-
local supportedDeviceEnergyMgmtModes = supportedDeviceEnergyMgmtModesMap[ib.endpoint_id] or {}
454+
local supportedDeviceEnergyMgmtModes = get_field_for_endpoint(device, SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES, ib.endpoint_id) or {}
441455
local currentMode = ib.data.value
442456
for i, mode in ipairs(supportedDeviceEnergyMgmtModes) do
443457
if i - 1 == currentMode then
@@ -603,8 +617,7 @@ local function handle_set_mode_command(driver, device, cmd)
603617
local set_mode_handlers = {
604618
["main"] = function( ... )
605619
local ep = component_to_endpoint(device, cmd.component)
606-
local supportedEvseModesMap = device:get_field(SUPPORTED_EVSE_MODES_MAP)
607-
local supportedEvseModes = supportedEvseModesMap[ep] or {}
620+
local supportedEvseModes = get_field_for_endpoint(device, SUPPORTED_EVSE_MODES, ep) or {}
608621
for i, mode in ipairs(supportedEvseModes) do
609622
if cmd.args.mode == mode then
610623
device:send(clusters.EnergyEvseMode.commands.ChangeToMode(device, ep, i - 1))
@@ -615,8 +628,7 @@ local function handle_set_mode_command(driver, device, cmd)
615628
end,
616629
["deviceEnergyManagement"] = function( ... )
617630
local ep = component_to_endpoint(device, cmd.component)
618-
local supportedDeviceEnergyMgmtModesMap = device:get_field(SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP)
619-
local supportedDeviceEnergyMgmtModes = supportedDeviceEnergyMgmtModesMap[ep] or {}
631+
local supportedDeviceEnergyMgmtModes = get_field_for_endpoint(device, SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES, ep) or {}
620632
for i, mode in ipairs(supportedDeviceEnergyMgmtModes) do
621633
if cmd.args.mode == mode then
622634
device:send(clusters.DeviceEnergyManagementMode.commands.ChangeToMode(device, ep, i - 1))

0 commit comments

Comments
 (0)