ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
zigbee_attribute_esp32.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <limits>
5#include <type_traits>
6
9
10#ifdef USE_ESP32
11#ifdef USE_ZIGBEE
12
13#include "esp_zigbee.h"
14#include "zigbee_esp32.h"
15
16#ifdef USE_SENSOR
18#endif
19#ifdef USE_BINARY_SENSOR
21#endif
22#ifdef USE_SWITCH
24#endif
25
26namespace esphome::zigbee {
27
29 public:
30 ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
31 uint8_t attr_type, float scale, uint8_t max_size)
32 : ZigbeeAttributeBase(parent, endpoint_id, cluster_id, role, attr_id, attr_type, max_size), scale_(scale) {}
33
34 template<typename F> void add_on_value_callback(F &&callback) { on_value_callback_.add(std::forward<F>(callback)); }
35 void on_value(ezb_zcl_attribute_t attribute) override {
36 if (attribute.data.type == this->attr_type() && attribute.data.value) {
37 this->on_value_callback_.call(attribute);
38 }
39 }
40
41#ifdef USE_SENSOR
42 template<typename T> void connect(sensor::Sensor *sensor);
43 template<typename T, typename F> void connect(sensor::Sensor *sensor, F &&f);
44#endif
45#ifdef USE_BINARY_SENSOR
46 template<typename T> void connect(binary_sensor::BinarySensor *sensor);
47#endif
48#ifdef USE_SWITCH
49 template<typename T> void connect(switch_::Switch *device);
50#endif
51 protected:
52 float scale_;
53 LazyCallbackManager<void(ezb_zcl_attribute_t attribute)> on_value_callback_{};
54};
55
56#ifdef USE_SENSOR
57template<typename T> void ZigbeeAttribute::connect(sensor::Sensor *sensor) {
58 sensor->add_on_state_callback([this](float value) { this->set_attr(this->scale_value_<T>(value, this->scale_)); });
59}
60template<typename T, typename F> void ZigbeeAttribute::connect(sensor::Sensor *sensor, F &&f) {
61 sensor->add_on_state_callback([f = std::forward<F>(f), this](float value) { this->set_attr((T) f(value)); });
62}
63#endif
64#ifdef USE_BINARY_SENSOR
65template<typename T> void ZigbeeAttribute::connect(binary_sensor::BinarySensor *sensor) {
66 sensor->add_on_state_callback([this](bool value) { this->set_attr((T) (this->scale_ * value)); });
67}
68#endif
69#ifdef USE_SWITCH
70template<typename T> void ZigbeeAttribute::connect(switch_::Switch *device) {
72 [device](ezb_zcl_attribute_t attribute) { device->control(*(T *) attribute.data.value); });
73 device->add_on_state_callback([this](bool value) { this->set_attr((T) (this->scale_ * value)); });
74}
75#endif
76
77} // namespace esphome::zigbee
78
79#endif
80#endif
void add_on_state_callback(F &&callback)
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:48
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:121
Base class for all switches.
Definition switch.h:43
void add_on_state_callback(F &&callback)
Set callback for state changes.
Definition switch.h:101
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:12
LazyCallbackManager< void(ezb_zcl_attribute_t attribute)> on_value_callback_
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 on_value(ezb_zcl_attribute_t attribute) override
uint32_t value
Definition hal.cpp:59