Skip to content

Commit

Permalink
always set up switch rtc_
Browse files Browse the repository at this point in the history
  • Loading branch information
KaufHA committed Dec 8, 2022
1 parent 15674b2 commit 7e32f3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
38 changes: 16 additions & 22 deletions components/switch/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ void Switch::toggle() {
this->write_state(this->inverted_ == this->state);
}
optional<bool> Switch::get_initial_state() {
if (!(restore_mode & RESTORE_MODE_PERSISTENT_MASK))
return {};

// always set up rtc_ in case mode changes later to one that saves
if ( this->has_global_forced_addr ) { id(global_forced_addr) = this->forced_addr; }
if ( this->has_forced_hash ) {
this->rtc_ = global_preferences->make_preference<bool>(this->forced_hash);
} else {
this->rtc_ = global_preferences->make_preference<bool>(this->get_object_id_hash());
}

// don't actually try to load if not in a restoring mode
if (!(restore_mode & RESTORE_MODE_PERSISTENT_MASK))
return {};

bool initial_state;
if (!this->rtc_.load(&initial_state))
return {};
Expand All @@ -42,17 +44,20 @@ optional<bool> Switch::get_initial_state_with_restore_mode() {
return {};
}
bool initial_state = restore_mode & RESTORE_MODE_ON_MASK; // default value *_OFF or *_ON
if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) { // For RESTORE_*
optional<bool> restored_state = this->get_initial_state();
if (restored_state.has_value()) {
// Invert value if any of the *_INVERTED_* modes
initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
}

// always call get_initial_state so that rtc_ is set up.
optional<bool> restored_state = this->get_initial_state();

// don't use restored state unless in a persistent mode
if (restored_state.has_value() && (restore_mode & RESTORE_MODE_PERSISTENT_MASK)) {
// Invert value if any of the *_INVERTED_* modes
initial_state = restore_mode & RESTORE_MODE_INVERTED_MASK ? !restored_state.value() : restored_state.value();
}
return initial_state;
}
void Switch::publish_state(bool state) {
if (!this->publish_dedup_.next(state))

void Switch::publish_state(bool state, bool force_save) {
if (!force_save && !this->publish_dedup_.next(state))
return;
this->state = state != this->inverted_;

Expand All @@ -63,17 +68,6 @@ void Switch::publish_state(bool state) {
this->state_callback_.call(this->state);
}

void Switch::force_save(bool state) {
this->state = state != this->inverted_;

if (restore_mode & RESTORE_MODE_PERSISTENT_MASK) {
this->rtc_.save(&this->state);
ESP_LOGD(TAG, "'%s': Force saving state %s", this->name_.c_str(), ONOFF(this->state));
} else {
ESP_LOGD(TAG, "'%s': NOT force saving state %s", this->name_.c_str(), ONOFF(this->state));
}
}

bool Switch::assumed_state() { return false; }

void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
Expand Down
4 changes: 1 addition & 3 deletions components/switch/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class Switch : public EntityBase {
*
* @param state The new state.
*/
void publish_state(bool state);

void force_save(bool state);
void publish_state(bool state, bool force_save = false);

/// The current reported state of the binary sensor.
bool state;
Expand Down

0 comments on commit 7e32f3b

Please sign in to comment.