ESPHome 2026.1.0-dev
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::button {
5
6static const char *const TAG = "button";
7
8// Function implementation of LOG_BUTTON macro to reduce code size
9void log_button(const char *tag, const char *prefix, const char *type, Button *obj) {
10 if (obj == nullptr) {
11 return;
12 }
13
14 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
15
16 if (!obj->get_icon_ref().empty()) {
17 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
18 }
19}
20
22 ESP_LOGD(TAG, "'%s' Pressed.", this->get_name().c_str());
23 this->press_action();
24 this->press_callback_.call();
25}
26void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
27
28} // namespace esphome::button
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:85
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:25
LazyCallbackManager< void()> press_callback_
Definition button.h:44
void add_on_press_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition button.cpp:26
void press()
Press this button.
Definition button.cpp:21
virtual void press_action()=0
You should implement this virtual method if you want to create your own button.
uint16_t type
void log_button(const char *tag, const char *prefix, const char *type, Button *obj)
Definition button.cpp:9