@@ -34,8 +34,8 @@ if version.api < 12 then
34
34
end
35
35
36
36
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 "
39
39
40
40
local CUMULATIVE_REPORTS_NOT_SUPPORTED = " __cumulative_reports_not_supported"
41
41
local LAST_IMPORTED_REPORT_TIMESTAMP = " __last_imported_report_timestamp"
@@ -47,6 +47,11 @@ local TOTAL_CUMULATIVE_ENERGY_IMPORTED = "__total_cumulative_energy_imported"
47
47
local TOTAL_CUMULATIVE_ENERGY_EXPORTED = " __total_cumulative_energy_exported"
48
48
local TOTAL_ACTIVE_POWER = " __total_active_power"
49
49
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
+
50
55
local MAX_CHARGING_CURRENT_CONSTRAINT = 80000 -- In v1.3 release of stack, this check for 80 A is performed.
51
56
52
57
local EVSE_DEVICE_TYPE_ID = 0x050C
@@ -55,7 +60,6 @@ local BATTERY_STORAGE_DEVICE_TYPE_ID = 0x0018
55
60
local ELECTRICAL_SENSOR_DEVICE_TYPE_ID = 0x0510
56
61
local DEVICE_ENERGY_MANAGEMENT_DEVICE_TYPE_ID = 0x050D
57
62
58
-
59
63
local function get_endpoints_for_dt (device , device_type )
60
64
local endpoints = {}
61
65
for _ , ep in ipairs (device .endpoints ) do
@@ -100,6 +104,25 @@ local function component_to_endpoint(device, component)
100
104
end
101
105
end
102
106
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
+
103
126
local function time_zone_offset ()
104
127
return os.difftime (os.time (), os.time (os.date (" !*t" , os.time ())))
105
128
end
@@ -193,6 +216,7 @@ local BATTERY_CHARGING_STATE_MAP = {
193
216
194
217
-- Lifecycle Handlers --
195
218
local function device_init (driver , device )
219
+ check_field_name_updates (device )
196
220
device :subscribe ()
197
221
device :set_endpoint_to_component_fn (endpoint_to_component )
198
222
device :set_component_to_endpoint_fn (component_to_endpoint )
@@ -386,27 +410,22 @@ local function power_mode_handler(driver, device, ib, response)
386
410
end
387
411
388
412
local function energy_evse_supported_modes_attr_handler (driver , device , ib , response )
389
- local supportedEvseModesMap = device :get_field (SUPPORTED_EVSE_MODES_MAP ) or {}
390
413
local supportedEvseModes = {}
391
414
for _ , mode in ipairs (ib .data .elements ) do
392
415
if version .api < 11 then
393
416
clusters .EnergyEvseMode .types .ModeOptionStruct :augment_type (mode )
394
417
end
395
418
table.insert (supportedEvseModes , mode .elements .label .value )
396
419
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 })
399
421
local event = capabilities .mode .supportedModes (supportedEvseModes , { visibility = { displayed = false } })
400
422
device :emit_event_for_endpoint (ib .endpoint_id , event )
401
423
event = capabilities .mode .supportedArguments (supportedEvseModes , { visibility = { displayed = false } })
402
424
device :emit_event_for_endpoint (ib .endpoint_id , event )
403
425
end
404
426
405
427
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 {}
410
429
local currentMode = ib .data .value
411
430
for i , mode in ipairs (supportedEvseModes ) do
412
431
if i - 1 == currentMode then
@@ -417,27 +436,22 @@ local function energy_evse_mode_attr_handler(driver, device, ib, response)
417
436
end
418
437
419
438
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 {}
421
439
local supportedDeviceEnergyMgmtModes = {}
422
440
for _ , mode in ipairs (ib .data .elements ) do
423
441
if version .api < 12 then
424
442
clusters .DeviceEnergyManagementMode .types .ModeOptionStruct :augment_type (mode )
425
443
end
426
444
table.insert (supportedDeviceEnergyMgmtModes , mode .elements .label .value )
427
445
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 })
430
447
local event = capabilities .mode .supportedModes (supportedDeviceEnergyMgmtModes , { visibility = { displayed = false } })
431
448
device :emit_event_for_endpoint (ib .endpoint_id , event )
432
449
event = capabilities .mode .supportedArguments (supportedDeviceEnergyMgmtModes , { visibility = { displayed = false } })
433
450
device :emit_event_for_endpoint (ib .endpoint_id , event )
434
451
end
435
452
436
453
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 {}
441
455
local currentMode = ib .data .value
442
456
for i , mode in ipairs (supportedDeviceEnergyMgmtModes ) do
443
457
if i - 1 == currentMode then
@@ -603,8 +617,7 @@ local function handle_set_mode_command(driver, device, cmd)
603
617
local set_mode_handlers = {
604
618
[" main" ] = function ( ... )
605
619
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 {}
608
621
for i , mode in ipairs (supportedEvseModes ) do
609
622
if cmd .args .mode == mode then
610
623
device :send (clusters .EnergyEvseMode .commands .ChangeToMode (device , ep , i - 1 ))
@@ -615,8 +628,7 @@ local function handle_set_mode_command(driver, device, cmd)
615
628
end ,
616
629
[" deviceEnergyManagement" ] = function ( ... )
617
630
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 {}
620
632
for i , mode in ipairs (supportedDeviceEnergyMgmtModes ) do
621
633
if cmd .args .mode == mode then
622
634
device :send (clusters .DeviceEnergyManagementMode .commands .ChangeToMode (device , ep , i - 1 ))
0 commit comments