Skip to content

Commit d957e69

Browse files
authored
Matter Switch: Simplify component to endpoint mapping logic (#2560)
1 parent 2389712 commit d957e69

File tree

3 files changed

+17
-49
lines changed

3 files changed

+17
-49
lines changed

drivers/SmartThings/matter-switch/src/sub_drivers/camera/camera_handlers/attribute_handlers.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,13 @@ end
396396
function CameraAttributeHandlers.camera_av_stream_management_attribute_list_handler(driver, device, ib, response)
397397
if not ib.data.elements then return end
398398
local status_light_enabled_present, status_light_brightness_present = false, false
399-
local attribute_ids, capability_ids = {}, {}
399+
local attribute_ids = {}
400400
for _, attr in ipairs(ib.data.elements) do
401401
if attr.value == clusters.CameraAvStreamManagement.attributes.StatusLightEnabled.ID then
402402
status_light_enabled_present = true
403-
table.insert(capability_ids, capabilities.switch.ID)
404403
table.insert(attribute_ids, clusters.CameraAvStreamManagement.attributes.StatusLightEnabled.ID)
405404
elseif attr.value == clusters.CameraAvStreamManagement.attributes.StatusLightBrightness.ID then
406405
status_light_brightness_present = true
407-
table.insert(capability_ids, capabilities.mode.ID)
408406
table.insert(attribute_ids, clusters.CameraAvStreamManagement.attributes.StatusLightBrightness.ID)
409407
end
410408
end
@@ -413,7 +411,6 @@ function CameraAttributeHandlers.camera_av_stream_management_attribute_list_hand
413411
endpoint_id = ib.endpoint_id,
414412
cluster_id = ib.cluster_id,
415413
attribute_ids = attribute_ids,
416-
capability_ids = capability_ids
417414
}
418415
device:set_field(fields.COMPONENT_TO_ENDPOINT_MAP, component_map, {persist=true})
419416
camera_cfg.match_profile(device, status_light_enabled_present, status_light_brightness_present)

drivers/SmartThings/matter-switch/src/sub_drivers/camera/camera_utils/utils.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ function CameraUtils.update_camera_component_map(device)
3535
clusters.CameraAvStreamManagement.attributes.MicrophoneMaxLevel.ID,
3636
clusters.CameraAvStreamManagement.attributes.MicrophoneMinLevel.ID,
3737
},
38-
capability_ids = {
39-
capabilities.audioMute.ID,
40-
capabilities.audioVolume.ID,
41-
}
4238
}
4339
end
4440
if CameraUtils.feature_supported(device, clusters.CameraAvStreamManagement.ID, clusters.CameraAvStreamManagement.types.Feature.VIDEO) then
@@ -51,10 +47,6 @@ function CameraUtils.update_camera_component_map(device)
5147
clusters.CameraAvStreamManagement.attributes.SpeakerMaxLevel.ID,
5248
clusters.CameraAvStreamManagement.attributes.SpeakerMinLevel.ID,
5349
},
54-
capability_ids = {
55-
capabilities.audioMute.ID,
56-
capabilities.audioVolume.ID,
57-
}
5850
}
5951
end
6052
device:set_field(fields.COMPONENT_TO_ENDPOINT_MAP, component_map, {persist = true})

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

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -178,60 +178,40 @@ function utils.component_to_endpoint(device, component)
178178
return utils.find_default_endpoint(device)
179179
end
180180

181-
--- An extension of the library function endpoint_to_component, to support a mapping scheme
182-
--- that includes cluster and attribute id's so that we can use multiple components for a
183-
--- single endpoint.
181+
--- An extension of the library function endpoint_to_component, used to support a mapping scheme
182+
--- that optionally includes cluster and attribute ids so that multiple components can be mapped
183+
--- to a single endpoint.
184184
---
185185
--- @param device any a Matter device object
186-
--- @param opts number|table either is an ep_id or a table { endpoint_id, capability_id }
186+
--- @param ep_info number|table either an ep_id or a table { endpoint_id, optional(cluster_id), optional(attribute_id) }
187+
--- where cluster_id is required for an attribute_id to be handled.
187188
--- @return string component
188-
function utils.endpoint_to_component(device, opts)
189-
local ep_info = {}
190-
if type(opts) == "number" then
191-
ep_info.endpoint_id = opts
192-
elseif type(opts) == "table" then
193-
if opts.endpoint_info then
194-
ep_info = opts.endpoint_info
195-
else
196-
ep_info = {
197-
endpoint_id = opts.endpoint_id,
198-
cluster_id = opts.cluster_id,
199-
attribute_id = opts.attribute_id
200-
}
201-
end
189+
function utils.endpoint_to_component(device, ep_info)
190+
if type(ep_info) == "number" then
191+
ep_info = { endpoint_id = ep_info }
202192
end
203193
for component, map_info in pairs(device:get_field(fields.COMPONENT_TO_ENDPOINT_MAP) or {}) do
204194
if type(map_info) == "number" and map_info == ep_info.endpoint_id then
205195
return component
206-
elseif type(map_info) == "table" and map_info.endpoint_id == ep_info.endpoint_id then
207-
if (not map_info.cluster_id or (map_info.cluster_id == ep_info.cluster_id
208-
and utils.tbl_contains(map_info.attribute_ids, ep_info.attribute_id)))
209-
and (not opts.capability_id or utils.tbl_contains(map_info.capability_ids, opts.capability_id)) then
196+
elseif type(map_info) == "table" and map_info.endpoint_id == ep_info.endpoint_id
197+
and (not map_info.cluster_id or (map_info.cluster_id == ep_info.cluster_id
198+
and (not map_info.attribute_ids or utils.tbl_contains(map_info.attribute_ids, ep_info.attribute_id)))) then
210199
return component
211-
end
212200
end
213201
end
214202
return "main"
215203
end
216204

217-
--- An extension of the library function emit_event_for_endpoint, to support devices with
218-
--- multiple components defined for the same endpoint, since they can't be easily
219-
--- differentiated based on a simple endpoint id to component mapping, but we can extend
220-
--- this mapping to include the cluster and attribute id's so that we know which component
221-
--- to route events to.
205+
--- An extension of the library function emit_event_for_endpoint, used to support devices with
206+
--- multiple components mapped to the same endpoint. This is handled by extending the parameters to optionally
207+
--- include a cluster id and attribute id for more specific routing
222208
---
223209
--- @param device any a Matter device object
224-
--- @param ep_info number|table endpoint_id or ib (includes endpoint_id, cluster_id, attribute_id)
210+
--- @param ep_info number|table endpoint_id or an ib (the ib data includes endpoint_id, cluster_id, and attribute_id fields)
225211
--- @param event any a capability event object
226212
function utils.emit_event_for_endpoint(device, ep_info, event)
227213
if type(ep_info) == "number" then
228214
ep_info = { endpoint_id = ep_info }
229-
elseif type(ep_info) == "table" then
230-
ep_info = {
231-
endpoint_id = ep_info.endpoint_id,
232-
cluster_id = ep_info.cluster_id,
233-
attribute_id = ep_info.attribute_id
234-
}
235215
end
236216
if device:get_field(fields.IS_PARENT_CHILD_DEVICE) then
237217
local child = utils.find_child(device, ep_info.endpoint_id)
@@ -240,8 +220,7 @@ function utils.emit_event_for_endpoint(device, ep_info, event)
240220
return
241221
end
242222
end
243-
local opts = { endpoint_info = ep_info, capability_id = event.capability.ID }
244-
local comp_id = utils.endpoint_to_component(device, opts)
223+
local comp_id = utils.endpoint_to_component(device, ep_info)
245224
local comp = device.profile.components[comp_id]
246225
device:emit_component_event(comp, event)
247226
end

0 commit comments

Comments
 (0)