@@ -178,60 +178,40 @@ function utils.component_to_endpoint(device, component)
178178 return utils .find_default_endpoint (device )
179179end
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"
215203end
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
226212function 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 )
247226end
0 commit comments