ESPHome  2022.11.3
button.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace button {
9 
10 #define LOG_BUTTON(prefix, type, obj) \
11  if ((obj) != nullptr) { \
12  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
13  if (!(obj)->get_icon().empty()) { \
14  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
15  } \
16  }
17 
22 class Button : public EntityBase {
23  public:
24  explicit Button();
25  explicit Button(const std::string &name);
26 
31  void press();
32 
37  void add_on_press_callback(std::function<void()> &&callback);
38 
40  void set_device_class(const std::string &device_class);
41 
43  std::string get_device_class();
44 
45  protected:
48  virtual void press_action() = 0;
49 
51  std::string device_class_{};
52 };
53 
54 } // namespace button
55 } // namespace esphome
const char * name
Definition: stm32flash.h:78
Base class for all buttons.
Definition: button.h:22
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