@@ -21,8 +21,9 @@ local lua_socket = require "socket"
21
21
local utils = require " st.utils"
22
22
local device_lib = require " st.device"
23
23
local embedded_cluster_utils = require " embedded-cluster-utils"
24
- -- Include driver-side definitions when lua libs api version is < 11
25
24
local version = require " version"
25
+
26
+ -- Include driver-side definitions when lua libs api version is < 11
26
27
if version .api < 11 then
27
28
clusters .ElectricalEnergyMeasurement = require " ElectricalEnergyMeasurement"
28
29
clusters .ElectricalPowerMeasurement = require " ElectricalPowerMeasurement"
@@ -43,19 +44,7 @@ local SWITCH_LEVEL_LIGHTING_MIN = 1
43
44
local CURRENT_HUESAT_ATTR_MIN = 0
44
45
local CURRENT_HUESAT_ATTR_MAX = 254
45
46
46
- local SWITCH_INITIALIZED = " __switch_intialized"
47
- -- COMPONENT_TO_ENDPOINT_MAP is here only to preserve the endpoint mapping for
48
- -- devices that were joined to this driver as MCD devices before the transition
49
- -- to join all matter-switch devices as parent-child. This value will only exist
50
- -- in the device table for devices that joined prior to this transition, and it
51
- -- will not be set for new devices.
52
47
local COMPONENT_TO_ENDPOINT_MAP = " __component_to_endpoint_map"
53
- -- COMPONENT_TO_ENDPOINT_MAP_BUTTON is for devices with button endpoints, to
54
- -- preserve the MCD functionality for button devices from the matter-button
55
- -- driver after it was merged into the matter-switch driver. Note that devices
56
- -- containing both button endpoints and switch endpoints will use this field
57
- -- rather than COMPONENT_TO_ENDPOINT_MAP.
58
- local COMPONENT_TO_ENDPOINT_MAP_BUTTON = " __component_to_endpoint_map_button"
59
48
local ENERGY_MANAGEMENT_ENDPOINT = " __energy_management_endpoint"
60
49
local IS_PARENT_CHILD_DEVICE = " __is_parent_child_device"
61
50
local COLOR_TEMP_BOUND_RECEIVED_KELVIN = " __colorTemp_bound_received_kelvin"
@@ -66,6 +55,12 @@ local LEVEL_BOUND_RECEIVED = "__level_bound_received"
66
55
local LEVEL_MIN = " __level_min"
67
56
local LEVEL_MAX = " __level_max"
68
57
local COLOR_MODE = " __color_mode"
58
+
59
+ local updated_fields = {
60
+ { field_name = " __component_to_endpoint_map_button" , updated_field_name = COMPONENT_TO_ENDPOINT_MAP },
61
+ { field_name = " __switch_intialized" , updated_field_name = nil }
62
+ }
63
+
69
64
local HUE_SAT_COLOR_MODE = clusters .ColorControl .types .ColorMode .CURRENT_HUE_AND_CURRENT_SATURATION
70
65
local X_Y_COLOR_MODE = clusters .ColorControl .types .ColorMode .CURRENTX_AND_CURRENTY
71
66
@@ -455,15 +450,15 @@ local function find_default_endpoint(device)
455
450
end
456
451
457
452
local function component_to_endpoint (device , component )
458
- local map = device :get_field (COMPONENT_TO_ENDPOINT_MAP_BUTTON ) or device : get_field ( COMPONENT_TO_ENDPOINT_MAP ) or {}
453
+ local map = device :get_field (COMPONENT_TO_ENDPOINT_MAP ) or {}
459
454
if map [component ] then
460
455
return map [component ]
461
456
end
462
457
return find_default_endpoint (device )
463
458
end
464
459
465
460
local function endpoint_to_component (device , ep )
466
- local map = device :get_field (COMPONENT_TO_ENDPOINT_MAP_BUTTON ) or device : get_field ( COMPONENT_TO_ENDPOINT_MAP ) or {}
461
+ local map = device :get_field (COMPONENT_TO_ENDPOINT_MAP ) or {}
467
462
for component , endpoint in pairs (map ) do
468
463
if endpoint == ep then
469
464
return component
@@ -472,6 +467,17 @@ local function endpoint_to_component(device, ep)
472
467
return " main"
473
468
end
474
469
470
+ local function check_field_name_updates (device )
471
+ for _ , field in ipairs (updated_fields ) do
472
+ if device :get_field (field .field_name ) then
473
+ if field .updated_field_name ~= nil then
474
+ device :set_field (field .updated_field_name , device :get_field (field .field_name ), {persist = true })
475
+ end
476
+ device :set_field (field .field_name , nil )
477
+ end
478
+ end
479
+ end
480
+
475
481
local function assign_child_profile (device , child_ep )
476
482
local profile
477
483
@@ -511,38 +517,6 @@ local function assign_child_profile(device, child_ep)
511
517
return profile or " switch-binary"
512
518
end
513
519
514
- local function do_configure (driver , device )
515
- if device :get_field (BUTTON_DEVICE_PROFILED ) then
516
- return
517
- end
518
- local level_eps = embedded_cluster_utils .get_endpoints (device , clusters .LevelControl .ID )
519
- local energy_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalEnergyMeasurement .ID )
520
- local power_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalPowerMeasurement .ID )
521
- local valve_eps = embedded_cluster_utils .get_endpoints (device , clusters .ValveConfigurationAndControl .ID )
522
- local profile_name = nil
523
- local level_support = " "
524
- if # level_eps > 0 then
525
- level_support = " -level"
526
- end
527
- if # energy_eps > 0 and # power_eps > 0 then
528
- profile_name = " plug" .. level_support .. " -power-energy-powerConsumption"
529
- elseif # energy_eps > 0 then
530
- profile_name = " plug" .. level_support .. " -energy-powerConsumption"
531
- elseif # power_eps > 0 then
532
- profile_name = " plug" .. level_support .. " -power"
533
- elseif # valve_eps > 0 then
534
- profile_name = " water-valve"
535
- if # embedded_cluster_utils .get_endpoints (device , clusters .ValveConfigurationAndControl .ID ,
536
- {feature_bitmap = clusters .ValveConfigurationAndControl .types .Feature .LEVEL }) > 0 then
537
- profile_name = profile_name .. " -level"
538
- end
539
- end
540
-
541
- if profile_name then
542
- device :try_update_metadata ({ profile = profile_name })
543
- end
544
- end
545
-
546
520
local function configure_buttons (device )
547
521
if device .network_type == device_lib .NETWORK_TYPE_CHILD then
548
522
return
@@ -596,7 +570,7 @@ local function try_build_button_component_map(device, main_endpoint, button_eps)
596
570
component_map [button_component ] = ep
597
571
end
598
572
end
599
- device :set_field (COMPONENT_TO_ENDPOINT_MAP_BUTTON , component_map , {persist = true })
573
+ device :set_field (COMPONENT_TO_ENDPOINT_MAP , component_map , {persist = true })
600
574
end
601
575
end
602
576
@@ -656,8 +630,6 @@ local function try_build_child_switch_profiles(driver, device, switch_eps, main_
656
630
device :set_field (IS_PARENT_CHILD_DEVICE , true , {persist = true })
657
631
end
658
632
659
- device :set_field (SWITCH_INITIALIZED , true , {persist = true })
660
-
661
633
-- this is needed in initialize_buttons_and_switches
662
634
return num_switch_server_eps
663
635
end
@@ -725,20 +697,10 @@ local function device_init(driver, device)
725
697
if device .network_type ~= device_lib .NETWORK_TYPE_MATTER then
726
698
return
727
699
end
728
-
700
+ check_field_name_updates ( device )
729
701
device :set_component_to_endpoint_fn (component_to_endpoint )
730
702
device :set_endpoint_to_component_fn (endpoint_to_component )
731
-
732
703
local main_endpoint = find_default_endpoint (device )
733
- if not device :get_field (COMPONENT_TO_ENDPOINT_MAP ) and -- this field is only set for old MCD devices. See comments in the field def.
734
- not device :get_field (SWITCH_INITIALIZED ) and
735
- not detect_bridge (device ) then
736
- -- initialize the main device card with buttons if applicable, and create child devices as needed for multi-switch devices.
737
- initialize_buttons_and_switches (driver , device , main_endpoint )
738
- end
739
- if device :get_field (IS_PARENT_CHILD_DEVICE ) then
740
- device :set_find_child (find_child )
741
- end
742
704
-- ensure subscription to all endpoint attributes- including those mapped to child devices
743
705
for _ , ep in ipairs (device .endpoints ) do
744
706
if ep .endpoint_id ~= main_endpoint then
@@ -760,6 +722,47 @@ local function device_init(driver, device)
760
722
device :subscribe ()
761
723
end
762
724
725
+ local function do_configure (driver , device )
726
+ if not detect_bridge (device ) then
727
+ local main_endpoint = find_default_endpoint (device )
728
+ -- initialize the main device card with buttons if applicable, and create child devices as needed for multi-switch devices.
729
+ initialize_buttons_and_switches (driver , device , main_endpoint )
730
+ if device :get_field (IS_PARENT_CHILD_DEVICE ) then
731
+ device :set_find_child (find_child )
732
+ end
733
+ if device :get_field (BUTTON_DEVICE_PROFILED ) then
734
+ return
735
+ end
736
+ end
737
+
738
+ local level_eps = embedded_cluster_utils .get_endpoints (device , clusters .LevelControl .ID )
739
+ local energy_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalEnergyMeasurement .ID )
740
+ local power_eps = embedded_cluster_utils .get_endpoints (device , clusters .ElectricalPowerMeasurement .ID )
741
+ local valve_eps = embedded_cluster_utils .get_endpoints (device , clusters .ValveConfigurationAndControl .ID )
742
+ local profile_name = nil
743
+ local level_support = " "
744
+ if # level_eps > 0 then
745
+ level_support = " -level"
746
+ end
747
+ if # energy_eps > 0 and # power_eps > 0 then
748
+ profile_name = " plug" .. level_support .. " -power-energy-powerConsumption"
749
+ elseif # energy_eps > 0 then
750
+ profile_name = " plug" .. level_support .. " -energy-powerConsumption"
751
+ elseif # power_eps > 0 then
752
+ profile_name = " plug" .. level_support .. " -power"
753
+ elseif # valve_eps > 0 then
754
+ profile_name = " water-valve"
755
+ if # embedded_cluster_utils .get_endpoints (device , clusters .ValveConfigurationAndControl .ID ,
756
+ {feature_bitmap = clusters .ValveConfigurationAndControl .types .Feature .LEVEL }) > 0 then
757
+ profile_name = profile_name .. " -level"
758
+ end
759
+ end
760
+
761
+ if profile_name then
762
+ device :try_update_metadata ({ profile = profile_name })
763
+ end
764
+ end
765
+
763
766
local function device_removed (driver , device )
764
767
log .info (" device removed" )
765
768
delete_import_poll_schedule (device )
0 commit comments