ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
zigbee_esp32.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_ESP32
5#ifdef USE_ZIGBEE
6
7#include <map>
8#include <tuple>
9#include <atomic>
10
11#include "esp_zigbee.h"
12#include "ezbee/zha.h"
18#include "zigbee_event_esp32.h"
19
20namespace esphome::zigbee {
21
22/* Zigbee configuration */
23static const uint8_t MAX_CHILDREN = 10;
24static const uint32_t EZB_PRIMARY_CHANNEL_MASK = 0x07FFF800U; /* channels 11-26 */
25static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
26
27#define EZB_DEFAULT_RADIO_CONFIG() \
28 { \
29 .radio_mode = ESP_ZIGBEE_RADIO_MODE_NATIVE, \
30 }
31
32uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
33
35 public:
36 virtual void on_value(ezb_zcl_attribute_t attribute) = 0;
37 virtual void report() = 0;
38};
39
40class ZigbeeComponent final : public Component {
41 public:
42 void setup() override;
43 void loop() override;
44 void dump_config() override;
45
46 // After entities that build the data model (AFTER_BLUETOOTH+); dependents use BEFORE_CONNECTION.
47 float get_setup_priority() const override { return setup_priority::WIFI; }
48
49 void set_sleepy(bool sleepy) { this->sleepy_ = sleepy; }
50 void set_keep_alive(uint32_t keep_alive) { this->keep_alive_ = keep_alive; }
51 void set_basic_cluster(const char *model, const char *manufacturer, const char *location, const char *sw_build_id,
52 uint8_t power_source, uint8_t app_version);
53 void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role);
54 void create_default_cluster(uint8_t endpoint_id, uint16_t device_id);
55 bool register_device();
56
57 template<typename T>
58 void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
59 uint16_t attr_id, uint8_t max_size, uint8_t attr_type, uint8_t attr_access, T value);
60
61 template<typename T>
62 void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size,
63 uint8_t attr_type, uint8_t attr_access, T value);
64
65 static bool app_signal_handler(const ezb_app_signal_t *app_signal);
66 static void esp_zigbee_alarm_bdb_commissioning(ezb_bdb_comm_mode_mask_t mode);
67
68 void factory_reset();
69 void report();
70
71 template<typename F> void add_on_join_callback(F &&cb) { this->join_cb_.add(std::forward<F>(cb)); }
72 template<typename F> void add_on_start_callback(F &&cb) { this->start_cb_.add(std::forward<F>(cb)); }
73
74 bool is_battery_powered() { return this->basic_cluster_data_.power_source == EZB_ZCL_BASIC_POWER_SOURCE_BATTERY; }
75
76 // True after the Zigbee stack has been initialized and the device has started up. Is set before the stack started
77 // network commissioning or has joined a network and won't be reset until the device is rebooted.
78 bool is_started() { return this->started_; }
79
80 // True if the device has joined a network and is ready to send and receive messages.
81 bool is_joined() { return this->joined_; }
82
83 protected:
84 bool sleepy_{false};
86 struct {
87 uint8_t *model;
88 uint8_t *manufacturer;
89 uint8_t *date;
90 uint8_t *location;
91 uint8_t *sw_build_id;
92 uint8_t power_source;
93 uint8_t app_version;
95#ifdef CONFIG_ZB_ZED
96 ezb_nwk_device_type_t device_role_ = EZB_NWK_DEVICE_TYPE_END_DEVICE;
97#else
98 ezb_nwk_device_type_t device_role_ = EZB_NWK_DEVICE_TYPE_ROUTER;
99#endif
100 void update_basic_cluster_(ezb_af_ep_desc_t ep_desc);
102 void end_parent_link_failures_(uint8_t seq);
104 template<typename T>
105 void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
106 uint16_t attr_id, uint8_t attr_type, uint8_t attr_access, T *value_p);
107 void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
108 // attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
109 // automations
110 // key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
111 std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttributeInterface *> attributes_;
112 ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
114 template<typename... Args> friend void enqueue_zb_event(Args... args);
116 // Pool sized to queue capacity (SIZE-1) because LockFreeQueue<T,N> is a ring
117 // buffer that holds N-1 elements (one slot distinguishes full from empty).
118 // This guarantees allocate() returns nullptr before push() can fail, which:
119 // 1. Prevents leaking a pool slot (the Nth allocate succeeds but push fails)
120 // 2. Avoids needing release() on the producer path after a failed push(),
121 // preserving the SPSC contract on the pool's internal free list
124 bool start_reported_{false};
125 std::atomic<bool> started_ = false;
126 std::atomic<bool> joined_ = false;
127 std::atomic<bool> join_pending_ = false;
128 std::atomic<bool> factory_new_ = false;
129 // Current series of parent link failures, only used with the Zigbee stack lock held
133 uint8_t link_failures_{0};
135};
136
137template<typename T>
138void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
139 uint8_t max_size, uint8_t attr_type, uint8_t attr_access, T value) {
140 this->add_attr<T>(nullptr, endpoint_id, cluster_id, role, attr_id, max_size, attr_type, attr_access, value);
141}
142
143template<typename T>
144void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
145 uint16_t attr_id, uint8_t max_size, uint8_t attr_type, uint8_t attr_access, T value) {
146 // The size byte of the zcl_str must be set to the maximum value,
147 // even though the initial string may be shorter.
148 if constexpr (std::is_same<T, std::string>::value) {
149 auto zcl_str = get_zcl_string(value.c_str(), max_size, true);
150 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, attr_type, attr_access, zcl_str);
151 delete[] zcl_str;
152 } else if constexpr (std::is_convertible<T, const char *>::value) {
153 auto zcl_str = get_zcl_string(value, max_size, true);
154 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, attr_type, attr_access, zcl_str);
155 delete[] zcl_str;
156 } else {
157 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, attr_type, attr_access, &value);
158 }
159}
160
161template<typename T>
162void ZigbeeComponent::add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
163 uint16_t attr_id, uint8_t attr_type, uint8_t attr_access, T *value_p) {
164 ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
165 if (ep_desc == NULL) {
166 return;
167 }
168 ezb_zcl_cluster_desc_t cluster_desc = ezb_af_endpoint_get_cluster_desc(ep_desc, cluster_id, role);
169 if (cluster_desc == NULL) {
170 return;
171 }
172 esphome_zb_cluster_add_or_update_attr(cluster_id, cluster_desc, attr_id, attr_type, attr_access, value_p);
173
174 if (attr != nullptr) {
175 this->attributes_[{endpoint_id, cluster_id, role, attr_id}] = attr;
176 }
177}
178
179} // namespace esphome::zigbee
180
181#endif
182#endif
BedjetMode mode
BedJet operating mode.
virtual void on_value(ezb_zcl_attribute_t attribute)=0
float get_setup_priority() const override
void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t attr_type, uint8_t attr_access, T *value_p)
CallbackManager< void(bool)> join_cb_
void create_default_cluster(uint8_t endpoint_id, uint16_t device_id)
friend void enqueue_zb_event(Args... args)
std::atomic< bool > factory_new_
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute)
esphome::LockFreeQueue< ZBEvent, MAX_ZB_QUEUE_SIZE > zb_events_
void update_basic_cluster_(ezb_af_ep_desc_t ep_desc)
struct esphome::zigbee::ZigbeeComponent::@207 basic_cluster_data_
void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role)
static void esp_zigbee_alarm_bdb_commissioning(ezb_bdb_comm_mode_mask_t mode)
LazyCallbackManager< void()> start_cb_
static bool app_signal_handler(const ezb_app_signal_t *app_signal)
void set_keep_alive(uint32_t keep_alive)
std::atomic< bool > join_pending_
ezb_nwk_device_type_t device_role_
void set_basic_cluster(const char *model, const char *manufacturer, const char *location, const char *sw_build_id, uint8_t power_source, uint8_t app_version)
void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, uint8_t attr_type, uint8_t attr_access, T value)
std::map< std::tuple< uint8_t, uint16_t, uint8_t, uint16_t >, ZigbeeAttributeInterface * > attributes_
void end_parent_link_failures_(uint8_t seq)
esphome::EventPool< ZBEvent, MAX_ZB_QUEUE_SIZE - 1 > zb_event_pool_
ezb_af_device_desc_t dev_desc_
constexpr float WIFI
Definition component.h:51
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
uint32_t value
Definition hal.cpp:59
static void uint32_t
uint16_t seq
ezb_err_t esphome_zb_cluster_add_or_update_attr(uint16_t cluster_id, ezb_zcl_cluster_desc_t cluster_desc, uint16_t attr_id, uint8_t attr_type, uint8_t attr_access, void *value_p)