Skip to content

Commit b828a5a

Browse files
authored
Matter Switch: Add more reliability to device type checking (#2564)
1 parent e628b47 commit b828a5a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

drivers/SmartThings/matter-switch/src/switch_utils/device_configuration.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function SwitchDeviceConfiguration.assign_profile_for_onoff_ep(device, server_on
2525
-- per spec, the Switch device types support OnOff as CLIENT, though some vendors break spec and support it as SERVER.
2626
local primary_dt_id = switch_utils.find_max_subset_device_type(ep_info, fields.DEVICE_TYPE_ID.LIGHT)
2727
or switch_utils.find_max_subset_device_type(ep_info, fields.DEVICE_TYPE_ID.SWITCH)
28-
or ep_info.device_types[1] and ep_info.device_types[1].device_type_id
28+
or switch_utils.find_primary_device_type(ep_info)
2929

3030
local generic_profile = fields.device_type_profile_map[primary_dt_id]
3131

drivers/SmartThings/matter-switch/src/switch_utils/fields.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SwitchFields.CURRENT_HUESAT_ATTR_MAX = 254
3636

3737
SwitchFields.DEVICE_TYPE_ID = {
3838
AGGREGATOR = 0x000E,
39+
BRIDGED_NODE = 0x0013,
3940
CAMERA = 0x0142,
4041
CHIME = 0x0146,
4142
DIMMABLE_PLUG_IN_UNIT = 0x010B,

drivers/SmartThings/matter-switch/src/switch_utils/utils.lua

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,33 @@ function utils.device_type_supports_button_switch_combination(device, endpoint_i
9393
return utils.tbl_contains(dimmable_eps, endpoint_id)
9494
end
9595

96-
-- Some devices report multiple device types which are a subset of
97-
-- a superset device type (Ex. Dimmable Light is a superset of On/Off Light).
98-
-- We should map to the largest superset device type supported.
99-
-- This can be done by matching to the device type with the highest ID
96+
--- Some devices report multiple device types which are a subset of a superset
97+
--- device type (Ex. Dimmable Light is a superset of On/Off Light). We should map
98+
--- to the largest superset device type supported.
99+
--- This can be done by matching to the device type with the highest ID
100+
--- note: that superset device types have a higher ID than those of their subset
101+
--- is heuristic and could therefore break in the future, were the spec expanded
100102
function utils.find_max_subset_device_type(ep, device_type_set)
101103
if ep.endpoint_id == 0 then return end -- EP-scoped device types not permitted on Root Node
102-
local primary_dt_id = ep.device_types[1] and ep.device_types[1].device_type_id
103-
if utils.tbl_contains(device_type_set, primary_dt_id) then
104-
for _, dt in ipairs(ep.device_types) do
105-
-- only device types in the subset should be considered.
106-
if utils.tbl_contains(device_type_set, dt.device_type_id) then
107-
primary_dt_id = math.max(primary_dt_id, dt.device_type_id)
108-
end
104+
local primary_dt_id = -1
105+
for _, dt in ipairs(ep.device_types) do
106+
-- only device types in the subset should be considered.
107+
if utils.tbl_contains(device_type_set, dt.device_type_id) then
108+
primary_dt_id = math.max(primary_dt_id, dt.device_type_id)
109+
end
110+
end
111+
return (primary_dt_id > 0) and primary_dt_id or nil
112+
end
113+
114+
--- Lights and Switches are Device Types that have Superset-style functionality
115+
--- For all other device types, this function should be used to identify the primary device type
116+
function utils.find_primary_device_type(ep_info)
117+
for _, dt in ipairs(ep_info.device_types) do
118+
if dt.device_type_id ~= fields.DEVICE_TYPE_ID.BRIDGED_NODE then
119+
-- if this is not a bridged node, return the first device type seen
120+
return dt.device_type_id
109121
end
110-
return primary_dt_id
111122
end
112-
return nil
113123
end
114124

115125
--- find_default_endpoint is a helper function to handle situations where

0 commit comments

Comments
 (0)