Skip to content

Conversation

@paulr34
Copy link
Collaborator

@paulr34 paulr34 commented Nov 18, 2025

Automatic Endpoint Creation for Device Type Composition Requirements
Note: This PR depends on the backend queries PR (#XXX). Please merge that PR first.

Overview
This PR adds automatic endpoint creation when device types have composition requirements. When adding an endpoint with a device type that requires additional endpoints (e.g., Refrigerator requires Temperature Controlled Cabinet), the required endpoints are automatically created.

…ements functionality. Added new database queries and REST API endpoints to retrieve device type composition requirements. Enhanced loadComposition and addEndpoint functions to automatically create required endpoints based on composition constraints. Updated template to reflect new requirements in endpoint configuration.
- Remove redundant recursion by adding skipCompositionCheck parameter to addEndpoint
- Add cycle detection using compositionChain Set to prevent infinite loops
- Remove all debug console.log statements
- Maintain backward compatibility with default parameter value
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @paulr34, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances ZAP's capability to handle complex Matter device configurations by introducing automatic endpoint creation based on device type composition requirements. This feature ensures that when a primary device type is added, any necessary dependent device types are automatically provisioned as separate endpoints, adhering to specified conformance and constraint rules. The changes span across documentation, template helpers, backend API, database queries, and the frontend Vuex store to provide a complete and integrated solution.

Highlights

  • Automatic Endpoint Creation: The system now automatically creates required endpoints when a device type with composition requirements is added, streamlining the setup of complex Matter devices.
  • New Documentation: A comprehensive documentation file (docs/endpoint-composition.md) has been added, detailing the feature's overview, database structure, template usage, REST API, database queries, XML definition, and best practices.
  • New Template Helper: A new Handlebars helper, user_endpoint_composition_requirements, is introduced, allowing templates to easily query and display endpoint composition requirements.
  • Backend API Endpoints: Two new REST API endpoints (/zcl/endpointCompositionRequirements and /zcl/deviceTypesByEndpointTypeId) have been added to facilitate querying composition data from the backend.
  • Vuex Store Integration: The addEndpoint action in the Vuex store has been updated to trigger the automatic creation of required endpoints, including recursive processing and cycle detection for nested compositions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@mergify
Copy link

mergify bot commented Nov 18, 2025

⚠️ The sha of the head commit of this PR conflicts with #1668. Mergify cannot evaluate rules on this PR. ⚠️

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant feature for automatic endpoint creation based on composition requirements, which is particularly useful for Matter device types. The implementation is comprehensive, covering backend queries, REST APIs, frontend logic, and template helpers, along with documentation and tests. I've identified a few critical and high-severity issues in the endpoint creation logic that could lead to incorrect behavior, such as endpoint ID collisions and incorrect handling of constraints. Additionally, there are some minor suggestions for improving the new documentation. Overall, this is a valuable contribution that will be even stronger with these issues addressed.

}

let createdEndpoints = []
let nextEndpointId = parentEndpointIdentifier + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current logic for determining nextEndpointId by simply incrementing the parentEndpointIdentifier can lead to endpoint ID collisions if an endpoint with that ID already exists. This could cause existing endpoints to be overwritten silently. To prevent this, the logic should ensure a unique endpoint ID is generated. A robust approach would be to find the maximum existing endpoint ID in the session and start incrementing from there.

    let nextEndpointId =
      context.rootState.zap.endpoints.reduce(
        (maxId, ep) => Math.max(ep.endpointId, maxId),
        0
      ) + 1


// Process each requirement
// Handle constraints - deviceConstraint might indicate minimum count (e.g., "min 1" = 1)
for (let requirement of requirementsRes.data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The code automatically creates endpoints for all composition requirements, but it doesn't check if a requirement is mandatory. This can lead to the creation of unwanted optional endpoints. The automatic creation should only apply to mandatory ('M') requirements. Please add a check for requirement.conformance at the beginning of the loop, like so:

if (requirement.conformance !== 'M') {
  continue;
}

Comment on lines +590 to +602
let minCount = 1
if (requirement.deviceConstraint != null) {
// Try to parse constraint - could be a number or string like "min 1"
if (typeof requirement.deviceConstraint === 'number') {
minCount = Math.max(1, requirement.deviceConstraint)
} else if (typeof requirement.deviceConstraint === 'string') {
// Try to extract number from string like "min 1"
let match = requirement.deviceConstraint.match(/min\s*(\d+)/i)
if (match) {
minCount = parseInt(match[1], 10)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic for parsing deviceConstraint is incomplete. It handles numbers and strings like "min 1", but it doesn't handle plain number strings like "1" or "2". This will cause it to default to creating only one endpoint, even when more are required by the constraint.

      let minCount = 1
      if (requirement.deviceConstraint != null) {
        if (typeof requirement.deviceConstraint === 'number') {
          minCount = requirement.deviceConstraint
        } else if (typeof requirement.deviceConstraint === 'string') {
          const match = requirement.deviceConstraint.match(/\d+/)
          if (match) {
            minCount = parseInt(match[0], 10)
          }
        }
      }

{{/user_endpoints}}
```

**Note**: Automatic cluster merging (where composition-specific clusters override base clusters) is planned for future implementation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The note about cluster merging functionality not being implemented yet is repeated multiple times in this document (e.g., lines 42, 159, 342, 413, 545, 590). To improve readability and reduce redundancy, consider consolidating this information. For example, you could keep the note in the 'Composition-Specific Clusters' section and the 'Best Practices' section, and remove the other occurrences.

Comment on lines +462 to +476
console.log(` Composition-specific clusters: ${compClusters.length}`)
compClusters.forEach((cluster) => {
console.log(` - ${cluster.clusterName}`)
})
}

// 5. Get base clusters for comparison
const baseClusters =
await queryDeviceType.selectDeviceTypeClustersByDeviceTypeRef(
db,
req.requiredDeviceTypeRef
)

console.log(` Base clusters: ${baseClusters.length}`)
console.log(` Composition-specific clusters: ${compClusters.length}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In the queryCompositionData example, the logging of compClusters.length is a bit inconsistent. It's logged inside an if (compClusters.length > 0) block on line 462, and then again unconditionally on line 476. This means if there are no composition-specific clusters, it will only be logged on line 476. Consider making the logging more consistent, for example by removing the log on line 462 and only keeping the summary logs at the end.

@mergify
Copy link

mergify bot commented Nov 19, 2025

⚠️ The sha of the head commit of this PR conflicts with #1668. Mergify cannot evaluate rules on this PR. ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant