ESPHome  2022.9.1
button.cpp
Go to the documentation of this file.
1 #include "button.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace button {
6 
7 static const char *const TAG = "button";
8 
9 Button::Button(const std::string &name) : EntityBase(name) {}
11 
12 void Button::press() {
13  ESP_LOGD(TAG, "'%s' Pressed.", this->get_name().c_str());
14  this->press_action();
15  this->press_callback_.call();
16 }
17 void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
18 
19 void Button::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
20 std::string Button::get_device_class() { return this->device_class_; }
21 
22 } // namespace button
23 } // namespace esphome
const char * name
Definition: stm32flash.h:78
Base class for all buttons.
Definition: button.h:22
const std::string & get_name() const
Definition: entity_base.cpp:11
std::string device_class_
Definition: button.h:51
std::string get_device_class()
Get the device class for this button.
Definition: button.cpp:20
void press()
Press this button.
Definition: button.cpp:12
void add_on_press_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition: button.cpp:17
virtual void press_action()=0
You should implement this virtual method if you want to create your own button.
Definition: a4988.cpp:4
void set_device_class(const std::string &device_class)
Set the Home Assistant device class (see button::device_class).
Definition: button.cpp:19
CallbackManager< void()> press_callback_
Definition: button.h:50