ESPHome 2026.3.0-dev
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
68
69class ZigbeeComponent : public Component {
70 public:
71 void setup() override;
72 void dump_config() override;
73 void add_callback(zb_uint8_t endpoint, std::function<void(zb_bufid_t bufid)> &&cb) {
74 // endpoints are enumerated from 1
75 this->callbacks_[endpoint - 1] = std::move(cb);
76 }
77 void add_join_callback(std::function<void()> &&cb) { this->join_cb_.add(std::move(cb)); }
78 void zboss_signal_handler_esphome(zb_bufid_t bufid);
79 void factory_reset();
81 void force_report();
82 void loop() override;
83
84 protected:
85 static void zcl_device_cb(zb_bufid_t bufid);
86 void on_join_();
87#ifdef USE_ZIGBEE_WIPE_ON_BOOT
88 void erase_flash_(int area);
89#endif
90 void dump_reporting_();
91 std::array<std::function<void(zb_bufid_t bufid)>, ZIGBEE_ENDPOINTS_COUNT> callbacks_{};
94 bool force_report_{false};
95 uint32_t sleep_time_{};
96 uint32_t sleep_remainder_{};
97};
98
100 public:
101 void set_parent(ZigbeeComponent *parent) { this->parent_ = parent; }
102 void set_endpoint(zb_uint8_t endpoint) { this->endpoint_ = endpoint; }
103
104 protected:
105 zb_uint8_t endpoint_{0};
107};
108
109} // namespace esphome::zigbee
110#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]