From a82196d2566f723a89cc2eacc10bfa947a60b548 Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Fri, 6 Sep 2024 09:25:20 +0200 Subject: [PATCH] Code and docs aligned with ESPHome requirements --- README.rst | 2 -- components/haier/haier_base.cpp | 15 +++++++------ components/haier/haier_base.h | 2 +- components/haier/hon_climate.cpp | 31 ++++++++++++++++---------- components/haier/smartair2_climate.cpp | 12 +++++----- components/haier/switch/__init__.py | 6 +++-- docs/esphome-docs/climate/haier.rst | 2 -- 7 files changed, 38 insertions(+), 32 deletions(-) diff --git a/README.rst b/README.rst index 6c684ec..5b65850 100644 --- a/README.rst +++ b/README.rst @@ -107,10 +107,8 @@ This component requires a `UART Bus `_ Configuration variables: ------------------------ -- **id** (*Optional*, `ID `_): Manually specify the ID used for code generation. - **uart_id** (*Optional*, `ID `_): ID of the UART port to communicate with AC. - **protocol** (*Optional*, string): Defines communication protocol with AC. Possible values: ``hon`` or ``smartair2``. The default value is ``smartair2``. -- **name** (**Required**, string): The name of the climate device. - **wifi_signal** (*Optional*, boolean): If ``true`` - send wifi signal level to AC. - **answer_timeout** (*Optional*, `Time `_): Responce timeout. The default value is ``200ms``. - **alternative_swing_control** (*Optional*, boolean): (supported by smartAir2 only) If ``true`` - use alternative values to control swing mode. Use only if the original control method is not working for your AC. diff --git a/components/haier/haier_base.cpp b/components/haier/haier_base.cpp index 32f3525..ba80c1c 100644 --- a/components/haier/haier_base.cpp +++ b/components/haier/haier_base.cpp @@ -126,14 +126,14 @@ haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() { #endif void HaierClimateBase::save_settings() { - HaierBaseSettings settings { this->get_health_mode(), this->get_display_state() }; + HaierBaseSettings settings{this->get_health_mode(), this->get_display_state()}; if (!this->base_rtc_.save(&settings)) { ESP_LOGW(TAG, "Failed to save settings"); } } bool HaierClimateBase::get_display_state() const { - return (this->display_status_ == SwitchState::ON) || (this->display_status_ == SwitchState::PENDING_ON); + return (this->display_status_ == SwitchState::ON) || (this->display_status_ == SwitchState::PENDING_ON); } void HaierClimateBase::set_display_state(bool state) { @@ -305,7 +305,7 @@ void HaierClimateBase::loop() { if ((this->health_mode_switch_ != nullptr) && (this->health_mode_switch_->state != this->get_health_mode())) { this->health_mode_switch_->publish_state(this->get_health_mode()); } -#endif // USE_SWITCH +#endif // USE_SWITCH } void HaierClimateBase::process_protocol_reset() { @@ -349,13 +349,14 @@ bool HaierClimateBase::prepare_pending_action() { ClimateTraits HaierClimateBase::traits() { return traits_; } void HaierClimateBase::initialization() { - constexpr uint32_t restore_settings_version = 0xA77D21EF; - this->base_rtc_ = global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); + constexpr uint32_t restore_settings_version = 0xA77D21EF; + this->base_rtc_ = + global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); HaierBaseSettings recovered; if (!this->base_rtc_.load(&recovered)) { - recovered = { false, true }; + recovered = {false, true}; } - this->display_status_= recovered.display_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; + this->display_status_ = recovered.display_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; this->health_mode_ = recovered.health_mode ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF; #ifdef USE_SWITCH if (this->display_switch_ != nullptr) { diff --git a/components/haier/haier_base.h b/components/haier/haier_base.h index bc2b893..f0597c4 100644 --- a/components/haier/haier_base.h +++ b/components/haier/haier_base.h @@ -146,7 +146,7 @@ class HaierClimateBase : public esphome::Component, ActionRequest action; esphome::optional message; }; - enum class SwitchState{ + enum class SwitchState { OFF = 0b00, ON = 0b01, PENDING_OFF = 0b10, diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index cd92cc2..e7be1fa 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -54,7 +54,9 @@ void HonClimate::set_quiet_mode_state(bool state) { } } -bool HonClimate::get_quiet_mode_state() const { return (this->quiet_mode_state_ == SwitchState::ON) || (this->quiet_mode_state_ == SwitchState::PENDING_ON); } +bool HonClimate::get_quiet_mode_state() const { + return (this->quiet_mode_state_ == SwitchState::ON) || (this->quiet_mode_state_ == SwitchState::PENDING_ON); +} esphome::optional HonClimate::get_vertical_airflow() const { return this->current_vertical_swing_; @@ -497,7 +499,8 @@ haier_protocol::HaierMessage HonClimate::get_power_message(bool state) { void HonClimate::initialization() { HaierClimateBase::initialization(); constexpr uint32_t restore_settings_version = 0x57EB59DDUL; - this->hon_rtc_ = global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); + this->hon_rtc_ = + global_preferences->make_preference(this->get_object_id_hash() ^ restore_settings_version); HonSettings recovered; if (this->hon_rtc_.load(&recovered)) { this->settings_ = recovered; @@ -661,9 +664,9 @@ haier_protocol::HaierMessage HonClimate::get_control_message() { out_data->beeper_status = ((!this->get_beeper_state()) || (!has_hvac_settings)) ? 1 : 0; control_out_buffer[4] = 0; // This byte should be cleared before setting values out_data->display_status = this->get_display_state() ? 1 : 0; - this->display_status_ = (SwitchState)((uint8_t)this->display_status_ & 0b01); + this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01); out_data->health_mode = this->get_health_mode() ? 1 : 0; - this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01); + this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01); return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS, control_out_buffer, this->real_control_packet_size_); @@ -929,13 +932,13 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * if (this->mode == CLIMATE_MODE_OFF) { // AC just turned on from remote need to turn off display this->force_send_control_ = true; - } else if ((((uint8_t)this->health_mode_) & 0b10) == 0) { + } else if ((((uint8_t) this->health_mode_) & 0b10) == 0) { this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF; } } } // Health mode - if ((((uint8_t)this->health_mode_) & 0b10) == 0) { + if ((((uint8_t) this->health_mode_) & 0b10) == 0) { bool old_health_mode = this->get_health_mode(); this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF; should_publish = should_publish || (old_health_mode != this->get_health_mode()); @@ -994,7 +997,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * } { // Quiet mode, should be after climate mode - if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) && ((((uint8_t)this->quiet_mode_state_) & 0b10) == 0)) { + if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) && + ((((uint8_t) this->quiet_mode_state_) & 0b10) == 0)) { // In proper mode and not in pending state bool new_quiet_mode = packet.control.quiet_mode != 0; if (new_quiet_mode != this->get_quiet_mode_state()) { @@ -1007,17 +1011,20 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * { // Swing mode ClimateSwingMode old_swing_mode = this->swing_mode; - const std::set& swing_modes = traits_.get_supported_swing_modes(); + const std::set &swing_modes = traits_.get_supported_swing_modes(); bool vertical_swing_supported = swing_modes.find(CLIMATE_SWING_VERTICAL) != swing_modes.end(); bool horizontal_swing_supported = swing_modes.find(CLIMATE_SWING_HORIZONTAL) != swing_modes.end(); - if (horizontal_swing_supported && (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) { - if (vertical_swing_supported && (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) { + if (horizontal_swing_supported && + (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) { + if (vertical_swing_supported && + (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) { this->swing_mode = CLIMATE_SWING_BOTH; } else { this->swing_mode = CLIMATE_SWING_HORIZONTAL; } } else { - if (vertical_swing_supported && (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) { + if (vertical_swing_supported && + (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) { this->swing_mode = CLIMATE_SWING_VERTICAL; } else { this->swing_mode = CLIMATE_SWING_OFF; @@ -1075,7 +1082,7 @@ void HonClimate::fill_control_messages_queue_() { (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER + (uint8_t) hon_protocol::DataParameters::HEALTH_MODE, this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2)); - this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01); + this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01); } // Climate mode ClimateMode climate_mode = this->mode; diff --git a/components/haier/smartair2_climate.cpp b/components/haier/smartair2_climate.cpp index f694ab3..63c2282 100644 --- a/components/haier/smartair2_climate.cpp +++ b/components/haier/smartair2_climate.cpp @@ -377,9 +377,9 @@ haier_protocol::HaierMessage Smartair2Climate::get_control_message() { } } out_data->display_status = this->get_display_state() ? 0 : 1; - this->display_status_ = (SwitchState)((uint8_t)this->display_status_ & 0b01); + this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01); out_data->health_mode = this->get_health_mode() ? 1 : 0; - this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01); + this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01); return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, 0x4D5F, control_out_buffer, sizeof(smartair2_protocol::HaierPacketControl)); } @@ -451,20 +451,20 @@ haier_protocol::HandlerError Smartair2Climate::process_status_message_(const uin // Display status // should be before "Climate mode" because it is changing this->mode if (packet.control.ac_power != 0) { - // if AC is off display status always ON so process it only when AC is on - bool disp_status = packet.control.display_status == 0; + // if AC is off display status always ON so process it only when AC is on + bool disp_status = packet.control.display_status == 0; if (disp_status != this->get_display_state()) { // Do something only if display status changed if (this->mode == CLIMATE_MODE_OFF) { // AC just turned on from remote need to turn off display this->force_send_control_ = true; - } else if ((((uint8_t)this->health_mode_) & 0b10) == 0) { + } else if ((((uint8_t) this->health_mode_) & 0b10) == 0) { this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF; } } } // Health mode - if ((((uint8_t)this->health_mode_) & 0b10) == 0) { + if ((((uint8_t) this->health_mode_) & 0b10) == 0) { bool old_health_mode = this->get_health_mode(); this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF; should_publish = should_publish || (old_health_mode != this->get_health_mode()); diff --git a/components/haier/switch/__init__.py b/components/haier/switch/__init__.py index 0155f1a..2a8aa72 100644 --- a/components/haier/switch/__init__.py +++ b/components/haier/switch/__init__.py @@ -60,6 +60,7 @@ } ) + async def to_code(config): full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID]) @@ -75,5 +76,6 @@ async def to_code(config): await cg.register_parented(sw_var, parent) cg.add(getattr(parent, f"set_{switch_type}_switch")(sw_var)) else: - raise ValueError(f"{switch_type} switch is only supported for hon climate") - + raise ValueError( + f"{switch_type} switch is only supported for hon climate" + ) diff --git a/docs/esphome-docs/climate/haier.rst b/docs/esphome-docs/climate/haier.rst index 783297c..56ed801 100644 --- a/docs/esphome-docs/climate/haier.rst +++ b/docs/esphome-docs/climate/haier.rst @@ -109,10 +109,8 @@ This component requires a :ref:`uart` to be setup. Configuration variables: ------------------------ -- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation. - **uart_id** (*Optional*, :ref:`config-id`): ID of the UART port to communicate with AC. - **protocol** (*Optional*, string): Defines communication protocol with AC. Possible values: ``hon`` or ``smartair2``. The default value is ``smartair2``. -- **name** (**Required**, string): The name of the climate device. - **wifi_signal** (*Optional*, boolean): If ``true`` - send wifi signal level to AC. - **answer_timeout** (*Optional*, :ref:`config-time`): Responce timeout. The default value is ``200ms``. - **alternative_swing_control** (*Optional*, boolean): (supported by smartAir2 only) If ``true`` - use alternative values to control swing mode. Use only if the original control method is not working for your AC.