Skip to content

Commit

Permalink
Refactoring stage 5. Added custom packets sending
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Oct 22, 2023
1 parent 599e043 commit 141428b
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 141 deletions.
70 changes: 36 additions & 34 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const char *HaierClimateBase::phase_to_string_(ProtocolPhases phase) {
"SENDING_UPDATE_SIGNAL_REQUEST",
"SENDING_SIGNAL_LEVEL",
"SENDING_CONTROL",
"SENDING_POWER_ON_COMMAND",
"SENDING_POWER_OFF_COMMAND",
"SENDING_ACTION_COMMAND",
"UNKNOWN" // Should be the last!
};
static_assert(
Expand All @@ -52,7 +51,6 @@ bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::stead
HaierClimateBase::HaierClimateBase()
: haier_protocol_(*this),
protocol_phase_(ProtocolPhases::SENDING_INIT_1),
action_request_(ActionRequest::NO_ACTION),
display_status_(true),
health_mode_(false),
force_send_control_(false),
Expand Down Expand Up @@ -91,7 +89,7 @@ void HaierClimateBase::reset_to_idle_() {
this->current_hvac_settings_.reset();
this->forced_request_status_ = true;
this->set_phase(ProtocolPhases::IDLE);
this->action_request_ = ActionRequest::NO_ACTION;
this->action_request_.reset();
}

bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
Expand Down Expand Up @@ -146,11 +144,11 @@ void HaierClimateBase::set_health_mode(bool state) {
}
}

void HaierClimateBase::send_power_on_command() { this->action_request_ = ActionRequest::TURN_POWER_ON; }
void HaierClimateBase::send_power_on_command() { this->action_request_ = PendingAction({ActionRequest::TURN_POWER_ON}); }

void HaierClimateBase::send_power_off_command() { this->action_request_ = ActionRequest::TURN_POWER_OFF; }
void HaierClimateBase::send_power_off_command() { this->action_request_ = PendingAction({ActionRequest::TURN_POWER_OFF}); }

void HaierClimateBase::toggle_power() { this->action_request_ = ActionRequest::TOGGLE_POWER; }
void HaierClimateBase::toggle_power() { this->action_request_ = PendingAction({ActionRequest::TOGGLE_POWER}); }

void HaierClimateBase::set_supported_swing_modes(const std::set<climate::ClimateSwingMode> &modes) {
this->traits_.set_supported_swing_modes(modes);
Expand All @@ -174,6 +172,10 @@ void HaierClimateBase::set_supported_presets(const std::set<climate::ClimatePres

void HaierClimateBase::set_send_wifi(bool send_wifi) { this->send_wifi_signal_ = send_wifi; }

void HaierClimateBase::send_custom_command(const haier_protocol::HaierMessage& message) {
this->action_request_ = PendingAction({ActionRequest::SEND_CUSTOM_COMMAND, message});
}

haier_protocol::HandlerError HaierClimateBase::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,
Expand Down Expand Up @@ -248,15 +250,15 @@ void HaierClimateBase::loop() {
return;
}
};
if ((this->protocol_phase_ == ProtocolPhases::IDLE) ||
(!this->haier_protocol_.is_waiting_for_answer() &&
((this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
if ((!this->haier_protocol_.is_waiting_for_answer()) && (
(this->protocol_phase_ == ProtocolPhases::IDLE) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL)))) {
(this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL))) {
// If control message or action is pending we should send it ASAP unless we are in initialisation
// procedure or waiting for an answer
if (this->action_request_ != ActionRequest::NO_ACTION) {
this->process_pending_action();
if (this->action_request_.has_value() && this->prepare_pending_action()) {
this->set_phase(ProtocolPhases::SENDING_ACTION_COMMAND);
} else if (this->next_hvac_settings_.valid || this->force_send_control_) {
ESP_LOGV(TAG, "Control packet is pending...");
this->set_phase(ProtocolPhases::SENDING_CONTROL);
Expand Down Expand Up @@ -287,27 +289,27 @@ void HaierClimateBase::process_protocol_reset() {
this->set_phase(ProtocolPhases::SENDING_INIT_1);
}

void HaierClimateBase::process_pending_action() {
ActionRequest request = this->action_request_;
if (this->action_request_ == ActionRequest::TOGGLE_POWER) {
request = this->mode == CLIMATE_MODE_OFF ? ActionRequest::TURN_POWER_ON : ActionRequest::TURN_POWER_OFF;
}
switch (request) {
case ActionRequest::TURN_POWER_ON:
this->set_phase(ProtocolPhases::SENDING_POWER_ON_COMMAND);
break;
case ActionRequest::TURN_POWER_OFF:
this->set_phase(ProtocolPhases::SENDING_POWER_OFF_COMMAND);
break;
case ActionRequest::TOGGLE_POWER:
case ActionRequest::NO_ACTION:
// shouldn't get here, do nothing
break;
default:
ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) this->action_request_);
break;
}
this->action_request_ = ActionRequest::NO_ACTION;
bool HaierClimateBase::prepare_pending_action() {
if (this->action_request_.has_value()) {
switch (this->action_request_.value().action) {
case ActionRequest::SEND_CUSTOM_COMMAND:
return true;
case ActionRequest::TURN_POWER_ON:
this->action_request_.value().message = this->get_power_message(true);
return true;
case ActionRequest::TURN_POWER_OFF:
this->action_request_.value().message = this->get_power_message(false);
return true;
case ActionRequest::TOGGLE_POWER:
this->action_request_.value().message = this->get_power_message(this->mode == ClimateMode::CLIMATE_MODE_OFF);
return true;
default:
ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) this->action_request_.value().action);
this->action_request_.reset();
return false;
}
} else
return false;
}

ClimateTraits HaierClimateBase::traits() { return traits_; }
Expand Down
32 changes: 19 additions & 13 deletions components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace esphome {
namespace haier {

enum class ActionRequest : uint8_t {
NO_ACTION = 0,
SEND_CUSTOM_COMMAND = 0,
TURN_POWER_ON = 1,
TURN_POWER_OFF = 2,
TOGGLE_POWER = 3,
Expand Down Expand Up @@ -55,30 +55,32 @@ class HaierClimateBase : public esphome::Component,
bool can_send_message() const { return haier_protocol_.get_outgoing_queue_size() == 0; };
void set_answer_timeout(uint32_t timeout);
void set_send_wifi(bool send_wifi);
void send_custom_command(const haier_protocol::HaierMessage& message);
//void send_custom_command(haier_protocol::HaierMessage&& message);

protected:
enum class ProtocolPhases {
UNKNOWN = -1,
// INITIALIZATION
SENDING_INIT_1 = 0,
SENDING_INIT_2 = 1,
SENDING_FIRST_STATUS_REQUEST = 2,
SENDING_ALARM_STATUS_REQUEST = 3,
SENDING_INIT_2,
SENDING_FIRST_STATUS_REQUEST,
SENDING_ALARM_STATUS_REQUEST,
// FUNCTIONAL STATE
IDLE = 4,
SENDING_STATUS_REQUEST = 5,
SENDING_UPDATE_SIGNAL_REQUEST = 6,
SENDING_SIGNAL_LEVEL = 7,
SENDING_CONTROL = 8,
SENDING_POWER_ON_COMMAND = 9,
SENDING_POWER_OFF_COMMAND = 10,
IDLE,
SENDING_STATUS_REQUEST,
SENDING_UPDATE_SIGNAL_REQUEST,
SENDING_SIGNAL_LEVEL,
SENDING_CONTROL,
SENDING_ACTION_COMMAND,
NUM_PROTOCOL_PHASES
};
const char *phase_to_string_(ProtocolPhases phase);
virtual void set_handlers() = 0;
virtual void process_phase(std::chrono::steady_clock::time_point now) = 0;
virtual haier_protocol::HaierMessage get_control_message() = 0;
virtual void process_pending_action();
virtual haier_protocol::HaierMessage get_power_message(bool state) = 0;
virtual bool prepare_pending_action();
virtual void process_protocol_reset();
esphome::climate::ClimateTraits traits() override;
// Answer handlers
Expand Down Expand Up @@ -118,9 +120,13 @@ class HaierClimateBase : public esphome::Component,
HvacSettings &operator=(const HvacSettings &) = default;
void reset();
};
struct PendingAction {
ActionRequest action;
esphome::optional<haier_protocol::HaierMessage> message;
};
haier_protocol::ProtocolHandler haier_protocol_;
ProtocolPhases protocol_phase_;
ActionRequest action_request_;
esphome::optional<PendingAction> action_request_;
uint8_t fan_mode_speed_;
uint8_t other_modes_fan_speed_;
bool display_status_;
Expand Down
Loading

0 comments on commit 141428b

Please sign in to comment.