ESPHome 2026.1.4
Loading...
Searching...
No Matches
zigbee_zephyr.h
Go to the documentation of this file.
1#pragma once
3#if defined(USE_ZIGBEE) && defined(USE_NRF52)
6extern "C" {
7#include <zboss_api.h>
8#include <zboss_api_addons.h>
9}
10
11// copy of ZB_DECLARE_SIMPLE_DESC. Due to https://github.com/nrfconnect/sdk-nrfxlib/pull/666
12#define ESPHOME_ZB_DECLARE_SIMPLE_DESC(ep_name, in_clusters_count, out_clusters_count) \
13 typedef ZB_PACKED_PRE struct zb_af_simple_desc_##ep_name##_##in_clusters_count##_##out_clusters_count##_s { \
14 zb_uint8_t endpoint; /* Endpoint */ \
15 zb_uint16_t app_profile_id; /* Application profile identifier */ \
16 zb_uint16_t app_device_id; /* Application device identifier */ \
17 zb_bitfield_t app_device_version : 4; /* Application device version */ \
18 zb_bitfield_t reserved : 4; /* Reserved */ \
19 zb_uint8_t app_input_cluster_count; /* Application input cluster count */ \
20 zb_uint8_t app_output_cluster_count; /* Application output cluster count */ \
21 /* Application input and output cluster list */ \
22 zb_uint16_t app_cluster_list[(in_clusters_count) + (out_clusters_count)]; \
23 } ZB_PACKED_STRUCT zb_af_simple_desc_##ep_name##_##in_clusters_count##_##out_clusters_count##_t
24
25#define ESPHOME_CAT7(a, b, c, d, e, f, g) a##b##c##d##e##f##g
26// needed to use ESPHOME_ZB_DECLARE_SIMPLE_DESC
27#define ESPHOME_ZB_AF_SIMPLE_DESC_TYPE(ep_name, in_num, out_num) \
28 ESPHOME_CAT7(zb_af_simple_desc_, ep_name, _, in_num, _, out_num, _t)
29
30// needed to use ESPHOME_ZB_DECLARE_SIMPLE_DESC
31#define ESPHOME_ZB_ZCL_DECLARE_SIMPLE_DESC(ep_name, ep_id, in_clust_num, out_clust_num, app_device_id, ...) \
32 ESPHOME_ZB_DECLARE_SIMPLE_DESC(ep_name, in_clust_num, out_clust_num); \
33 ESPHOME_ZB_AF_SIMPLE_DESC_TYPE(ep_name, in_clust_num, out_clust_num) \
34 simple_desc_##ep_name = {ep_id, ZB_AF_HA_PROFILE_ID, app_device_id, 0, 0, in_clust_num, out_clust_num, {__VA_ARGS__}}
35
36// needed to use ESPHOME_ZB_ZCL_DECLARE_SIMPLE_DESC
37#define ESPHOME_ZB_HA_DECLARE_EP(ep_name, ep_id, cluster_list, in_cluster_num, out_cluster_num, report_attr_count, \
38 app_device_id, ...) \
39 ESPHOME_ZB_ZCL_DECLARE_SIMPLE_DESC(ep_name, ep_id, in_cluster_num, out_cluster_num, app_device_id, __VA_ARGS__); \
40 ZBOSS_DEVICE_DECLARE_REPORTING_CTX(reporting_info##ep_name, report_attr_count); \
41 ZB_AF_DECLARE_ENDPOINT_DESC(ep_name, ep_id, ZB_AF_HA_PROFILE_ID, 0, NULL, \
42 ZB_ZCL_ARRAY_SIZE(cluster_list, zb_zcl_cluster_desc_t), cluster_list, \
43 (zb_af_simple_desc_1_1_t *) &simple_desc_##ep_name, report_attr_count, \
44 reporting_info##ep_name, 0, NULL)
45
46namespace esphome::zigbee {
47
49 zb_bool_t out_of_service;
50 zb_bool_t present_value;
51 zb_uint8_t status_flags;
52 zb_uchar_t description[ZB_ZCL_MAX_STRING_SIZE];
53};
54
56 zb_bool_t out_of_service;
58 zb_uint8_t status_flags;
59 zb_uint16_t engineering_units;
60 zb_uchar_t description[ZB_ZCL_MAX_STRING_SIZE];
61};
62
63class ZigbeeComponent : public Component {
64 public:
65 void setup() override;
66 void dump_config() override;
67 void add_callback(zb_uint8_t endpoint, std::function<void(zb_bufid_t bufid)> &&cb) {
68 // endpoints are enumerated from 1
69 this->callbacks_[endpoint - 1] = std::move(cb);
70 }
71 void add_join_callback(std::function<void()> &&cb) { this->join_cb_.add(std::move(cb)); }
72 void zboss_signal_handler_esphome(zb_bufid_t bufid);
73 void factory_reset();
75 void flush();
76 void loop() override;
77
78 protected:
79 static void zcl_device_cb(zb_bufid_t bufid);
80 void on_join_();
81#ifdef USE_ZIGBEE_WIPE_ON_BOOT
82 void erase_flash_(int area);
83#endif
84 std::array<std::function<void(zb_bufid_t bufid)>, ZIGBEE_ENDPOINTS_COUNT> callbacks_{};
87 bool need_flush_{false};
88};
89
91 public:
92 void set_parent(ZigbeeComponent *parent) { this->parent_ = parent; }
93 void set_endpoint(zb_uint8_t endpoint) { this->endpoint_ = endpoint; }
94
95 protected:
96 zb_uint8_t endpoint_{0};
98};
99
100} // namespace esphome::zigbee
101#endif
static void zcl_device_cb(zb_bufid_t bufid)
CallbackManager< void()> join_cb_
void add_join_callback(std::function< void()> &&cb)
std::array< std::function< void(zb_bufid_t bufid)>, ZIGBEE_ENDPOINTS_COUNT > callbacks_
void zboss_signal_handler_esphome(zb_bufid_t bufid)
void add_callback(zb_uint8_t endpoint, std::function< void(zb_bufid_t bufid)> &&cb)
void set_endpoint(zb_uint8_t endpoint)
void set_parent(ZigbeeComponent *parent)
zb_uchar_t description[ZB_ZCL_MAX_STRING_SIZE]
zb_uchar_t description[ZB_ZCL_MAX_STRING_SIZE]