diff --git a/components/haier/haier_base.cpp b/components/haier/haier_base.cpp index 21c7d83..32f3525 100644 --- a/components/haier/haier_base.cpp +++ b/components/haier/haier_base.cpp @@ -125,6 +125,13 @@ haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() { } #endif +void HaierClimateBase::save_settings() { + HaierBaseSettings settings { this->get_health_mode(), this->get_display_state() }; + if (!this->base_rtc_.save(&settings)) { + ESP_LOGW(TAG, "Failed to save settings"); + } +} + bool HaierClimateBase::get_display_state() const { return (this->display_status_ == SwitchState::ON) || (this->display_status_ == SwitchState::PENDING_ON); } @@ -133,6 +140,7 @@ void HaierClimateBase::set_display_state(bool state) { if (state != this->get_display_state()) { this->display_status_ = state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; this->force_send_control_ = true; + this->save_settings(); } } @@ -144,6 +152,7 @@ void HaierClimateBase::set_health_mode(bool state) { if (state != this->get_health_mode()) { this->health_mode_ = state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; this->force_send_control_ = true; + this->save_settings(); } } @@ -339,6 +348,25 @@ bool HaierClimateBase::prepare_pending_action() { ClimateTraits HaierClimateBase::traits() { return traits_; } +void HaierClimateBase::initialization() { + constexpr uint32_t restore_settings_version = 0xA77D21EF; + this->base_rtc_ = global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); + HaierBaseSettings recovered; + if (!this->base_rtc_.load(&recovered)) { + recovered = { false, true }; + } + this->display_status_= recovered.display_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; + this->health_mode_ = recovered.health_mode ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; +#ifdef USE_SWITCH + if (this->display_switch_ != nullptr) { + this->display_switch_->publish_state(this->get_display_state()); + } + if (this->health_mode_switch_ != nullptr) { + this->health_mode_switch_->publish_state(this->get_health_mode()); + } +#endif +} + void HaierClimateBase::control(const ClimateCall &call) { ESP_LOGD("Control", "Control call"); if (!this->valid_connection()) { diff --git a/components/haier/haier_base.h b/components/haier/haier_base.h index dccefd7..bc2b893 100644 --- a/components/haier/haier_base.h +++ b/components/haier/haier_base.h @@ -24,6 +24,11 @@ enum class ActionRequest : uint8_t { START_STERI_CLEAN = 5, // only hOn }; +struct HaierBaseSettings { + bool health_mode; + bool display_state; +}; + class HaierClimateBase : public esphome::Component, public esphome::climate::Climate, public esphome::uart::UARTDevice, @@ -95,7 +100,8 @@ class HaierClimateBase : public esphome::Component, virtual void process_phase(std::chrono::steady_clock::time_point now) = 0; virtual haier_protocol::HaierMessage get_control_message() = 0; // NOLINT(readability-identifier-naming) virtual haier_protocol::HaierMessage get_power_message(bool state) = 0; // NOLINT(readability-identifier-naming) - virtual void initialization(){}; + virtual void save_settings(); + virtual void initialization(); virtual bool prepare_pending_action(); virtual void process_protocol_reset(); esphome::climate::ClimateTraits traits() override; @@ -167,6 +173,7 @@ class HaierClimateBase : public esphome::Component, std::chrono::steady_clock::time_point last_status_request_; // To request AC status std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level CallbackManager status_message_callback_{}; + ESPPreferenceObject base_rtc_; }; class StatusMessageTrigger : public Trigger { diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index ba0809f..e2145ba 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -38,7 +38,7 @@ void HonClimate::set_beeper_state(bool state) { #ifdef USE_SWITCH this->beeper_switch_->publish_state(state); #endif - this->rtc_.save(&this->settings_); + this->hon_rtc_.save(&this->settings_); } } @@ -483,10 +483,11 @@ haier_protocol::HaierMessage HonClimate::get_power_message(bool state) { } void HonClimate::initialization() { + HaierClimateBase::initialization(); constexpr uint32_t restore_settings_version = 0x2A3613DCUL; - this->rtc_ = global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); + this->hon_rtc_ = global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); HonSettings recovered; - if (this->rtc_.load(&recovered)) { + if (this->hon_rtc_.load(&recovered)) { this->settings_ = recovered; } else { this->settings_ = {hon_protocol::VerticalSwingMode::CENTER, hon_protocol::HorizontalSwingMode::CENTER, true}; @@ -1016,7 +1017,7 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * if (save_settings) { this->settings_.last_vertiacal_swing = this->current_vertical_swing_.value(); this->settings_.last_horizontal_swing = this->current_horizontal_swing_.value(); - this->rtc_.save(&this->settings_); + this->hon_rtc_.save(&this->settings_); } should_publish = should_publish || (old_swing_mode != this->swing_mode); } diff --git a/components/haier/hon_climate.h b/components/haier/hon_climate.h index ac87903..a8125b3 100644 --- a/components/haier/hon_climate.h +++ b/components/haier/hon_climate.h @@ -185,7 +185,7 @@ class HonClimate : public HaierClimateBase { esphome::optional current_vertical_swing_{}; esphome::optional current_horizontal_swing_{}; HonSettings settings_; - ESPPreferenceObject rtc_; + ESPPreferenceObject hon_rtc_; }; class HaierAlarmStartTrigger : public Trigger {