|
| 1 | +#include <algorithm> |
| 2 | +#include <array> |
1 | 3 | #include <chrono> |
2 | 4 | #include <string> |
3 | 5 | #include "esphome/components/climate/climate.h" |
@@ -32,6 +34,32 @@ const std::vector<hon_protocol::HorizontalSwingMode> HORIZONTAL_SWING_MODES_ORDE |
32 | 34 | hon_protocol::HorizontalSwingMode::RIGHT, |
33 | 35 | hon_protocol::HorizontalSwingMode::MAX_RIGHT, |
34 | 36 | }; |
| 37 | + |
| 38 | +static const std::array<const char *, 8> VERTICAL_SWING_MODE_OPTIONS = { |
| 39 | + "Auto", "Health Up", "Max Up", "Up", "Center", "Down", "Max Down", "Health Down"}; |
| 40 | +static const std::array<const char *, 6> HORIZONTAL_SWING_MODE_OPTIONS = { |
| 41 | + "Auto", "Max Left", "Left", "Center", "Right", "Max Right"}; |
| 42 | + |
| 43 | +static std::string vertical_swing_mode_to_string_(hon_protocol::VerticalSwingMode mode) { |
| 44 | + if (mode == hon_protocol::VerticalSwingMode::AUTO_SPECIAL) { |
| 45 | + return VERTICAL_SWING_MODE_OPTIONS[0]; |
| 46 | + } |
| 47 | + auto mode_it = std::find(VERTICAL_SWING_MODES_ORDER.begin(), VERTICAL_SWING_MODES_ORDER.end(), mode); |
| 48 | + if (mode_it == VERTICAL_SWING_MODES_ORDER.end()) { |
| 49 | + ESP_LOGW("haier.climate", "Unsupported vertical airflow mode: 0x%X", static_cast<uint8_t>(mode)); |
| 50 | + return VERTICAL_SWING_MODE_OPTIONS[4]; |
| 51 | + } |
| 52 | + return VERTICAL_SWING_MODE_OPTIONS[mode_it - VERTICAL_SWING_MODES_ORDER.begin()]; |
| 53 | +} |
| 54 | + |
| 55 | +static std::string horizontal_swing_mode_to_string_(hon_protocol::HorizontalSwingMode mode) { |
| 56 | + auto mode_it = std::find(HORIZONTAL_SWING_MODES_ORDER.begin(), HORIZONTAL_SWING_MODES_ORDER.end(), mode); |
| 57 | + if (mode_it == HORIZONTAL_SWING_MODES_ORDER.end()) { |
| 58 | + ESP_LOGW("haier.climate", "Unsupported horizontal airflow mode: 0x%X", static_cast<uint8_t>(mode)); |
| 59 | + return HORIZONTAL_SWING_MODE_OPTIONS[3]; |
| 60 | + } |
| 61 | + return HORIZONTAL_SWING_MODE_OPTIONS[mode_it - HORIZONTAL_SWING_MODES_ORDER.begin()]; |
| 62 | +} |
35 | 63 | #endif // USE_SELECT |
36 | 64 |
|
37 | 65 | static const char *const TAG = "haier.climate"; |
@@ -843,17 +871,18 @@ void HonClimate::set_quiet_mode_switch(switch_::Switch *sw) { |
843 | 871 |
|
844 | 872 | #ifdef USE_SELECT |
845 | 873 | void HonClimate::set_vertical_airflow_select(select::Select *sel) { |
846 | | -// this->vertical_airflow_select_ = sel; |
847 | | -// if (this->current_vertical_swing_.has_value() && (this->vertical_airflow_select_ != nullptr)) { |
848 | | -// this->vertical_airflow_select_->publish_state(VerticalAirflowSelect::vertical_airflow_to_string(this->current_vertical_swing_.value())); |
849 | | -// } |
| 874 | + this->vertical_airflow_select_ = sel; |
| 875 | + if ((this->vertical_airflow_select_ != nullptr) && this->current_vertical_swing_.has_value()) { |
| 876 | + this->vertical_airflow_select_->publish_state(vertical_swing_mode_to_string_(this->current_vertical_swing_.value())); |
| 877 | + } |
850 | 878 | } |
851 | 879 |
|
852 | 880 | void HonClimate::set_horizontal_airflow_select(select::Select *sel) { |
853 | | -// this->horizontal_airflow_select_ = sel; |
854 | | -// if (this->horizontal_airflow_select_ != nullptr) { |
855 | | -// this->horizontal_airflow_select_->publish_state((uint8_t) this->settings_.last_horizontal_swing); |
856 | | -// } |
| 881 | + this->horizontal_airflow_select_ = sel; |
| 882 | + if ((this->horizontal_airflow_select_ != nullptr) && this->current_horizontal_swing_.has_value()) { |
| 883 | + this->horizontal_airflow_select_->publish_state( |
| 884 | + horizontal_swing_mode_to_string_(this->current_horizontal_swing_.value())); |
| 885 | + } |
857 | 886 | } |
858 | 887 | #endif // USE_SELECT |
859 | 888 |
|
@@ -1101,6 +1130,15 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t * |
1101 | 1130 | this->settings_.last_horizontal_swing = this->current_horizontal_swing_.value(); |
1102 | 1131 | this->hon_rtc_.save(&this->settings_); |
1103 | 1132 | } |
| 1133 | +#ifdef USE_SELECT |
| 1134 | + if (this->vertical_airflow_select_ != nullptr) { |
| 1135 | + this->vertical_airflow_select_->publish_state(vertical_swing_mode_to_string_(this->current_vertical_swing_.value())); |
| 1136 | + } |
| 1137 | + if (this->horizontal_airflow_select_ != nullptr) { |
| 1138 | + this->horizontal_airflow_select_->publish_state( |
| 1139 | + horizontal_swing_mode_to_string_(this->current_horizontal_swing_.value())); |
| 1140 | + } |
| 1141 | +#endif // USE_SELECT |
1104 | 1142 | should_publish = should_publish || (old_swing_mode != this->swing_mode); |
1105 | 1143 | } |
1106 | 1144 | this->last_valid_status_timestamp_ = std::chrono::steady_clock::now(); |
|
0 commit comments