From 3afba23593db1c8fdc5aebda5ff921a6b30ae50f Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Wed, 23 Oct 2024 08:26:07 +0200 Subject: [PATCH] Select for haier implementation changed to keep order of item names and values only in one place --- .vscode/settings.json | 57 ++++++++++++++++++- components/haier/hon_climate.cpp | 22 +++++++ components/haier/hon_climate.h | 5 ++ components/haier/select/__init__.py | 48 +++++++--------- .../haier/select/horizontal_airflow.cpp | 17 ++---- components/haier/select/vertical_airflow.cpp | 21 ++----- 6 files changed, 111 insertions(+), 59 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fe23763..a0d6b94 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,7 +18,62 @@ "queue": "cpp", "iosfwd": "cpp", "xlocbuf": "cpp", - "atomic": "cpp" + "atomic": "cpp", + "algorithm": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "format": "cpp", + "forward_list": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "random": "cpp", + "ratio": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "xtree": "cpp" }, "files.trimTrailingWhitespace": true, "files.exclude": { diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index d57242a..b85ba52 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -12,6 +12,28 @@ using namespace esphome::uart; namespace esphome { namespace haier { +#ifdef USE_SELECT +const std::vector VERTICAL_SWING_MODES_ORDER = { + hon_protocol::VerticalSwingMode::AUTO, + hon_protocol::VerticalSwingMode::HEALTH_UP, + hon_protocol::VerticalSwingMode::MAX_UP, + hon_protocol::VerticalSwingMode::UP, + hon_protocol::VerticalSwingMode::CENTER, + hon_protocol::VerticalSwingMode::DOWN, + hon_protocol::VerticalSwingMode::MAX_DOWN, + hon_protocol::VerticalSwingMode::HEALTH_DOWN, +}; + +const std::vector HORIZONTAL_SWING_MODES_ORDER = { + hon_protocol::HorizontalSwingMode::AUTO, + hon_protocol::HorizontalSwingMode::MAX_LEFT, + hon_protocol::HorizontalSwingMode::LEFT, + hon_protocol::HorizontalSwingMode::CENTER, + hon_protocol::HorizontalSwingMode::RIGHT, + hon_protocol::HorizontalSwingMode::MAX_RIGHT, +}; +#endif // USE_SELECT + static const char *const TAG = "haier.climate"; constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000; constexpr int PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET = -64; diff --git a/components/haier/hon_climate.h b/components/haier/hon_climate.h index 07e4606..ee06a71 100644 --- a/components/haier/hon_climate.h +++ b/components/haier/hon_climate.h @@ -23,6 +23,11 @@ namespace esphome { namespace haier { +#ifdef USE_SELECT +extern const std::vector HORIZONTAL_SWING_MODES_ORDER; +extern const std::vector VERTICAL_SWING_MODES_ORDER; +#endif + enum class CleaningState : uint8_t { NO_CLEANING = 0, SELF_CLEAN = 1, diff --git a/components/haier/select/__init__.py b/components/haier/select/__init__.py index 139b997..f626e4a 100644 --- a/components/haier/select/__init__.py +++ b/components/haier/select/__init__.py @@ -25,33 +25,25 @@ ICON_ARROW_HORIZONTAL = "mdi:arrow-expand-horizontal" ICON_ARROW_VERTICAL = "mdi:arrow-expand-vertical" -AIRFLOW_VERTICAL_DIRECTION_OPTIONS = { - "Auto": VerticalSwingMode.AUTO, - "Health Up": VerticalSwingMode.HEALTH_UP, - "Max Up": VerticalSwingMode.MAX_UP, - "Up": VerticalSwingMode.UP, - "Center": VerticalSwingMode.CENTER, - "Down": VerticalSwingMode.DOWN, - "Max Down": VerticalSwingMode.MAX_DOWN, - "Health Down": VerticalSwingMode.HEALTH_DOWN, -} +AIRFLOW_VERTICAL_DIRECTION_OPTIONS = [ + "Auto", + "Health Up", + "Max Up", + "Up", + "Center", + "Down", + "Max Down", + "Health Down", +] -AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = { - "Auto": HorizontalSwingMode.AUTO, - "Max Left": HorizontalSwingMode.MAX_LEFT, - "Left": HorizontalSwingMode.LEFT, - "Center": HorizontalSwingMode.CENTER, - "Right": HorizontalSwingMode.RIGHT, - "Max Right": HorizontalSwingMode.MAX_RIGHT, -} - -#def check_airflow_map(value, options_map): -# cv.check_not_templatable(value) -# option_key = cv.All(cv.string_strict) -# option_value = -# if value not in AIRFLOW_VERTICAL_DIRECTION_OPTIONS: -# raise cv.Invalid(f"Invalid airflow value {value}, must be one of: {', '.join(AIRFLOW_VERTICAL_DIRECTION_OPTIONS)}") -# return value +AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = [ + "Auto", + "Max Left", + "Left", + "Center", + "Right", + "Max Right", +] CONFIG_SCHEMA = cv.Schema( { @@ -73,12 +65,12 @@ async def to_code(config): full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID]) if conf := config.get(CONF_VERTICAL_AIRFLOW): - sel_var = await select.new_select(conf, options=list(AIRFLOW_VERTICAL_DIRECTION_OPTIONS.keys())) + sel_var = await select.new_select(conf, options=AIRFLOW_VERTICAL_DIRECTION_OPTIONS) await cg.register_parented(sel_var, parent) cg.add(getattr(parent, f"set_{CONF_VERTICAL_AIRFLOW}_select")(sel_var)) if conf := config.get(CONF_HORIZONTAL_AIRFLOW): - sel_var = await select.new_select(conf, options=list(AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS.keys())) + sel_var = await select.new_select(conf, options=AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS) await cg.register_parented(sel_var, parent) cg.add(getattr(parent, f"set_{CONF_HORIZONTAL_AIRFLOW}_select")(sel_var)) diff --git a/components/haier/select/horizontal_airflow.cpp b/components/haier/select/horizontal_airflow.cpp index 4031f0f..521ba2c 100644 --- a/components/haier/select/horizontal_airflow.cpp +++ b/components/haier/select/horizontal_airflow.cpp @@ -6,22 +6,13 @@ namespace haier { void HorizontalAirflowSelect::control(const std::string &value) { hon_protocol::HorizontalSwingMode state; - if (value == "Auto") { - state = hon_protocol::HorizontalSwingMode::AUTO; - } else if (value == "Max Left") { - state = hon_protocol::HorizontalSwingMode::MAX_LEFT; - } else if (value == "Left") { - state = hon_protocol::HorizontalSwingMode::LEFT; - } else if (value == "Center") { - state = hon_protocol::HorizontalSwingMode::CENTER; - } else if (value == "Right") { - state = hon_protocol::HorizontalSwingMode::RIGHT; - } else if (value == "Max Right") { - state = hon_protocol::HorizontalSwingMode::MAX_RIGHT; - } else { + static const std::vector options = this->traits.get_options(); + auto item_it = std::find(options.begin(), options.end(), value); + if (item_it == options.end()) { ESP_LOGE("haier", "Invalid horizontal airflow mode: %s", value.c_str()); return; } + state = HORIZONTAL_SWING_MODES_ORDER[item_it - options.begin()]; const esphome::optional current_state = this->parent_->get_horizontal_airflow(); if (!current_state.has_value() || (current_state.value() != state)) { this->parent_->set_horizontal_airflow(state); diff --git a/components/haier/select/vertical_airflow.cpp b/components/haier/select/vertical_airflow.cpp index 2ba65af..4f2dad8 100644 --- a/components/haier/select/vertical_airflow.cpp +++ b/components/haier/select/vertical_airflow.cpp @@ -6,26 +6,13 @@ namespace haier { void VerticalAirflowSelect::control(const std::string &value) { hon_protocol::VerticalSwingMode state; - if (value == "Auto") { - state = hon_protocol::VerticalSwingMode::AUTO; - } else if (value == "Health Up") { - state = hon_protocol::VerticalSwingMode::HEALTH_UP; - } else if (value == "Max Up") { - state = hon_protocol::VerticalSwingMode::MAX_UP; - } else if (value == "Up") { - state = hon_protocol::VerticalSwingMode::UP; - } else if (value == "Center") { - state = hon_protocol::VerticalSwingMode::CENTER; - } else if (value == "Down") { - state = hon_protocol::VerticalSwingMode::DOWN; - } else if (value == "Max Down") { - state = hon_protocol::VerticalSwingMode::MAX_DOWN; - } else if (value == "Health Down") { - state = hon_protocol::VerticalSwingMode::HEALTH_DOWN; - } else { + static const std::vector options = this->traits.get_options(); + auto item_it = std::find(options.begin(), options.end(), value); + if (item_it == options.end()) { ESP_LOGE("haier", "Invalid vertical airflow mode: %s", value.c_str()); return; } + state = VERTICAL_SWING_MODES_ORDER[item_it - options.begin()]; const esphome::optional current_state = this->parent_->get_vertical_airflow(); if (!current_state.has_value() || (current_state.value() != state)) { this->parent_->set_vertical_airflow(state);