ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
zigbee_attribute_esp32.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
8
9#ifdef USE_ESP32
10#ifdef USE_ZIGBEE
11
12#include "esp_zigbee_core.h"
13#include "zigbee_esp32.h"
14
15#ifdef USE_SENSOR
17#endif
18#ifdef USE_BINARY_SENSOR
20#endif
21
22namespace esphome::zigbee {
23
29
30class ZigbeeAttribute : public Component {
31 public:
32 ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
33 uint8_t attr_type, float scale, uint8_t max_size)
34 : zb_(parent),
35 endpoint_id_(endpoint_id),
36 cluster_id_(cluster_id),
37 role_(role),
38 attr_id_(attr_id),
40 max_size_(max_size),
41 scale_(scale) {}
42 void loop() override;
43 template<typename T> void add_attr(T value);
44 esp_zb_zcl_reporting_info_t get_reporting_info();
45 template<typename T> void set_attr(const T &value);
46 uint8_t attr_type() { return attr_type_; }
47 void set_report(bool force);
48#ifdef USE_SENSOR
49 template<typename T> void connect(sensor::Sensor *sensor);
50#endif
51#ifdef USE_BINARY_SENSOR
52 template<typename T> void connect(binary_sensor::BinarySensor *sensor);
53#endif
54 bool report_enabled = false;
55
56 protected:
57 void set_attr_();
58 void report_(bool has_lock);
60 uint8_t endpoint_id_;
61 uint16_t cluster_id_;
62 uint8_t role_;
63 uint16_t attr_id_;
64 uint8_t attr_type_;
65 uint8_t max_size_;
66 float scale_;
67 void *value_p_{nullptr};
69 bool force_report_{false};
70};
71
72template<typename T> void ZigbeeAttribute::add_attr(T value) {
73 // Attribute type does never change and add_attr is only called once during startup, so this is safe.
74 // For now we need to support only simple numeric/bool types for (binary) sensors.
75 // For strings and arrays we would need to allocate a buffer of the maximum size.
76 this->value_p_ = (void *) (new T);
77 this->zb_->add_attr(this, this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_, this->max_size_,
78 std::move(value));
79}
80
81template<typename T> void ZigbeeAttribute::set_attr(const T &value) {
82 *static_cast<T *>(this->value_p_) = value;
83 this->set_attr_requested_ = true;
84 this->enable_loop();
85}
86
87#ifdef USE_SENSOR
88template<typename T> void ZigbeeAttribute::connect(sensor::Sensor *sensor) {
89 sensor->add_on_state_callback([this](float value) { this->set_attr((T) (this->scale_ * value)); });
90}
91#endif
92#ifdef USE_BINARY_SENSOR
93template<typename T> void ZigbeeAttribute::connect(binary_sensor::BinarySensor *sensor) {
94 sensor->add_on_state_callback([this](bool value) { this->set_attr((T) (this->scale_ * value)); });
95}
96#endif
97
98} // namespace esphome::zigbee
99
100#endif
101#endif
void enable_loop()
Enable this component's loop.
Definition component.h:258
void add_on_state_callback(F &&callback)
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:47
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
esp_zb_zcl_reporting_info_t get_reporting_info()
void connect(sensor::Sensor *sensor)
ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t attr_type, float scale, uint8_t max_size)
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)