Skip to content

Commit

Permalink
Refactoring stage 3. Adjusting smartair2 initial phases to use retry …
Browse files Browse the repository at this point in the history
…mechanism
  • Loading branch information
paveldn committed Oct 12, 2023
1 parent 4332b96 commit 8f7f0d9
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 116 deletions.
3 changes: 1 addition & 2 deletions components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,4 @@ async def to_code(config):
if CONF_CONTROL_PACKET_SIZE in config:
cg.add(var.set_extra_control_packet_bytes_size(config[CONF_CONTROL_PACKET_SIZE] - PROTOCOL_CONTROL_PACKET_SIZE))
# https://github.com/paveldn/HaierProtocol
#cg.add_library("pavlodn/HaierProtocol", "0.9.22")
cg.add_library(name="HaierProtocol", repository="https://github.com/paveldn/HaierProtocol", version="a1a2a56258da1e8a24473d37464a4e11c9f71b0e")
cg.add_library("pavlodn/HaierProtocol", "0.9.23")
32 changes: 22 additions & 10 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using namespace esphome::climate;
using namespace esphome::uart;


namespace esphome {
namespace haier {

Expand Down Expand Up @@ -55,6 +56,12 @@ const char *HaierClimateBase::phase_to_string_(ProtocolPhases phase) {
}
#endif

bool check_timeout_(std::chrono::steady_clock::time_point now,
std::chrono::steady_clock::time_point tpoint, size_t timeout) {
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
}


HaierClimateBase::HaierClimateBase()
: haier_protocol_(*this),
protocol_phase_(ProtocolPhases::SENDING_INIT_1),
Expand Down Expand Up @@ -99,25 +106,20 @@ void HaierClimateBase::reset_to_idle_() {
this->action_request_ = ActionRequest::NO_ACTION;
}

bool HaierClimateBase::check_timeout_(std::chrono::steady_clock::time_point now,
std::chrono::steady_clock::time_point tpoint, size_t timeout) {
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
}

bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
return check_timeout_(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
}

bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
return check_timeout_(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
}

bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
return check_timeout_(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
}

bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
return check_timeout_(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
}

#ifdef USE_WIFI
Expand Down Expand Up @@ -202,6 +204,16 @@ haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(haier_protocol
return result;
}

haier_protocol::HandlerError HaierClimateBase::report_network_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
haier_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->set_phase(ProtocolPhases::IDLE);
return result;
}

haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", request_type, phase_to_string_(this->protocol_phase_));
Expand All @@ -221,9 +233,9 @@ void HaierClimateBase::setup() {
// Set timestamp here to give AC time to boot
this->last_request_timestamp_ = std::chrono::steady_clock::now();
this->set_phase(ProtocolPhases::SENDING_INIT_1);
this->set_handlers();
this->haier_protocol_.set_default_timeout_handler(
std::bind(&esphome::haier::HaierClimateBase::timeout_default_handler_, this, std::placeholders::_1));
this->set_handlers();
}

void HaierClimateBase::dump_config() {
Expand Down
10 changes: 3 additions & 7 deletions components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class HaierClimateBase : public esphome::Component,
void control(const esphome::climate::ClimateCall &call) override;
void dump_config() override;
float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
void set_fahrenheit(bool fahrenheit);
void set_display_state(bool state);
bool get_display_state() const;
void set_health_mode(bool state);
Expand Down Expand Up @@ -93,18 +92,18 @@ class HaierClimateBase : public esphome::Component,
virtual haier_protocol::HaierMessage get_control_message() = 0;
virtual void process_pending_action();
esphome::climate::ClimateTraits traits() override;
// Answers handlers
// Answer handlers
haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase);
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
// Timeout handler
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type);
// Helper functions
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_retries = 0, std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
virtual void set_phase(ProtocolPhases phase);
void reset_to_idle_();
bool check_timeout_(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
size_t timeout);
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now);
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
Expand All @@ -119,10 +118,7 @@ class HaierClimateBase : public esphome::Component,
esphome::optional<esphome::climate::ClimateSwingMode> swing_mode;
esphome::optional<float> target_temperature;
esphome::optional<esphome::climate::ClimatePreset> preset;
esphome::optional<bool> display;
esphome::optional<bool> health;
bool valid;
bool force_update;
HvacSettings() : valid(false){};
HvacSettings(const HvacSettings&) = default;
HvacSettings& operator=(const HvacSettings&) = default;
Expand Down
13 changes: 1 addition & 12 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,6 @@ haier_protocol::HandlerError HonClimate::get_management_information_answer_handl
}
}

haier_protocol::HandlerError HonClimate::report_network_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
haier_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->set_phase(ProtocolPhases::IDLE);
return result;
}

haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
if (request_type == haier_protocol::FrameType::GET_ALARM_STATUS) {
Expand Down Expand Up @@ -430,7 +420,6 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
: ProtocolPhases::WAITING_POWER_OFF_ANSWER);
}
break;

case ProtocolPhases::WAITING_INIT_1_ANSWER:
case ProtocolPhases::WAITING_INIT_2_ANSWER:
case ProtocolPhases::WAITING_FIRST_STATUS_ANSWER:
Expand Down Expand Up @@ -555,7 +544,7 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
}
if (climate_control.target_temperature.has_value()) {
float target_temp = climate_control.target_temperature.value();
out_data->set_point = ((int) target_temp) - 16; // set the temperature at our offset, subtract 16.
out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
}
if (out_data->ac_power == 0) {
Expand Down
2 changes: 0 additions & 2 deletions components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class HonClimate : public HaierClimateBase {
size_t data_size);
haier_protocol::HandlerError get_management_information_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError get_alarm_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
// Helper functions
Expand Down
Loading

0 comments on commit 8f7f0d9

Please sign in to comment.