From d682a456343312bddcbde1367618d2e1f03343f9 Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Fri, 13 Oct 2023 09:02:13 +0200 Subject: [PATCH] Format andjustments to align with ESPHome requirements --- components/haier/climate.py | 24 +++- components/haier/haier_base.cpp | 58 ++++---- components/haier/haier_base.h | 18 ++- components/haier/hon_climate.cpp | 185 +++++++++++++------------ components/haier/hon_climate.h | 21 +-- components/haier/smartair2_climate.cpp | 35 ++--- components/haier/smartair2_climate.h | 9 +- components/haier/smartair2_packet.h | 3 +- 8 files changed, 190 insertions(+), 163 deletions(-) diff --git a/components/haier/climate.py b/components/haier/climate.py index 97bd91c..f34acb5 100644 --- a/components/haier/climate.py +++ b/components/haier/climate.py @@ -195,7 +195,9 @@ def validate_visual(config): PROTOCOL_SMARTAIR2: BASE_CONFIG_SCHEMA.extend( { cv.GenerateID(): cv.declare_id(Smartair2Climate), - cv.Optional(CONF_ALTERNATIVE_SWING_CONTROL, default=False) : cv.boolean, + cv.Optional( + CONF_ALTERNATIVE_SWING_CONTROL, default=False + ): cv.boolean, cv.Optional( CONF_SUPPORTED_PRESETS, default=list( @@ -209,11 +211,15 @@ def validate_visual(config): PROTOCOL_HON: BASE_CONFIG_SCHEMA.extend( { cv.GenerateID(): cv.declare_id(HonClimate), - cv.Optional(CONF_CONTROL_METHOD, default="SET_GROUP_PARAMETERS") : cv.ensure_list( + cv.Optional( + CONF_CONTROL_METHOD, default="SET_GROUP_PARAMETERS" + ): cv.ensure_list( cv.enum(SUPPORTED_HON_CONTROL_METHODS, upper=True) ), cv.Optional(CONF_BEEPER, default=True): cv.boolean, - cv.Optional(CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50), + cv.Optional( + CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE + ): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50), cv.Optional( CONF_SUPPORTED_PRESETS, default=list(SUPPORTED_CLIMATE_PRESETS_HON_OPTIONS.keys()), @@ -422,7 +428,7 @@ async def to_code(config): await cg.register_component(var, config) await uart.register_uart_device(var, config) await climate.register_climate(var, config) - + cg.add(var.set_send_wifi(config[CONF_WIFI_SIGNAL])) if CONF_CONTROL_METHOD in config: cg.add(var.set_control_method(config[CONF_CONTROL_METHOD])) @@ -442,8 +448,14 @@ async def to_code(config): if CONF_ANSWER_TIMEOUT in config: cg.add(var.set_answer_timeout(config[CONF_ANSWER_TIMEOUT])) if CONF_ALTERNATIVE_SWING_CONTROL in config: - cg.add(var.set_alternative_swing_control(config[CONF_ALTERNATIVE_SWING_CONTROL])) + cg.add( + var.set_alternative_swing_control(config[CONF_ALTERNATIVE_SWING_CONTROL]) + ) 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)) + 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.23") diff --git a/components/haier/haier_base.cpp b/components/haier/haier_base.cpp index d5e8c12..28d8fcd 100644 --- a/components/haier/haier_base.cpp +++ b/components/haier/haier_base.cpp @@ -10,7 +10,6 @@ using namespace esphome::climate; using namespace esphome::uart; - namespace esphome { namespace haier { @@ -56,12 +55,11 @@ 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) { +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(now - tpoint).count() > timeout; } - HaierClimateBase::HaierClimateBase() : haier_protocol_(*this), protocol_phase_(ProtocolPhases::SENDING_INIT_1), @@ -107,19 +105,19 @@ void HaierClimateBase::reset_to_idle_() { } bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) { - return 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 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 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 check_timeout_(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL); + return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL); } #ifdef USE_WIFI @@ -169,9 +167,7 @@ void HaierClimateBase::set_supported_swing_modes(const std::settraits_.add_supported_swing_mode(climate::CLIMATE_SWING_OFF); } -void HaierClimateBase::set_answer_timeout(uint32_t timeout) { - this->haier_protocol_.set_answer_timeout(timeout); -} +void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); } void HaierClimateBase::set_supported_modes(const std::set &modes) { this->traits_.set_supported_modes(modes); @@ -187,15 +183,16 @@ void HaierClimateBase::set_supported_presets(const std::setsend_wifi_signal_ = send_wifi; } -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, - ProtocolPhases expected_phase) { +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, + ProtocolPhases expected_phase) { haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK; - if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && (request_message_type != expected_request_message_type)) + if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && + (request_message_type != expected_request_message_type)) result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE; - if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && (answer_message_type != expected_answer_message_type)) + if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && + (answer_message_type != expected_answer_message_type)) result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE; if ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)) result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE; @@ -204,19 +201,20 @@ 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); +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", (uint8_t) request_type, phase_to_string_(this->protocol_phase_)); + ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type, + phase_to_string_(this->protocol_phase_)); #else ESP_LOGW(TAG, "Answer timeout for command %02X, phase %d", (uint8_t) request_type, (int) this->protocol_phase_); #endif @@ -240,8 +238,7 @@ void HaierClimateBase::setup() { void HaierClimateBase::dump_config() { LOG_CLIMATE("", "Haier Climate", this); - ESP_LOGCONFIG(TAG, " Device communication status: %s", - this->valid_connection() ? "established" : "none"); + ESP_LOGCONFIG(TAG, " Device communication status: %s", this->valid_connection() ? "established" : "none"); } void HaierClimateBase::loop() { @@ -324,7 +321,7 @@ void HaierClimateBase::control(const ClimateCall &call) { ESP_LOGW(TAG, "Can't send control packet, first poll answer not received"); return; // cancel the control, we cant do it without a poll answer. } - if (this->current_hvac_settings_.valid) { + if (this->current_hvac_settings_.valid) { ESP_LOGW(TAG, "New settings come faster then processed!"); } { @@ -349,9 +346,10 @@ void HaierClimateBase::HvacSettings::reset() { this->swing_mode.reset(); this->target_temperature.reset(); this->preset.reset(); -} +} -void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats, std::chrono::milliseconds interval) { +void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats, + std::chrono::milliseconds interval) { this->haier_protocol_.send_message(command, use_crc, num_repeats, interval); this->last_request_timestamp_ = std::chrono::steady_clock::now(); } diff --git a/components/haier/haier_base.h b/components/haier/haier_base.h index cc113b5..9afc27e 100644 --- a/components/haier/haier_base.h +++ b/components/haier/haier_base.h @@ -93,15 +93,19 @@ class HaierClimateBase : public esphome::Component, virtual void process_pending_action(); esphome::climate::ClimateTraits traits() override; // 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, + 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, + 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()); + void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats = 0, + std::chrono::milliseconds interval = std::chrono::milliseconds::zero()); virtual void set_phase(ProtocolPhases phase); void reset_to_idle_(); bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now); @@ -120,8 +124,8 @@ class HaierClimateBase : public esphome::Component, esphome::optional preset; bool valid; HvacSettings() : valid(false){}; - HvacSettings(const HvacSettings&) = default; - HvacSettings& operator=(const HvacSettings&) = default; + HvacSettings(const HvacSettings &) = default; + HvacSettings &operator=(const HvacSettings &) = default; void reset(); }; haier_protocol::ProtocolHandler haier_protocol_; @@ -143,7 +147,7 @@ class HaierClimateBase : public esphome::Component, std::chrono::steady_clock::time_point last_request_timestamp_; // For interval between messages 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 + std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level }; } // namespace haier diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index c80a015..c7ca482 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -110,7 +110,8 @@ void HonClimate::start_steri_cleaning() { } } -haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, +haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size) { // Should check this before preprocess if (message_type == haier_protocol::FrameType::INVALID) { @@ -142,7 +143,8 @@ haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haie strncpy(tmp, answr->device_name, 8); this->hvac_hardware_info_.value().device_name_ = std::string(tmp); this->hvac_hardware_info_.value().functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support - this->hvac_hardware_info_.value().functions_[1] = (answr->functions[1] & 0x02) != 0; // controller-device mode support + this->hvac_hardware_info_.value().functions_[1] = + (answr->functions[1] & 0x02) != 0; // controller-device mode support this->hvac_hardware_info_.value().functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support this->hvac_hardware_info_.value().functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support this->hvac_hardware_info_.value().functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support @@ -156,7 +158,8 @@ haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haie } } -haier_protocol::HandlerError HonClimate::get_device_id_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, +haier_protocol::HandlerError HonClimate::get_device_id_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::GET_DEVICE_ID, message_type, @@ -171,8 +174,9 @@ haier_protocol::HandlerError HonClimate::get_device_id_answer_handler_(haier_pro } } -haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, - const uint8_t *data, size_t data_size) { +haier_protocol::HandlerError HonClimate::status_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::CONTROL, message_type, haier_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN); @@ -221,14 +225,12 @@ haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameTy } } -haier_protocol::HandlerError HonClimate::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 result = - this->answer_preprocess_(request_type, haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION, - message_type, haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION_RESPONSE, - ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER); +haier_protocol::HandlerError HonClimate::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 result = this->answer_preprocess_( + request_type, haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION, message_type, + haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION_RESPONSE, ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER); if (result == haier_protocol::HandlerError::HANDLER_OK) { this->set_phase(ProtocolPhases::SENDING_SIGNAL_LEVEL); return result; @@ -238,7 +240,8 @@ haier_protocol::HandlerError HonClimate::get_management_information_answer_handl } } -haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, +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) { if (message_type != haier_protocol::FrameType::GET_ALARM_STATUS_RESPONSE) { @@ -291,15 +294,18 @@ void HonClimate::set_handlers() { void HonClimate::dump_config() { HaierClimateBase::dump_config(); ESP_LOGCONFIG(TAG, " Protocol version: hOn"); - ESP_LOGCONFIG(TAG, " Control method: %d", (uint8_t)this->control_method_); + ESP_LOGCONFIG(TAG, " Control method: %d", (uint8_t) this->control_method_); if (this->hvac_hardware_info_.has_value()) { ESP_LOGCONFIG(TAG, " Device protocol version: %s", this->hvac_hardware_info_.value().protocol_version_.c_str()); ESP_LOGCONFIG(TAG, " Device software version: %s", this->hvac_hardware_info_.value().software_version_.c_str()); ESP_LOGCONFIG(TAG, " Device hardware version: %s", this->hvac_hardware_info_.value().hardware_version_.c_str()); ESP_LOGCONFIG(TAG, " Device name: %s", this->hvac_hardware_info_.value().device_name_.c_str()); - ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s", (this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""), - (this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""), (this->hvac_hardware_info_.value().functions_[2] ? " crc" : ""), - (this->hvac_hardware_info_.value().functions_[3] ? " multinode" : ""), (this->hvac_hardware_info_.value().functions_[4] ? " role" : "")); + ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s", + (this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""), + (this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""), + (this->hvac_hardware_info_.value().functions_[2] ? " crc" : ""), + (this->hvac_hardware_info_.value().functions_[3] ? " multinode" : ""), + (this->hvac_hardware_info_.value().functions_[4] ? " role" : "")); ESP_LOGCONFIG(TAG, " Active alarms: %s", buf_to_hex(this->active_alarms_, sizeof(this->active_alarms_)).c_str()); } } @@ -369,8 +375,7 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) { #endif case ProtocolPhases::SENDING_ALARM_STATUS_REQUEST: if (this->can_send_message() && this->is_message_interval_exceeded_(now)) { - static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST( - haier_protocol::FrameType::GET_ALARM_STATUS); + static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST(haier_protocol::FrameType::GET_ALARM_STATUS); this->send_message_(ALARM_STATUS_REQUEST, this->use_crc_); this->set_phase(ProtocolPhases::WAITING_ALARM_STATUS_ANSWER); } @@ -379,10 +384,9 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) { if (this->control_messages_queue_.empty()) { switch (this->control_method_) { case HonControlMethod::SET_GROUP_PARAMETERS: { - haier_protocol::HaierMessage control_message = this->get_control_message(); - this->control_messages_queue_.push(control_message); - } - break; + haier_protocol::HaierMessage control_message = this->get_control_message(); + this->control_messages_queue_.push(control_message); + } break; case HonControlMethod::SET_SINGLE_PARAMETER: this->fill_control_messages_queue_(); break; @@ -401,7 +405,8 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) { this->reset_to_idle_(); } else if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) { ESP_LOGI(TAG, "Sending control packet, queue size %d", this->control_messages_queue_.size()); - this->send_message_(this->control_messages_queue_.front(), this->use_crc_, CONTROL_MESSAGE_RETRIES, CONTROL_MESSAGE_RETRIES_INTERVAL); + this->send_message_(this->control_messages_queue_.front(), this->use_crc_, CONTROL_MESSAGE_RETRIES, + CONTROL_MESSAGE_RETRIES_INTERVAL); this->set_phase(ProtocolPhases::WAITING_CONTROL_ANSWER); } break; @@ -461,7 +466,7 @@ haier_protocol::HaierMessage HonClimate::get_control_message() { bool has_hvac_settings = false; if (this->current_hvac_settings_.valid) { has_hvac_settings = true; - HvacSettings& climate_control = this->current_hvac_settings_; + HvacSettings &climate_control = this->current_hvac_settings_; if (climate_control.mode.has_value()) { switch (climate_control.mode.value()) { case CLIMATE_MODE_OFF: @@ -636,7 +641,9 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * hon_protocol::HaierPacketSensors sensors; } packet; memcpy(&packet.control, packet_buffer + 2, sizeof(hon_protocol::HaierPacketControl)); - memcpy(&packet.sensors, packet_buffer + 2 + sizeof(hon_protocol::HaierPacketControl) + this->extra_control_packet_bytes_, sizeof(hon_protocol::HaierPacketSensors)); + memcpy(&packet.sensors, + packet_buffer + 2 + sizeof(hon_protocol::HaierPacketControl) + this->extra_control_packet_bytes_, + sizeof(hon_protocol::HaierPacketSensors)); if (packet.sensors.error_status != 0) { ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status); } @@ -832,19 +839,19 @@ void HonClimate::fill_control_messages_queue_() { climate_control = this->current_hvac_settings_; // Beeper command { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::BEEPER_STATUS, - this->beeper_status_ ? zero_buf : one_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::BEEPER_STATUS, + this->beeper_status_ ? zero_buf : one_buf, 2)); } // Health mode { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::HEALTH_MODE, - this->health_mode_ ? one_buf : zero_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::HEALTH_MODE, + this->health_mode_ ? one_buf : zero_buf, 2)); } // Climate mode bool new_power = this->mode != CLIMATE_MODE_OFF; @@ -859,41 +866,41 @@ void HonClimate::fill_control_messages_queue_() { case CLIMATE_MODE_HEAT_COOL: new_power = true; buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_MODE, - buffer, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_MODE, + buffer, 2)); fan_mode_buf[1] = this->other_modes_fan_speed_; break; case CLIMATE_MODE_HEAT: new_power = true; buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_MODE, - buffer, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_MODE, + buffer, 2)); fan_mode_buf[1] = this->other_modes_fan_speed_; break; case CLIMATE_MODE_DRY: new_power = true; buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_MODE, - buffer, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_MODE, + buffer, 2)); fan_mode_buf[1] = this->other_modes_fan_speed_; break; case CLIMATE_MODE_FAN_ONLY: new_power = true; buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_MODE, - buffer, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_MODE, + buffer, 2)); fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode // Disabling eco mode for Fan only quiet_mode_buf[1] = 0; @@ -901,11 +908,11 @@ void HonClimate::fill_control_messages_queue_() { case CLIMATE_MODE_COOL: new_power = true; buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_MODE, - buffer, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_MODE, + buffer, 2)); fan_mode_buf[1] = this->other_modes_fan_speed_; break; default: @@ -915,11 +922,11 @@ void HonClimate::fill_control_messages_queue_() { } // Climate power { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::AC_POWER, - new_power ? one_buf : zero_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::AC_POWER, + new_power ? one_buf : zero_buf, 2)); } // CLimate preset { @@ -936,7 +943,7 @@ void HonClimate::fill_control_messages_queue_() { break; case CLIMATE_PRESET_ECO: // Eco is not supported in Fan only mode - quiet_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01: 0x00; + quiet_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00; fast_mode_buf[1] = 0x00; break; case CLIMATE_PRESET_BOOST: @@ -950,29 +957,29 @@ void HonClimate::fill_control_messages_queue_() { } } if (quiet_mode_buf[1] != 0xFF) { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::QUIET_MODE, - quiet_mode_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::QUIET_MODE, + quiet_mode_buf, 2)); } if (fast_mode_buf[1] != 0xFF) { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::FAST_MODE, - fast_mode_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::FAST_MODE, + fast_mode_buf, 2)); } } // Target temperature if (climate_control.target_temperature.has_value()) { uint8_t buffer[2] = {0x00, 0x00}; - buffer[1] = ((uint8_t)climate_control.target_temperature.value()) - 16; - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::SET_POINT, - buffer, 2 - )); + buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16; + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::SET_POINT, + buffer, 2)); } // Fan mode if (climate_control.fan_mode.has_value()) { @@ -995,11 +1002,11 @@ void HonClimate::fill_control_messages_queue_() { break; } if (fan_mode_buf[1] != 0xFF) { - this->control_messages_queue_.push(haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::FAN_MODE, - fan_mode_buf, 2 - )); + this->control_messages_queue_.push( + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::FAN_MODE, + fan_mode_buf, 2)); } } } diff --git a/components/haier/hon_climate.h b/components/haier/hon_climate.h index 601a34a..bca19d2 100644 --- a/components/haier/hon_climate.h +++ b/components/haier/hon_climate.h @@ -30,11 +30,7 @@ enum class CleaningState : uint8_t { STERI_CLEAN = 2, }; -enum class HonControlMethod { - MONITOR_ONLY = 0, - SET_GROUP_PARAMETERS, - SET_SINGLE_PARAMETER -}; +enum class HonControlMethod { MONITOR_ONLY = 0, SET_GROUP_PARAMETERS, SET_SINGLE_PARAMETER }; class HonClimate : public HaierClimateBase { public: @@ -64,15 +60,20 @@ class HonClimate : public HaierClimateBase { void process_pending_action() override; // Answers handlers - haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); - haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); - haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, + haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); - haier_protocol::HandlerError get_management_information_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + 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 get_alarm_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + 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 haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size); diff --git a/components/haier/smartair2_climate.cpp b/components/haier/smartair2_climate.cpp index 17428ff..c7e5540 100644 --- a/components/haier/smartair2_climate.cpp +++ b/components/haier/smartair2_climate.cpp @@ -21,7 +21,8 @@ Smartair2Climate::Smartair2Climate() { last_status_message_ = std::unique_ptr(new uint8_t[sizeof(smartair2_protocol::HaierPacketControl)]); } -haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, +haier_protocol::HandlerError Smartair2Climate::status_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::CONTROL, message_type, @@ -65,10 +66,9 @@ haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::F } } -haier_protocol::HandlerError Smartair2Climate::get_device_version_answer_handler_(haier_protocol::FrameType request_type, - haier_protocol::FrameType message_type, - const uint8_t *data, - size_t data_size) { +haier_protocol::HandlerError Smartair2Climate::get_device_version_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_DEVICE_VERSION) return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE; if (ProtocolPhases::WAITING_INIT_1_ANSWER != this->protocol_phase_) @@ -83,7 +83,8 @@ haier_protocol::HandlerError Smartair2Climate::get_device_version_answer_handler return haier_protocol::HandlerError::HANDLER_OK; } -haier_protocol::HandlerError Smartair2Climate::messages_timeout_handler_with_cycle_for_init_(haier_protocol::FrameType message_type) { +haier_protocol::HandlerError Smartair2Climate::messages_timeout_handler_with_cycle_for_init_( + haier_protocol::FrameType message_type) { if (this->protocol_phase_ >= ProtocolPhases::IDLE) return HaierClimateBase::timeout_default_handler_(message_type); ESP_LOGI(TAG, "Answer timeout for command %02X, phase %d", (uint8_t) message_type, (int) this->protocol_phase_); @@ -108,7 +109,8 @@ void Smartair2Climate::set_handlers() { haier_protocol::FrameType::REPORT_NETWORK_STATUS, std::bind(&Smartair2Climate::report_network_status_answer_handler_, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); - this->haier_protocol_.set_default_timeout_handler(std::bind(&Smartair2Climate::messages_timeout_handler_with_cycle_for_init_, this, std::placeholders::_1)); + this->haier_protocol_.set_default_timeout_handler( + std::bind(&Smartair2Climate::messages_timeout_handler_with_cycle_for_init_, this, std::placeholders::_1)); } void Smartair2Climate::dump_config() { @@ -140,13 +142,12 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now) case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST: case ProtocolPhases::SENDING_STATUS_REQUEST: if (this->can_send_message() && this->is_message_interval_exceeded_(now)) { - static const haier_protocol::HaierMessage STATUS_REQUEST(haier_protocol::FrameType::CONTROL, - 0x4D01); - - if (this->protocol_phase_ == ProtocolPhases::SENDING_FIRST_STATUS_REQUEST) + static const haier_protocol::HaierMessage STATUS_REQUEST(haier_protocol::FrameType::CONTROL, 0x4D01); + if (this->protocol_phase_ == ProtocolPhases::SENDING_FIRST_STATUS_REQUEST) { this->send_message_(STATUS_REQUEST, this->use_crc_, INIT_REQUESTS_RETRY, INIT_REQUESTS_RETRY_INTERVAL); - else + } else { this->send_message_(STATUS_REQUEST, this->use_crc_); + } this->last_status_request_ = now; this->set_phase((ProtocolPhases) ((uint8_t) this->protocol_phase_ + 1)); } @@ -178,15 +179,17 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now) break; case ProtocolPhases::SENDING_CONTROL: if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) { - ESP_LOGI(TAG, "Sending control packet"); - this->send_message_(get_control_message(), this->use_crc_, CONTROL_MESSAGE_RETRIES, CONTROL_MESSAGE_RETRIES_INTERVAL); + ESP_LOGI(TAG, "Sending control packet"); + this->send_message_(get_control_message(), this->use_crc_, CONTROL_MESSAGE_RETRIES, + CONTROL_MESSAGE_RETRIES_INTERVAL); this->set_phase(ProtocolPhases::WAITING_CONTROL_ANSWER); } break; case ProtocolPhases::SENDING_POWER_ON_COMMAND: case ProtocolPhases::SENDING_POWER_OFF_COMMAND: if (this->can_send_message() && this->is_message_interval_exceeded_(now)) { - haier_protocol::HaierMessage power_cmd(haier_protocol::FrameType::CONTROL, + haier_protocol::HaierMessage power_cmd( + haier_protocol::FrameType::CONTROL, this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND ? 0x4D02 : 0x4D03); this->send_message_(power_cmd, this->use_crc_); this->set_phase(this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND @@ -232,7 +235,7 @@ haier_protocol::HaierMessage Smartair2Climate::get_control_message() { smartair2_protocol::HaierPacketControl *out_data = (smartair2_protocol::HaierPacketControl *) control_out_buffer; out_data->cntrl = 0; if (this->current_hvac_settings_.valid) { - HvacSettings& climate_control = this->current_hvac_settings_; + HvacSettings &climate_control = this->current_hvac_settings_; if (climate_control.mode.has_value()) { switch (climate_control.mode.value()) { case CLIMATE_MODE_OFF: diff --git a/components/haier/smartair2_climate.h b/components/haier/smartair2_climate.h index f42bb6d..e4ee70c 100644 --- a/components/haier/smartair2_climate.h +++ b/components/haier/smartair2_climate.h @@ -20,11 +20,14 @@ class Smartair2Climate : public HaierClimateBase { void process_phase(std::chrono::steady_clock::time_point now) override; haier_protocol::HaierMessage get_control_message() override; // Answer handlers - haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, + haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); - haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); - haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, + haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type, + haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size); haier_protocol::HandlerError messages_timeout_handler_with_cycle_for_init_(haier_protocol::FrameType message_type); // Helper functions diff --git a/components/haier/smartair2_packet.h b/components/haier/smartair2_packet.h index 264a0e5..22570ff 100644 --- a/components/haier/smartair2_packet.h +++ b/components/haier/smartair2_packet.h @@ -41,7 +41,7 @@ struct HaierPacketControl { // 24 uint8_t : 8; // 25 - uint8_t swing_mode; // In normal mode: If 1 - swing both direction, if 0 - horizontal_swing and + uint8_t swing_mode; // In normal mode: If 1 - swing both direction, if 0 - horizontal_swing and // vertical_swing define vertical/horizontal/off // In alternative mode: 0 - off, 01 - vertical, 02 - horizontal, 03 - both // 26 @@ -83,7 +83,6 @@ struct HaierStatus { HaierPacketControl control; }; - } // namespace smartair2_protocol } // namespace haier } // namespace esphome