ESPHome  2023.3.1
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 // bit0: on/off. bit1: persistent. bit2: inverted. bit3: disabled
12 const int RESTORE_MODE_ON_MASK = 0x01;
14 const int RESTORE_MODE_INVERTED_MASK = 0x04;
15 const int RESTORE_MODE_DISABLED_MASK = 0x08;
16 
21  SWITCH_RESTORE_DEFAULT_ON = RESTORE_MODE_PERSISTENT_MASK | RESTORE_MODE_ON_MASK,
23  SWITCH_RESTORE_INVERTED_DEFAULT_ON = RESTORE_MODE_PERSISTENT_MASK | RESTORE_MODE_INVERTED_MASK | RESTORE_MODE_ON_MASK,
25 };
26 
32 class Switch : public EntityBase {
33  public:
34  explicit Switch();
35  explicit Switch(const std::string &name);
36 
44  void publish_state(bool state);
45 
47  bool state;
48 
51 
56  void turn_on();
61  void turn_off();
66  void toggle();
67 
77  void set_inverted(bool inverted);
78 
83  void add_on_state_callback(std::function<void(bool)> &&callback);
84 
89 
97 
103  virtual bool assumed_state();
104 
105  bool is_inverted() const;
106 
108  std::string get_device_class();
110  void set_device_class(const std::string &device_class);
112 
113  protected:
122  virtual void write_state(bool state) = 0;
123 
125  bool inverted_{false};
129 };
130 
131 #define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
132 void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
133 
134 } // namespace switch_
135 } // namespace esphome
Base class for all switches.
Definition: switch.h:32
CallbackManager< void(bool)> state_callback_
Definition: switch.h:124
const char * name
Definition: stm32flash.h:78
optional< std::string > device_class_
Definition: switch.h:128
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition: switch.h:111
void set_device_class(const std::string &device_class)
Set the Home Assistant device class for this switch.
Definition: switch.cpp:72
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
Definition: switch.cpp:74
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:59
virtual void write_state(bool state)=0
Write the given state to hardware.
Deduplicator< bool > publish_dedup_
Definition: switch.h:126
std::string get_device_class()
Get the device class for this switch.
Definition: switch.cpp:67
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted...
Definition: switch.cpp:24
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition: switch.cpp:34
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:61
const int RESTORE_MODE_ON_MASK
Definition: switch.h:12
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
uint8_t type
bool is_inverted() const
Definition: switch.cpp:65
SwitchRestoreMode restore_mode
Indicates whether or not state is to be retrieved from flash and how.
Definition: switch.h:50
void toggle()
Toggle this switch.
Definition: switch.cpp:20
const int RESTORE_MODE_INVERTED_MASK
Definition: switch.h:14
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition: switch.cpp:64
const int RESTORE_MODE_PERSISTENT_MASK
Definition: switch.h:13
Definition: a4988.cpp:4
ESPPreferenceObject rtc_
Definition: switch.h:127
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:48
const int RESTORE_MODE_DISABLED_MASK
Definition: switch.h:15
bool state
The current reported state of the binary sensor.
Definition: switch.h:47
void turn_off()
Turn this switch off.
Definition: switch.cpp:16