ESPHome  2024.11.0
switch.cpp
Go to the documentation of this file.
1 #include "switch.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace switch_ {
6 
7 static const char *const TAG = "switch";
8 
9 Switch::Switch() : state(false) {}
10 
12  ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
13  this->write_state(!this->inverted_);
14 }
16  ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
17  this->write_state(this->inverted_);
18 }
20  ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
21  this->write_state(this->inverted_ == this->state);
22 }
25  return {};
26 
28  bool initial_state;
29  if (!this->rtc_.load(&initial_state))
30  return {};
31  return initial_state;
32 }
35  return {};
36  }
37  bool initial_state = restore_mode & RESTORE_MODE_ON_MASK; // default value *_OFF or *_ON
38  if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) { // For RESTORE_*
39  optional<bool> restored_state = this->get_initial_state();
40  if (restored_state.has_value()) {
41  // Invert value if any of the *_INVERTED_* modes
42  initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
43  }
44  }
45  return initial_state;
46 }
48  if (!this->publish_dedup_.next(state))
49  return;
50  this->state = state != this->inverted_;
51 
53  this->rtc_.save(&this->state);
54 
55  ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
56  this->state_callback_.call(this->state);
57 }
58 bool Switch::assumed_state() { return false; }
59 
60 void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
61  this->state_callback_.add(std::move(callback));
62 }
63 void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
64 bool Switch::is_inverted() const { return this->inverted_; }
65 
66 void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
67  if (obj != nullptr) {
68  ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
69  if (!obj->get_icon().empty()) {
70  ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon().c_str());
71  }
72  if (obj->assumed_state()) {
73  ESP_LOGCONFIG(tag, "%s Assumed State: YES", prefix);
74  }
75  if (obj->is_inverted()) {
76  ESP_LOGCONFIG(tag, "%s Inverted: YES", prefix);
77  }
78  if (!obj->get_device_class().empty()) {
79  ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class().c_str());
80  }
81  const LogString *onoff = LOG_STR(""), *inverted = onoff, *restore;
83  restore = LOG_STR("disabled");
84  } else {
85  onoff = obj->restore_mode & RESTORE_MODE_ON_MASK ? LOG_STR("ON") : LOG_STR("OFF");
86  inverted = obj->restore_mode & RESTORE_MODE_INVERTED_MASK ? LOG_STR("inverted ") : LOG_STR("");
87  restore = obj->restore_mode & RESTORE_MODE_PERSISTENT_MASK ? LOG_STR("restore defaults to") : LOG_STR("always");
88  }
89 
90  ESP_LOGCONFIG(tag, "%s Restore Mode: %s%s %s", prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
91  LOG_STR_ARG(onoff));
92  }
93 }
94 
95 } // namespace switch_
96 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
value_type const & value() const
Definition: optional.h:89
CallbackManager< void(bool)> state_callback_
Definition: switch.h:126
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:504
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
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.
bool has_value() const
Definition: optional.h:87
Deduplicator< bool > publish_dedup_
Definition: switch.h:128
bool save(const T *src)
Definition: preferences.h:21
const char *const TAG
Definition: spi.cpp:8
std::string get_icon() const
Definition: entity_base.cpp:30
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
ESPPreferences * global_preferences
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
constexpr const char * c_str() const
Definition: string_ref.h:68
const int RESTORE_MODE_PERSISTENT_MASK
Definition: switch.h:20
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
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
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:15