diff --git a/components/haier/climate.py b/components/haier/climate.py index d4a2526..f7423a1 100644 --- a/components/haier/climate.py +++ b/components/haier/climate.py @@ -140,6 +140,7 @@ automation.Trigger.template(cg.const_char_ptr, cg.size_t), ) + def validate_visual(config): if CONF_VISUAL in config: visual_config = config[CONF_VISUAL] @@ -205,9 +206,7 @@ def validate_visual(config): ): cv.positive_time_period_milliseconds, cv.Optional(CONF_ON_STATUS_MESSAGE): automation.validate_automation( { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id( - StatusMessageTrigger - ), + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StatusMessageTrigger), } ), } @@ -246,10 +245,12 @@ def validate_visual(config): CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE ): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50), cv.Optional( - CONF_SENSORS_PACKET_SIZE, default=PROTOCOL_DEFAULT_SENSORS_PACKET_SIZE + CONF_SENSORS_PACKET_SIZE, + default=PROTOCOL_DEFAULT_SENSORS_PACKET_SIZE, ): cv.int_range(min=PROTOCOL_MIN_SENSORS_PACKET_SIZE, max=50), cv.Optional( - CONF_STATUS_MESSAGE_HEADER_SIZE, default=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE + CONF_STATUS_MESSAGE_HEADER_SIZE, + default=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE, ): cv.int_range(min=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE), cv.Optional( CONF_SUPPORTED_PRESETS, @@ -515,6 +516,6 @@ async def to_code(config): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation( trigger, [(cg.const_char_ptr, "data"), (cg.size_t, "data_size")], conf - ) + ) # https://github.com/paveldn/HaierProtocol cg.add_library("pavlodn/HaierProtocol", "0.9.31") diff --git a/components/haier/haier_base.cpp b/components/haier/haier_base.cpp index d3fb0ff..0bd3863 100644 --- a/components/haier/haier_base.cpp +++ b/components/haier/haier_base.cpp @@ -186,7 +186,7 @@ void HaierClimateBase::send_custom_command(const haier_protocol::HaierMessage &m this->action_request_ = PendingAction({ActionRequest::SEND_CUSTOM_COMMAND, message}); } -void HaierClimateBase::add_status_message_callback(std::function &&callback) { +void HaierClimateBase::add_status_message_callback(std::function &&callback) { this->status_message_callback_.add(std::move(callback)); } diff --git a/components/haier/haier_base.h b/components/haier/haier_base.h index 4070e1b..c0bf878 100644 --- a/components/haier/haier_base.h +++ b/components/haier/haier_base.h @@ -57,7 +57,7 @@ class HaierClimateBase : public esphome::Component, void set_answer_timeout(uint32_t timeout); void set_send_wifi(bool send_wifi); void send_custom_command(const haier_protocol::HaierMessage &message); - void add_status_message_callback(std::function &&callback); + void add_status_message_callback(std::function &&callback); protected: enum class ProtocolPhases { @@ -142,19 +142,18 @@ class HaierClimateBase : public esphome::Component, esphome::climate::ClimateTraits traits_; HvacSettings current_hvac_settings_; HvacSettings next_hvac_settings_; - std::unique_ptr last_status_message_{ nullptr }; + std::unique_ptr last_status_message_{nullptr}; 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 - CallbackManager status_message_callback_{}; + CallbackManager status_message_callback_{}; }; -class StatusMessageTrigger : public Trigger { +class StatusMessageTrigger : public Trigger { public: explicit StatusMessageTrigger(HaierClimateBase *parent) { - parent->add_status_message_callback( - [this](const char* data, size_t data_size) { this->trigger(data, data_size); }); + parent->add_status_message_callback([this](const char *data, size_t data_size) { this->trigger(data, data_size); }); } }; diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index 4ba05d3..11f3466 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -21,7 +21,6 @@ constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS = 600000; const uint8_t ONE_BUF[] = {0x00, 0x01}; const uint8_t ZERO_BUF[] = {0x00, 0x00}; - HonClimate::HonClimate() : cleaning_status_(CleaningState::NO_CLEANING), got_valid_outdoor_temp_(false), active_alarms_{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -177,11 +176,11 @@ haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameTy this->last_status_message_.reset(new uint8_t[this->real_control_packet_size_]); }; if (data_size >= this->real_control_packet_size_ + 2) { - memcpy(this->last_status_message_.get(), data + 2 + this->status_message_header_size_, this->real_control_packet_size_); - this->status_message_callback_.call((const char*) data, data_size); + memcpy(this->last_status_message_.get(), data + 2 + this->status_message_header_size_, + this->real_control_packet_size_); + this->status_message_callback_.call((const char *) data, data_size); } else { - ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size, - this->real_control_packet_size_); + ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size, this->real_control_packet_size_); } switch (this->protocol_phase_) { case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST: @@ -487,7 +486,7 @@ void HonClimate::initialization() { } haier_protocol::HaierMessage HonClimate::get_control_message() { - static uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE]; + uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE]; memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_); hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer; control_out_buffer[4] = 0; // This byte should be cleared before setting values @@ -766,7 +765,8 @@ void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const std::stri #endif // USE_TEXT_SENSOR haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) { - size_t expected_size = 2 + this->status_message_header_size_ + this->real_control_packet_size_ + this->real_sensors_packet_size_; + size_t expected_size = + 2 + this->status_message_header_size_ + this->real_control_packet_size_ + this->real_sensors_packet_size_; if (size < expected_size) { ESP_LOGW(TAG, "Unexpected message size %d (expexted >= %d)", size, expected_size); return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE; @@ -804,7 +804,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * hon_protocol::HaierPacketControl control; hon_protocol::HaierPacketSensors sensors; } packet; - memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_, sizeof(hon_protocol::HaierPacketControl)); + memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_, + sizeof(hon_protocol::HaierPacketControl)); memcpy(&packet.sensors, packet_buffer + 2 + this->status_message_header_size_ + this->real_control_packet_size_, sizeof(hon_protocol::HaierPacketSensors)); if (packet.sensors.error_status != 0) { @@ -1171,14 +1172,14 @@ void HonClimate::fill_control_messages_queue_() { (uint8_t) hon_protocol::DataParameters::SET_POINT, buffer, 2)); } - // Vertical swing mode + // Vertical swing mode if (climate_control.swing_mode.has_value()) { - uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO}; - uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO}; + uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO}; + uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO}; switch (climate_control.swing_mode.value()) { case CLIMATE_SWING_OFF: horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing; - vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing; + vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing; break; case CLIMATE_SWING_VERTICAL: horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing; @@ -1191,14 +1192,14 @@ void HonClimate::fill_control_messages_queue_() { } 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::HORIZONTAL_SWING_MODE, - horizontal_swing_buf, 2)); + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::HORIZONTAL_SWING_MODE, + horizontal_swing_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::VERTICAL_SWING_MODE, - vertical_swing_buf, 2)); + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::VERTICAL_SWING_MODE, + vertical_swing_buf, 2)); } // Fan mode if (climate_control.fan_mode.has_value()) { @@ -1254,16 +1255,14 @@ bool HonClimate::prepare_pending_action() { haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS, control_out_buffer, this->real_control_packet_size_); return true; - } - else if (this->control_method_ == HonControlMethod::SET_SINGLE_PARAMETER) { - this->action_request_.value().message = haier_protocol::HaierMessage( - haier_protocol::FrameType::CONTROL, - (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + - (uint8_t) hon_protocol::DataParameters::SELF_CLEANING, - ONE_BUF, 2); - return true; - } - else { + } else if (this->control_method_ == HonControlMethod::SET_SINGLE_PARAMETER) { + this->action_request_.value().message = + haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, + (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + + (uint8_t) hon_protocol::DataParameters::SELF_CLEANING, + ONE_BUF, 2); + return true; + } else { this->action_request_.reset(); return false; } diff --git a/components/haier/hon_climate.h b/components/haier/hon_climate.h index f00f020..64c5418 100644 --- a/components/haier/hon_climate.h +++ b/components/haier/hon_climate.h @@ -104,7 +104,7 @@ class HonClimate : public HaierClimateBase { void start_self_cleaning(); void start_steri_cleaning(); void set_extra_control_packet_bytes_size(size_t size) { this->extra_control_packet_bytes_ = size; }; - void set_extra_sensors_packet_bytes_size(size_t size) { this->extra_sensors_packet_bytes_ = size; }; + void set_extra_sensors_packet_bytes_size(size_t size) { this->extra_sensors_packet_bytes_ = size; }; void set_status_message_header_size(size_t size) { this->status_message_header_size_ = size; }; void set_control_method(HonControlMethod method) { this->control_method_ = method; }; void add_alarm_start_callback(std::function &&callback); @@ -163,8 +163,8 @@ class HonClimate : public HaierClimateBase { int extra_control_packet_bytes_{0}; int extra_sensors_packet_bytes_{4}; int status_message_header_size_{0}; - int real_control_packet_size_{ sizeof(hon_protocol::HaierPacketControl) }; - int real_sensors_packet_size_{ sizeof(hon_protocol::HaierPacketSensors) + 4 }; + int real_control_packet_size_{sizeof(hon_protocol::HaierPacketControl)}; + int real_sensors_packet_size_{sizeof(hon_protocol::HaierPacketSensors) + 4}; HonControlMethod control_method_; std::queue control_messages_queue_; CallbackManager alarm_start_callback_{}; diff --git a/components/haier/smartair2_climate.cpp b/components/haier/smartair2_climate.cpp index bf2894e..028e8a4 100644 --- a/components/haier/smartair2_climate.cpp +++ b/components/haier/smartair2_climate.cpp @@ -37,7 +37,7 @@ haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::F } else { if (data_size >= sizeof(smartair2_protocol::HaierPacketControl) + 2) { memcpy(this->last_status_message_.get(), data + 2, sizeof(smartair2_protocol::HaierPacketControl)); - this->status_message_callback_.call((const char*) data, data_size); + this->status_message_callback_.call((const char *) data, data_size); } else { ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size, sizeof(smartair2_protocol::HaierPacketControl));