ESPHome 2026.6.4
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_core.h"
12#include "zboss_api.h"
13#include "ha/esp_zigbee_ha_standard.h"
17
18#ifdef USE_BINARY_SENSOR
20#endif
21
22namespace esphome::zigbee {
23
24/* Zigbee configuration */
25static const uint16_t ED_KEEP_ALIVE = 3000; /* 3000 millisecond */
26static const uint8_t MAX_CHILDREN = 10;
27
28#define ESP_ZB_DEFAULT_RADIO_CONFIG() \
29 { .radio_mode = ZB_RADIO_MODE_NATIVE, }
30
31#define ESP_ZB_DEFAULT_HOST_CONFIG() \
32 { .host_connection_mode = ZB_HOST_CONNECTION_MODE_NONE, }
33
34uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
35
36class ZigbeeAttribute;
37
38class ZigbeeComponent : public Component {
39 public:
40 void setup() override;
41 void loop() override;
42 void dump_config() override;
43 esp_err_t create_endpoint(uint8_t endpoint_id, zb_ha_standard_devs_e device_id,
44 esp_zb_cluster_list_t *esp_zb_cluster_list);
45 void set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source);
46 void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role);
47 void create_default_cluster(uint8_t endpoint_id, zb_ha_standard_devs_e device_id);
48
49 template<typename T>
50 void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
51 uint8_t max_size, T value);
52
53 template<typename T>
54 void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
55
57 esp_zb_lock_acquire(portMAX_DELAY);
58 esp_zb_factory_reset(); // triggers a reboot
59 esp_zb_lock_release();
60 }
61
62 template<typename F> void add_on_join_callback(F &&cb) { this->join_cb_.add(std::forward<F>(cb)); }
63
64 bool is_battery_powered() { return this->basic_cluster_data_.power_source == ESP_ZB_ZCL_BASIC_POWER_SOURCE_BATTERY; }
65 bool is_started() { return this->started; }
66 bool is_connected() { return this->connected_; }
67 std::atomic<bool> started = false;
68 std::atomic<bool> joined = false;
69 std::atomic<bool> factory_new = false;
70
71 protected:
72 struct {
73 uint8_t *model;
74 uint8_t *manufacturer;
75 uint8_t *date;
76 uint8_t power_source;
78 bool connected_ = false;
79#ifdef ZB_ED_ROLE
80 esp_zb_nwk_device_type_t device_role_ = ESP_ZB_DEVICE_TYPE_ED;
81#else
82 esp_zb_nwk_device_type_t device_role_ = ESP_ZB_DEVICE_TYPE_ROUTER;
83#endif
84 esp_zb_attribute_list_t *create_basic_cluster_();
85 template<typename T>
86 void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
87 T *value_p);
88 // endpoint_list_ and attribute_list_ are only used during setup and are cleared afterwards
89 // value tuple could be replaced by struct
90 std::map<uint8_t, std::tuple<zb_ha_standard_devs_e, esp_zb_cluster_list_t *>> endpoint_list_;
91 // key tuple could be replaced by single 32 bit int with bit fields for endpoint, cluster and role
92 std::map<std::tuple<uint8_t, uint16_t, uint8_t>, esp_zb_attribute_list_t *> attribute_list_;
93 // attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
94 // automations
95 // key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
96 std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
97 esp_zb_ep_list_t *esp_zb_ep_list_ = esp_zb_ep_list_create();
99};
100
101template<typename T>
102void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
103 uint8_t max_size, T value) {
104 this->add_attr<T>(nullptr, endpoint_id, cluster_id, role, attr_id, max_size, value);
105}
106
107template<typename T>
108void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
109 uint16_t attr_id, uint8_t max_size, T value) {
110 // The size byte of the zcl_str must be set to the maximum value,
111 // even though the initial string may be shorter.
112 if constexpr (std::is_same<T, std::string>::value) {
113 auto zcl_str = get_zcl_string(value.c_str(), max_size, true);
114 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, zcl_str);
115 delete[] zcl_str;
116 } else if constexpr (std::is_convertible<T, const char *>::value) {
117 auto zcl_str = get_zcl_string(value, max_size, true);
118 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, zcl_str);
119 delete[] zcl_str;
120 } else {
121 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, &value);
122 }
123}
124
125template<typename T>
126void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
127 uint16_t attr_id, T *value_p) {
128 esp_zb_attribute_list_t *attr_list = this->attribute_list_[{endpoint_id, cluster_id, role}];
129 esphome_zb_cluster_add_or_update_attr(cluster_id, attr_list, attr_id, value_p);
130
131 if (attr != nullptr) {
132 this->attributes_[{endpoint_id, cluster_id, role, attr_id}] = attr;
133 }
134}
135
136} // namespace esphome::zigbee
137
138#endif
139#endif
CallbackManager< void(bool)> join_cb_
esp_zb_ep_list_t * esp_zb_ep_list_
struct esphome::zigbee::ZigbeeComponent::@200 basic_cluster_data_
std::map< std::tuple< uint8_t, uint16_t, uint8_t, uint16_t >, ZigbeeAttribute * > attributes_
esp_zb_nwk_device_type_t device_role_
void create_default_cluster(uint8_t endpoint_id, zb_ha_standard_devs_e device_id)
void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role)
std::atomic< bool > factory_new
std::map< uint8_t, std::tuple< zb_ha_standard_devs_e, esp_zb_cluster_list_t * > > endpoint_list_
std::map< std::tuple< uint8_t, uint16_t, uint8_t >, esp_zb_attribute_list_t * > attribute_list_
void set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source)
esp_err_t create_endpoint(uint8_t endpoint_id, zb_ha_standard_devs_e device_id, esp_zb_cluster_list_t *esp_zb_cluster_list)
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value)
esp_zb_attribute_list_t * create_basic_cluster_()
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, T *value_p)
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
esp_err_t esphome_zb_cluster_add_or_update_attr(uint16_t cluster_id, esp_zb_attribute_list_t *attr_list, uint16_t attr_id, void *value_p)