ESPHome
2026.1.0-dev
Loading...
Searching...
No Matches
esphome
components
button
button.cpp
Go to the documentation of this file.
1
#include "
button.h
"
2
#include "
esphome/core/log.h
"
3
4
namespace
esphome::button
{
5
6
static
const
char
*
const
TAG =
"button"
;
7
8
// Function implementation of LOG_BUTTON macro to reduce code size
9
void
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
21
void
Button::press
() {
22
ESP_LOGD(TAG,
"'%s' Pressed."
, this->
get_name
().c_str());
23
this->
press_action
();
24
this->
press_callback_
.call();
25
}
26
void
Button::add_on_press_callback
(std::function<
void
()> &&callback) { this->
press_callback_
.add(std::move(callback)); }
27
28
}
// namespace esphome::button
button.h
esphome::EntityBase::get_name
const StringRef & get_name() const
Definition
entity_base.cpp:11
esphome::EntityBase::get_icon_ref
StringRef get_icon_ref() const
Definition
entity_base.h:85
esphome::StringRef::c_str
constexpr const char * c_str() const
Definition
string_ref.h:69
esphome::StringRef::empty
constexpr bool empty() const
Definition
string_ref.h:71
esphome::button::Button
Base class for all buttons.
Definition
button.h:25
esphome::button::Button::press_callback_
LazyCallbackManager< void()> press_callback_
Definition
button.h:44
esphome::button::Button::add_on_press_callback
void add_on_press_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition
button.cpp:26
esphome::button::Button::press
void press()
Press this button.
Definition
button.cpp:21
esphome::button::Button::press_action
virtual void press_action()=0
You should implement this virtual method if you want to create your own button.
type
uint16_t type
Definition
dns_server_esp32_idf.cpp:0
log.h
esphome::button
Definition
automation.h:7
esphome::button::log_button
void log_button(const char *tag, const char *prefix, const char *type, Button *obj)
Definition
button.cpp:9
Generated by
1.12.0