20 ESP_LOGV(TAG,
"'%s' Turning ON.", LOG_STR_ARG(this->
get_log_name()));
24 ESP_LOGV(TAG,
"'%s' Turning OFF.", LOG_STR_ARG(this->
get_log_name()));
28 ESP_LOGV(TAG,
"'%s' Toggling %s.", LOG_STR_ARG(this->
get_log_name()),
29 this->
state ? LOG_STR_LITERAL(
"OFF") : LOG_STR_LITERAL(
"ON"));
36#ifdef USE_SWITCH_STORAGE
45 if (!this->
rtc_.
load(&initial_state))
56 if (restored_state.has_value()) {
84 ESP_LOGV(TAG,
"'%s' >> %s", LOG_STR_ARG(this->
get_log_name()), ONOFF(this->state));
86#if defined(USE_SWITCH) && defined(USE_CONTROLLER_REGISTRY)
87 ControllerRegistry::notify_switch_update(
this);
95 const LogString *onoff = LOG_STR(
""), *inverted = onoff, *restore;
97 restore = LOG_STR(
"disabled");
107 "%s Restore Mode: %s%s %s",
108 prefix,
type, LOG_STR_ARG(obj->
get_log_name()), prefix, LOG_STR_ARG(inverted), LOG_STR_ARG(restore),
112 LOG_ENTITY_ICON(tag, prefix, *obj);
114 ESP_LOGCONFIG(tag,
"%s Assumed State: YES", prefix);
117 ESP_LOGCONFIG(tag,
"%s Inverted: YES", prefix);
119 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void set_has_state(bool state)
Base class for all switches.
void toggle()
Toggle this switch.
LazyCallbackManager< void(bool)> state_callback_
optional< bool > get_initial_state()
Returns the initial state of the switch, as persisted previously, or empty if never persisted.
void turn_on()
Turn this switch on.
void turn_off()
Turn this switch off.
bool state
The current reported state of the binary sensor.
optional< bool > get_initial_write_state()
get_initial_state_with_restore_mode() with inverted applied: the level write_state() takes.
virtual void write_state(bool state)=0
Write the given state to hardware.
SwitchRestoreMode restore_mode
Indicates whether or not state is to be retrieved from flash and how.
void restore()
Drive the switch to the state its restore mode yields, through control(), so inversion and write_stat...
void publish_state(bool state)
Publish a state to the front-end from the back-end.
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Deduplicator< bool > publish_dedup_
void control(bool target_state)
Control this switch using a boolean state value.
constexpr int RESTORE_MODE_ON_MASK
constexpr int RESTORE_MODE_STORAGE_MASK
constexpr int RESTORE_MODE_INVERTED_MASK
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj)
constexpr int RESTORE_MODE_STORAGE_FLASH_MASK
constexpr int RESTORE_MODE_PERSISTENT_MASK
ESPHOME_LOG_TAG(TAG, "switch")
constexpr int RESTORE_MODE_DISABLED_MASK