ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
zigbee_attribute_esp32.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4#ifdef USE_ESP32
5#ifdef USE_ZIGBEE
6
7namespace esphome::zigbee {
8
9ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
10
12 if (!this->zb_->is_started()) {
13 return;
14 }
15 if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
16 ezb_zcl_status_t state = ezb_zcl_set_attr_value(this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_,
17 EZB_ZCL_STD_MANUF_CODE, this->value_p_, false);
18 // cleared before report_() so it can disable the loop
19 // when the report has to wait for join
20 this->set_attr_requested_ = false;
21 if (this->force_report_) {
22 this->report_(true);
23 }
24 // Check for error
25 if (state != EZB_ZCL_STATUS_SUCCESS) {
26 ESP_LOGE(TAG, "Setting attribute 0x%04X on cluster 0x%04X failed, ZCL status: %u", this->attr_id_,
27 this->cluster_id_, static_cast<unsigned>(state));
28 }
29 esp_zigbee_lock_release();
30 }
31}
32
33void ZigbeeAttribute::report_(bool has_lock) {
34 if (!this->report_enabled) {
35 return;
36 }
37 if (!this->zb_->is_joined()) {
38 this->report_requested_ = true;
39 if (!this->set_attr_requested_) {
40 this->disable_loop();
41 }
42 return;
43 }
44 if (has_lock or esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
45 ezb_zcl_report_attr_cmd_t cmd = {};
46 cmd.cmd_ctrl.fc.direction = EZB_ZCL_CMD_DIRECTION_TO_CLI;
47 cmd.cmd_ctrl.fc.dis_default_rsp = 1;
48 cmd.cmd_ctrl.dst_addr.addr_mode = EZB_ADDR_MODE_SHORT;
49 cmd.cmd_ctrl.dst_addr.u.short_addr = 0x0000;
50 cmd.cmd_ctrl.dst_ep = 1;
51 cmd.cmd_ctrl.src_ep = this->endpoint_id_;
52 cmd.cmd_ctrl.cluster_id = this->cluster_id_;
53 cmd.cmd_ctrl.fc.manuf_specific = 0;
54 cmd.payload.attr_id = this->attr_id_;
55
56 ezb_zcl_report_attr_cmd_req(&cmd);
57 this->report_requested_ = false;
58 if (!has_lock) {
59 esp_zigbee_lock_release();
60 }
61 }
62}
63
66 return;
67 }
68 this->report_enabled = true;
70 this->force_report_ = true;
71 }
72 this->zb_->add_on_join_callback([this](bool) {
73 if (this->report_requested_) {
74 this->enable_loop();
75 }
76 });
77}
78
80 if (!this->report_enabled) {
81 return;
82 }
83 this->report_requested_ = true;
84 this->enable_loop();
85}
86
88 if (this->set_attr_requested_) {
89 this->set_attr_();
90 }
91
92 if (this->report_requested_) {
93 this->report_(false);
94 }
95
96 if (!this->report_requested_ && !this->set_attr_requested_) {
97 this->disable_loop();
98 }
99}
100
101} // namespace esphome::zigbee
102
103#endif
104#endif
void enable_loop()
Enable this component's loop.
Definition component.h:248
void disable_loop()
Disable this component's loop.
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "zigbee.time")