ESPHome  2022.11.3
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(const std::string &name) : EntityBase(name), state(false) {}
11 
13  ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
14  this->write_state(!this->inverted_);
15 }
17  ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
18  this->write_state(this->inverted_);
19 }
21  ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
22  this->write_state(this->inverted_ == this->state);
23 }
26  bool initial_state;
27  if (!this->rtc_.load(&initial_state))
28  return {};
29  return initial_state;
30 }
32  if (!this->publish_dedup_.next(state))
33  return;
34  this->state = state != this->inverted_;
35 
36  this->rtc_.save(&this->state);
37  ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
38  this->state_callback_.call(this->state);
39 }
40 bool Switch::assumed_state() { return false; }
41 
42 void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
43  this->state_callback_.add(std::move(callback));
44 }
45 void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
46 bool Switch::is_inverted() const { return this->inverted_; }
47 
48 std::string Switch::get_device_class() {
49  if (this->device_class_.has_value())
50  return *this->device_class_;
51  return "";
52 }
53 void Switch::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
54 
55 } // namespace switch_
56 } // 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
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:482
std::string name_
Definition: entity_base.h:54
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.
const std::string & get_name() const
Definition: entity_base.cpp:11
bool has_value() const
Definition: optional.h:87
Deduplicator< bool > publish_dedup_
Definition: switch.h:112
std::string get_device_class()
Get the device class for this switch.
Definition: switch.cpp:48
bool save(const T *src)
Definition: preferences.h:21
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
ESPPreferences * global_preferences
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
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
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
uint32_t get_object_id_hash()
Definition: entity_base.cpp:42
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:16