Skip to content

Commit

Permalink
New platform haier switch added
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Apr 29, 2024
1 parent 79d1f6a commit 7e67119
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 88 deletions.
2 changes: 1 addition & 1 deletion components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def validate_visual(config):
): cv.ensure_list(
cv.enum(SUPPORTED_HON_CONTROL_METHODS, upper=True)
),
cv.Optional(CONF_BEEPER, default=True): cv.boolean,
cv.Optional(CONF_BEEPER): cv.boolean,
cv.Optional(
CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE
): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
Expand Down
67 changes: 62 additions & 5 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::stead
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
}

struct HaierStateData {
bool display_status_;
bool health_mode_;
};

HaierClimateBase::HaierClimateBase()
: haier_protocol_(*this),
protocol_phase_(ProtocolPhases::SENDING_INIT_1),
display_status_(true),
health_mode_(false),
force_send_control_(false),
forced_request_status_(false),
Expand All @@ -71,7 +75,12 @@ HaierClimateBase::HaierClimateBase()
this->traits_.set_supports_current_temperature(true);
}

HaierClimateBase::~HaierClimateBase() {}
HaierClimateBase::~HaierClimateBase() {
if (persistent_data_changed_) {
persistent_data_changed_ = false;
save_persistent_data_();
}
}

void HaierClimateBase::set_phase(ProtocolPhases phase) {
if (this->protocol_phase_ != phase) {
Expand Down Expand Up @@ -128,12 +137,17 @@ haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
}
#endif

bool HaierClimateBase::get_display_state() const { return this->display_status_; }
bool HaierClimateBase::get_display_state() const { return this->display_status_.value_or(true); }

void HaierClimateBase::set_display_state(bool state) {
if (this->display_status_ != state) {
if (!this->display_status_.has_value() || (this->display_status_.value() != state)) {
this->display_status_ = state;
this->force_send_control_ = true;
#ifdef USE_SWITCH
if (this->display_switch_ != nullptr)
this->display_switch_->publish_state(state);
#endif

}
}

Expand Down Expand Up @@ -235,7 +249,8 @@ void HaierClimateBase::setup() {
this->haier_protocol_.set_default_timeout_handler(
std::bind(&esphome::haier::HaierClimateBase::timeout_default_handler_, this, std::placeholders::_1));
this->set_handlers();
this->initialization();
if (!this->load_persistent_data_(false))
this->save_persistent_data_();
}

void HaierClimateBase::dump_config() {
Expand Down Expand Up @@ -288,6 +303,10 @@ void HaierClimateBase::loop() {
}
this->process_phase(now);
this->haier_protocol_.loop();
if (persistent_data_changed_) {
persistent_data_changed_ = false;
save_persistent_data_();
}
}

void HaierClimateBase::process_protocol_reset() {
Expand Down Expand Up @@ -373,5 +392,43 @@ void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command
}
}

bool HaierClimateBase::load_persistent_data_(bool forced) {
constexpr uint32_t RESTORE_SETTINGS_VERSION = 0xA1CC23B3;
this->haier_state_saver_ = global_preferences->make_preference<HaierStateData>(this->get_object_id_hash() ^
RESTORE_SETTINGS_VERSION);
this->persistent_data_loaded_ = true;
HaierStateData recovered;
if (!this->haier_state_saver_.load(&recovered))
recovered = { true, false };
bool result = true;
if (forced || !this->health_mode_.has_value()) {
this->health_mode_ = recovered.health_mode_;
#ifdef USE_SWITCH
if (this->health_mode_switch_ != nullptr)
this->health_mode_switch_->publish_state(recovered.health_mode_);
#endif // USE_SWITCH
} else
result = false;
if (forced || !this->display_status_.has_value()) {
this->display_status_ = recovered.display_status_;
#ifdef USE_SWITCH
if (this->display_switch_ != nullptr)
this->display_switch_->publish_state(recovered.display_status_);
#endif // USE_SWITCH
} else
result = false;
return result;
}

void HaierClimateBase::save_persistent_data_() {
if (!this->persistent_data_loaded_)
return;
HaierStateData current_state = {
this->display_status_.value_or(true),
this->health_mode_.value_or(false),
};
this->haier_state_saver_.save(&current_state);
}

} // namespace haier
} // namespace esphome
20 changes: 16 additions & 4 deletions components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "esphome/components/uart/uart.h"
// HaierProtocol
#include <protocol/haier_protocol.h>
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif


namespace esphome {
namespace haier {
Expand All @@ -23,11 +27,15 @@ class HaierClimateBase : public esphome::Component,
public esphome::climate::Climate,
public esphome::uart::UARTDevice,
public haier_protocol::ProtocolStream {
#ifdef USE_SWITCH
SUB_SWITCH(display)
SUB_SWITCH(health_mode)
#endif
public:
HaierClimateBase();
HaierClimateBase(const HaierClimateBase &) = delete;
HaierClimateBase &operator=(const HaierClimateBase &) = delete;
~HaierClimateBase();
virtual ~HaierClimateBase();
void setup() override;
void loop() override;
void control(const esphome::climate::ClimateCall &call) override;
Expand Down Expand Up @@ -80,7 +88,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;
virtual haier_protocol::HaierMessage get_power_message(bool state) = 0;
virtual void initialization() {};
virtual bool load_persistent_data_(bool forced);
virtual void save_persistent_data_();
virtual bool prepare_pending_action();
virtual void process_protocol_reset();
esphome::climate::ClimateTraits traits() override;
Expand Down Expand Up @@ -130,13 +139,15 @@ class HaierClimateBase : public esphome::Component,
esphome::optional<PendingAction> action_request_;
uint8_t fan_mode_speed_;
uint8_t other_modes_fan_speed_;
bool display_status_;
bool health_mode_;
esphome::optional<bool> display_status_{};
esphome::optional<bool> health_mode_{};
bool force_send_control_;
bool forced_request_status_;
bool reset_protocol_request_;
bool send_wifi_signal_;
bool use_crc_;
bool persistent_data_changed_{false};
bool persistent_data_loaded_{false};
esphome::climate::ClimateTraits traits_;
HvacSettings current_hvac_settings_;
HvacSettings next_hvac_settings_;
Expand All @@ -147,6 +158,7 @@ class HaierClimateBase : public esphome::Component,
std::chrono::steady_clock::time_point last_valid_status_timestamp_; // For protocol timeout
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
esphome::ESPPreferenceObject haier_state_saver_;
};

} // namespace haier
Expand Down

0 comments on commit 7e67119

Please sign in to comment.