ESPHome  2022.11.4
switch.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 namespace switch_ {
10 
11 #define LOG_SWITCH(prefix, type, obj) \
12  if ((obj) != nullptr) { \
13  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
14  if (!(obj)->get_icon().empty()) { \
15  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
16  } \
17  if ((obj)->assumed_state()) { \
18  ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
19  } \
20  if ((obj)->is_inverted()) { \
21  ESP_LOGCONFIG(TAG, "%s Inverted: YES", prefix); \
22  } \
23  if (!(obj)->get_device_class().empty()) { \
24  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
25  } \
26  }
27 
33 class Switch : public EntityBase {
34  public:
35  explicit Switch();
36  explicit Switch(const std::string &name);
37 
45  void publish_state(bool state);
46 
48  bool state;
49 
54  void turn_on();
59  void turn_off();
64  void toggle();
65 
75  void set_inverted(bool inverted);
76 
81  void add_on_state_callback(std::function<void(bool)> &&callback);
82 
84 
90  virtual bool assumed_state();
91 
92  bool is_inverted() const;
93 
95  std::string get_device_class();
97  void set_device_class(const std::string &device_class);
98 
99  protected:
108  virtual void write_state(bool state) = 0;
109 
111  bool inverted_{false};
115 };
116 
117 } // namespace switch_
118 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
CallbackManager< void(bool)> state_callback_
Definition: switch.h:110
const char * name
Definition: stm32flash.h:78
optional< std::string > device_class_
Definition: switch.h:114
void set_device_class(const std::string &device_class)
Set the Home Assistant device class for this switch.
Definition: switch.cpp:53
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:40
virtual void write_state(bool state)=0
Write the given state to hardware.
Deduplicator< bool > publish_dedup_
Definition: switch.h:112
std::string get_device_class()
Get the device class for this switch.
Definition: switch.cpp:48
optional< bool > get_initial_state()
Definition: switch.cpp:24
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:42
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
bool is_inverted() const
Definition: switch.cpp:46
void toggle()
Toggle this switch.
Definition: switch.cpp:20
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition: switch.cpp:45
Definition: a4988.cpp:4
ESPPreferenceObject rtc_
Definition: switch.h:113
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:31
bool state
The current reported state of the binary sensor.
Definition: switch.h:48
void turn_off()
Turn this switch off.
Definition: switch.cpp:16