ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
zigbee_number_esp32.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4#if defined(USE_ESP32) && defined(USE_ZIGBEE) && defined(USE_NUMBER)
5
6namespace esphome::zigbee {
7
8ESPHOME_LOG_TAG(TAG, "zigbee.number");
9
10namespace {
11
12class FlagGuard {
13 public:
14 explicit FlagGuard(bool &flag) : flag_(flag) { this->flag_ = true; }
15 ~FlagGuard() { this->flag_ = false; }
16 FlagGuard(const FlagGuard &) = delete;
17 FlagGuard &operator=(const FlagGuard &) = delete;
18
19 private:
20 bool &flag_;
21};
22
23} // namespace
24
26 const bool has_state = this->number_->has_state();
27 this->add_attr(0, has_state ? this->number_->state : NAN, &this->value_);
28 if (this->force_report_ && has_state) {
29 this->report();
30 }
31
32 // setup static attributes
33 this->zb_->add_attr(this->endpoint_id_, this->cluster_id_, this->role_,
34 EZB_ZCL_ATTR_ANALOG_OUTPUT_MIN_PRESENT_VALUE_ID, 0, EZB_ZCL_ATTR_TYPE_SINGLE, 0,
36 this->zb_->add_attr(this->endpoint_id_, this->cluster_id_, this->role_,
37 EZB_ZCL_ATTR_ANALOG_OUTPUT_MAX_PRESENT_VALUE_ID, 0, EZB_ZCL_ATTR_TYPE_SINGLE, 0,
39 this->zb_->add_attr(this->endpoint_id_, this->cluster_id_, this->role_, EZB_ZCL_ATTR_ANALOG_OUTPUT_RESOLUTION_ID, 0,
40 EZB_ZCL_ATTR_TYPE_SINGLE, 0, this->number_->traits.get_step());
41
42 // connect callbacks
43 this->number_->add_on_state_callback([this](float value) {
44 if (this->synchronizing_from_zigbee_ && value == this->value_) {
45 // The attribute already holds it; only a forced report is still owed
46 if (this->force_report_)
47 this->report();
48 return;
49 }
50 this->set_attr(value);
51 });
52}
53
54void ZigbeeNumber::on_value(ezb_zcl_attribute_t attribute) {
55 if (attribute.data.type == this->attr_type() && attribute.data.value) {
56 const float incoming_value = *static_cast<float *>(attribute.data.value);
57 this->value_ = incoming_value;
58 this->set_attr_requested_ = false;
59 auto call = this->number_->make_call();
60 call.set_value(incoming_value);
61 FlagGuard guard(this->synchronizing_from_zigbee_);
62 call.perform();
63 }
64}
65
66} // namespace esphome::zigbee
67
68#endif
bool has_state() const
NumberCall & set_value(float value)
NumberCall make_call()
Definition number.h:35
void add_on_state_callback(F &&callback)
Definition number.h:37
NumberTraits traits
Definition number.h:41
void add_attr(uint8_t attr_access, T value)
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)
void on_value(ezb_zcl_attribute_t attribute) override
ESPHOME_LOG_TAG(TAG, "zigbee.time")
uint32_t value
Definition hal.cpp:59