ESPHome 2025.9.0
Loading...
Searching...
No Matches
button.cpp
Go to the documentation of this file.
1#include "button.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace button {
6
7static const char *const TAG = "button";
8
9// Function implementation of LOG_BUTTON macro to reduce code size
10void log_button(const char *tag, const char *prefix, const char *type, Button *obj) {
11 if (obj == nullptr) {
12 return;
13 }
14
15 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
16
17 if (!obj->get_icon_ref().empty()) {
18 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
19 }
20}
21
23 ESP_LOGD(TAG, "'%s' Pressed.", this->get_name().c_str());
24 this->press_action();
25 this->press_callback_.call();
26}
27void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
28
29} // namespace button
30} // namespace esphome
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:62
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
Base class for all buttons.
Definition button.h:26
void add_on_press_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition button.cpp:27
CallbackManager< void()> press_callback_
Definition button.h:45
void press()
Press this button.
Definition button.cpp:22
virtual void press_action()=0
You should implement this virtual method if you want to create your own button.
uint8_t type
void log_button(const char *tag, const char *prefix, const char *type, Button *obj)
Definition button.cpp:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7