Skip to content

feat(zigbee): Remove static variables, improve binding, new example #11316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

P-R-O-C-H-Y
Copy link
Member

Description of Change

New Example: Zigbee Multi-Switch

  • Implemented the Zigbee_On_Off_MultiSwitch.ino sketch, which includes:
    • Configuration and control of up to three Zigbee lights.
    • Persistent storage of light configurations in NVS.
    • Serial command handling for light control, network management, and factory reset.

Enhancements to Zigbee Core Library

  • Added a new APS data indication handler to process bind/unbind requests and update the bound devices list dynamically. [1] [2]
  • Introduced a closeNetwork method to complement the existing openNetwork method, allowing for better network management.
  • Enhanced the Zigbee application signal handler to handle device updates for routers and improve endpoint binding logic. [1] [2]
  • Added a _debug flag to ZigbeeCore for enabling detailed logs during development.

These changes collectively improve the functionality and usability of the Zigbee library while providing a new example to demonstrate its capabilities.

Tests scenarios

Tested using 7x ESP32C6 (1 piece loaded with the new MultiSwitch example as Coordinator or Router, 6 pieces loaded with On/Off Light example).

  • When MultiSwitch acted as coordinator, all lights were able to join. and bind to the switch and can be controlled
  • When MultiSwitch acted as a router, it connects to a network running in HomeAssistant (ZHA), and when I bind all 6 lights that were also connected, I was able to control them.

Related links

Please provide links to related issue, PRs etc.

Related #11219

@P-R-O-C-H-Y P-R-O-C-H-Y added Status: Review needed Issue or PR is awaiting review Area: Zigbee Issues and Feature Request about Zigbee labels Apr 28, 2025
@P-R-O-C-H-Y P-R-O-C-H-Y added this to the 3.3.0 milestone Apr 28, 2025
@P-R-O-C-H-Y P-R-O-C-H-Y self-assigned this Apr 28, 2025
Copy link
Contributor

github-actions bot commented Apr 28, 2025

Messages
📖 🎉 Good Job! All checks are passing!

👋 Hello P-R-O-C-H-Y, we appreciate your contribution to this project!


📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more.

🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project.

Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against db5d678

Copy link
Contributor

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32S30‼️ +4K0.00⚠️ +0.550⚠️ +1120.00⚠️ +0.30
ESP32S20‼️ +3K0.00⚠️ +0.440⚠️ +800.00⚠️ +0.25
ESP32C30‼️ +3K0.00⚠️ +0.510⚠️ +800.00⚠️ +0.28
ESP32C60‼️ +4K0.00⚠️ +0.790⚠️ +880.00⚠️ +0.31
ESP32H20‼️ +4K0.00⚠️ +0.770⚠️ +960.00⚠️ +0.34
ESP320‼️ +4K0.00⚠️ +0.580⚠️ +1040.00⚠️ +0.26
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32S3ESP32S2ESP32C3ESP32C6ESP32H2ESP32
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch‼️ +3K⚠️ +104‼️ +3K⚠️ +80‼️ +3K⚠️ +72‼️ +4K⚠️ +72‼️ +4K⚠️ +80‼️ +4K⚠️ +96
libraries/Zigbee/examples/Zigbee_Gateway‼️ +3K⚠️ +104‼️ +3K⚠️ +72‼️ +3K⚠️ +64----‼️ +4K⚠️ +104
libraries/Zigbee/examples/Zigbee_On_Off_Switch‼️ +3K⚠️ +88‼️ +3K⚠️ +72‼️ +3K⚠️ +72‼️ +4K⚠️ +72‼️ +4K⚠️ +80‼️ +4K⚠️ +88
libraries/Zigbee/examples/Zigbee_Range_Extender‼️ +3K⚠️ +112‼️ +3K⚠️ +80‼️ +3K⚠️ +56‼️ +4K⚠️ +88‼️ +4K⚠️ +80‼️ +4K⚠️ +96
libraries/Zigbee/examples/Zigbee_Thermostat‼️ +4K⚠️ +96‼️ +3K⚠️ +80‼️ +3K⚠️ +80‼️ +4K⚠️ +80‼️ +4K⚠️ +88‼️ +4K⚠️ +96
libraries/Zigbee/examples/Zigbee_Analog_Input_Output------‼️ +4K⚠️ +88‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_CarbonDioxide_Sensor------‼️ +4K⚠️ +72‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light------‼️ +4K⚠️ +64‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Contact_Switch------‼️ +4K⚠️ +88‼️ +4K⚠️ +96--
libraries/Zigbee/examples/Zigbee_Dimmable_Light------‼️ +4K⚠️ +80‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Illuminance_Sensor------‼️ +4K⚠️ +80‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_OTA_Client------‼️ +4K⚠️ +80‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Occupancy_Sensor------‼️ +4K⚠️ +80‼️ +4K⚠️ +88--
libraries/Zigbee/examples/Zigbee_On_Off_Light------‼️ +4K⚠️ +80‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_On_Off_MultiSwitch------------
libraries/Zigbee/examples/Zigbee_PM25_Sensor------‼️ +4K⚠️ +72‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_Pressure_Flow_Sensor------‼️ +4K⚠️ +88‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_Scan_Networks------‼️ +4K⚠️ +88‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy------‼️ +4K⚠️ +80‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Temperature_Sensor------‼️ +4K⚠️ +88‼️ +4K⚠️ +80--
libraries/Zigbee/examples/Zigbee_Vibration_Sensor------‼️ +4K⚠️ +88‼️ +4K⚠️ +96--
libraries/Zigbee/examples/Zigbee_Wind_Speed_Sensor------‼️ +4K⚠️ +80‼️ +4K⚠️ +72--
libraries/Zigbee/examples/Zigbee_Window_Covering------‼️ +4K⚠️ +80‼️ +4K⚠️ +80--

Copy link
Contributor

Test Results

 76 files   76 suites   12m 42s ⏱️
 38 tests  38 ✅ 0 💤 0 ❌
241 runs  241 ✅ 0 💤 0 ❌

Results for commit db5d678.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new Zigbee multi-switch example and refactors the core Zigbee binding functionality to remove global static state in favor of instance‐based callbacks. Key changes include replacing static binding callbacks with instance methods and wrappers in endpoint classes, adding dynamic bound device management methods in ZigbeeEP, and integrating a new example sketch for a multi-switch controller.

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
libraries/Zigbee/src/ep/ZigbeeThermostat.{h,cpp} Refactored binding callbacks to instance methods and added _device.
libraries/Zigbee/src/ep/ZigbeeSwitch.{h,cpp} Updated binding callbacks and improved binding logic for switches.
libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.{h,cpp} Similar refactoring for color dimmer switches callbacks.
libraries/Zigbee/src/ZigbeeEP.{h,cpp} Removed static state and introduced dynamic bound device management.
libraries/Zigbee/src/ZigbeeCore.{h,cpp} Added network close functionality and enhanced binding table logic.
libraries/Zigbee/examples/Zigbee_On_Off_MultiSwitch/{ino,README.md} New multi-switch example to demonstrate the improved Zigbee binding.
Files not reviewed (1)
  • libraries/Zigbee/examples/Zigbee_On_Off_MultiSwitch/ci.json: Language not supported
Comments suppressed due to low confidence (3)

libraries/Zigbee/src/ep/ZigbeeSwitch.cpp:56

  • Consider checking the return value of malloc to ensure that the allocated pointer is not null, which can prevent potential null pointer dereferences.
zb_device_params_t *light = (zb_device_params_t *)malloc(sizeof(zb_device_params_t));

libraries/Zigbee/src/ZigbeeEP.cpp:562

  • The static local variables 'found_devices' and 'all_records' used in bindingTableCb persist across callback invocations; consider adding thread-safety measures or proper documentation to ensure they are safe in concurrent contexts.
static std::set<DeviceIdentifier> found_devices;

libraries/Zigbee/src/ZigbeeCore.cpp:447

  • Replace the magic literal 0x00 with a defined constant (e.g., APS_SUCCESS) to improve code clarity and maintainability.
if (ind.status == 0x00)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Zigbee Issues and Feature Request about Zigbee Status: Review needed Issue or PR is awaiting review
Projects
Development

Successfully merging this pull request may close these issues.

2 participants