ESPHome  2024.5.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 SUB_SWITCH(name) \
12  protected: \
13  switch_::Switch *name##_switch_{nullptr}; \
14 \
15  public: \
16  void set_##name##_switch(switch_::Switch *s) { this->name##_switch_ = s; }
17 
18 // bit0: on/off. bit1: persistent. bit2: inverted. bit3: disabled
19 const int RESTORE_MODE_ON_MASK = 0x01;
21 const int RESTORE_MODE_INVERTED_MASK = 0x04;
22 const int RESTORE_MODE_DISABLED_MASK = 0x08;
23 
28  SWITCH_RESTORE_DEFAULT_ON = RESTORE_MODE_PERSISTENT_MASK | RESTORE_MODE_ON_MASK,
30  SWITCH_RESTORE_INVERTED_DEFAULT_ON = RESTORE_MODE_PERSISTENT_MASK | RESTORE_MODE_INVERTED_MASK | RESTORE_MODE_ON_MASK,
32 };
33 
39 class Switch : public EntityBase, public EntityBase_DeviceClass {
40  public:
41  explicit Switch();
42 
50  void publish_state(bool state);
51 
53  bool state;
54 
57 
62  void turn_on();
67  void turn_off();
72  void toggle();
73 
83  void set_inverted(bool inverted);
84 
89  void add_on_state_callback(std::function<void(bool)> &&callback);
90 
95 
103 
109  virtual bool assumed_state();
110 
111  bool is_inverted() const;
112 
114 
115  protected:
124  virtual void write_state(bool state) = 0;
125 
127  bool inverted_{false};
130 };
131 
132 #define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
133 void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj);
134 
135 } // namespace switch_
136 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
CallbackManager< void(bool)> state_callback_
Definition: switch.h:126
void set_restore_mode(SwitchRestoreMode restore_mode)
Definition: switch.h:113
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
Definition: switch.cpp:66
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:58
virtual void write_state(bool state)=0
Write the given state to hardware.
Deduplicator< bool > publish_dedup_
Definition: switch.h:128
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted...
Definition: switch.cpp:23
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition: switch.cpp:33
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:60
const int RESTORE_MODE_ON_MASK
Definition: switch.h:19
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
uint8_t type
bool is_inverted() const
Definition: switch.cpp:64
SwitchRestoreMode restore_mode
Indicates whether or not state is to be retrieved from flash and how.
Definition: switch.h:56
void toggle()
Toggle this switch.
Definition: switch.cpp:19
const int RESTORE_MODE_INVERTED_MASK
Definition: switch.h:21
void set_inverted(bool inverted)
Set whether the state should be treated as inverted.
Definition: switch.cpp:63
const int RESTORE_MODE_PERSISTENT_MASK
Definition: switch.h:20
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
ESPPreferenceObject rtc_
Definition: switch.h:129
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
const int RESTORE_MODE_DISABLED_MASK
Definition: switch.h:22
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
void turn_off()
Turn this switch off.
Definition: switch.cpp:15