Skip to content

Commit

Permalink
Added state save for display and health mode settings
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Aug 31, 2024
1 parent af375cc commit d09b5e1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
28 changes: 28 additions & 0 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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<HaierBaseSettings>(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;
#if 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()) {
Expand Down
9 changes: 8 additions & 1 deletion components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<void(const char *, size_t)> status_message_callback_{};
ESPPreferenceObject base_rtc_;
};

class StatusMessageTrigger : public Trigger<const char *, size_t> {
Expand Down
9 changes: 5 additions & 4 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
}

Expand Down Expand Up @@ -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<HonSettings>(this->get_object_id_hash() ^ restore_settings_version);
this->hon_rtc_ = global_preferences->make_preference<HonSettings>(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};
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class HonClimate : public HaierClimateBase {
esphome::optional<hon_protocol::VerticalSwingMode> current_vertical_swing_{};
esphome::optional<hon_protocol::HorizontalSwingMode> current_horizontal_swing_{};
HonSettings settings_;
ESPPreferenceObject rtc_;
ESPPreferenceObject hon_rtc_;
};

class HaierAlarmStartTrigger : public Trigger<uint8_t, const char *> {
Expand Down

0 comments on commit d09b5e1

Please sign in to comment.