From 3c6231956f0f5a1cca992fb93691ce9f417c7179 Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Sat, 7 Sep 2024 22:25:20 +0200 Subject: [PATCH] Preparation commit --- .vscode/settings.json | 5 +- components/haier/hon_climate.cpp | 20 ++++++ components/haier/hon_climate.h | 12 ++++ components/haier/select/__init__.py | 63 +++++++++++-------- .../haier/select/horizontal_airflow.cpp | 33 ++++++++++ components/haier/select/horizontal_airflow.h | 18 ++++++ components/haier/select/vertical_airflow.cpp | 12 ++++ components/haier/select/vertical_airflow.h | 18 ++++++ 8 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 components/haier/select/horizontal_airflow.cpp create mode 100644 components/haier/select/horizontal_airflow.h create mode 100644 components/haier/select/vertical_airflow.cpp create mode 100644 components/haier/select/vertical_airflow.h diff --git a/.vscode/settings.json b/.vscode/settings.json index c08e182..fe23763 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,7 +15,10 @@ "istream": "cpp", "sstream": "cpp", "optional": "cpp", - "queue": "cpp" + "queue": "cpp", + "iosfwd": "cpp", + "xlocbuf": "cpp", + "atomic": "cpp" }, "files.trimTrailingWhitespace": true, "files.exclude": { diff --git a/components/haier/hon_climate.cpp b/components/haier/hon_climate.cpp index e7be1fa..125322d 100644 --- a/components/haier/hon_climate.cpp +++ b/components/haier/hon_climate.cpp @@ -5,6 +5,10 @@ #include "esphome/core/helpers.h" #include "hon_climate.h" #include "hon_packet.h" +#ifdef USE_SELECT +#include "select/vertical_airflow.h" +#include "select/horizontal_airflow.h" +#endif using namespace esphome::climate; using namespace esphome::uart; @@ -806,6 +810,22 @@ void HonClimate::set_quiet_mode_switch(switch_::Switch *sw) { } #endif // USE_SWITCH +#ifdef USE_SELECT +void HonClimate::set_vertical_airflow_select(select::Select *sel) { +// this->vertical_airflow_select_ = sel; +// if (this->current_vertical_swing_.has_value() && (this->vertical_airflow_select_ != nullptr)) { +// this->vertical_airflow_select_->publish_state(VerticalAirflowSelect::vertical_airflow_to_string(this->current_vertical_swing_.value())); +// } +} + +void HonClimate::set_horizontal_airflow_select(select::Select *sel) { +// this->horizontal_airflow_select_ = sel; +// if (this->horizontal_airflow_select_ != nullptr) { +// this->horizontal_airflow_select_->publish_state((uint8_t) this->settings_.last_horizontal_swing); +// } +} +#endif // USE_SELECT + 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_; diff --git a/components/haier/hon_climate.h b/components/haier/hon_climate.h index 58173f8..07e4606 100644 --- a/components/haier/hon_climate.h +++ b/components/haier/hon_climate.h @@ -13,6 +13,9 @@ #ifdef USE_SWITCH #include "esphome/components/switch/switch.h" #endif +#ifdef USE_SELECT +#include "esphome/components/select/select.h" +#endif #include "esphome/core/automation.h" #include "haier_base.h" #include "hon_packet.h" @@ -100,6 +103,15 @@ class HonClimate : public HaierClimateBase { protected: switch_::Switch *beeper_switch_{nullptr}; switch_::Switch *quiet_mode_switch_{nullptr}; +#endif +#ifdef USE_SELECT +public: + void set_vertical_airflow_select(select::Select *sel); + void set_horizontal_airflow_select(select::Select *sel); + + protected: + select::Select *vertical_airflow_select_{nullptr}; + select::Select *horizontal_airflow_select_{nullptr}; #endif public: HonClimate(); diff --git a/components/haier/select/__init__.py b/components/haier/select/__init__.py index f94ff40..139b997 100644 --- a/components/haier/select/__init__.py +++ b/components/haier/select/__init__.py @@ -2,12 +2,14 @@ import esphome.config_validation as cv from esphome.components import select from esphome.const import ( + CONF_OPTIONS, ENTITY_CATEGORY_CONFIG, ) from ..climate import ( CONF_HAIER_ID, HonClimate, haier_ns, + hon_protocol_ns, CONF_VERTICAL_AIRFLOW, CONF_HORIZONTAL_AIRFLOW, ) @@ -16,29 +18,40 @@ VerticalAirflowSelect = haier_ns.class_("VerticalAirflowSelect", select.Select) HorizontalAirflowSelect = haier_ns.class_("HorizontalAirflowSelect", select.Select) +VerticalSwingMode = hon_protocol_ns.enum("VerticalSwingMode", True) +HorizontalSwingMode = hon_protocol_ns.enum("HorizontalSwingMode", True) + # Additional icons ICON_ARROW_HORIZONTAL = "mdi:arrow-expand-horizontal" ICON_ARROW_VERTICAL = "mdi:arrow-expand-vertical" -AIRFLOW_VERTICAL_DIRECTION_OPTIONS = [ - "Auto", - "Health Up", - "Max Up", - "Up", - "Center", - "Down", - "Max Down", - "Health Down", -] +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_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, +} -AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = [ - "Auto", - "Max Left", - "Left", - "Center", - "Right", - "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 CONFIG_SCHEMA = cv.Schema( { @@ -60,15 +73,13 @@ 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): - pass - #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)) + sel_var = await select.new_select(conf, options=list(AIRFLOW_VERTICAL_DIRECTION_OPTIONS.keys())) + 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): - pass - #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)) + sel_var = await select.new_select(conf, options=list(AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS.keys())) + 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 new file mode 100644 index 0000000..4031f0f --- /dev/null +++ b/components/haier/select/horizontal_airflow.cpp @@ -0,0 +1,33 @@ +#include "horizontal_airflow.h" +#include + +namespace esphome { +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 { + ESP_LOGE("haier", "Invalid horizontal airflow mode: %s", value.c_str()); + return; + } + 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); + } + this->publish_state(value); +} + +} // namespace haier +} // namespace esphome diff --git a/components/haier/select/horizontal_airflow.h b/components/haier/select/horizontal_airflow.h new file mode 100644 index 0000000..b707bdf --- /dev/null +++ b/components/haier/select/horizontal_airflow.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/select/select.h" +#include "../hon_climate.h" + +namespace esphome { +namespace haier { + +class HorizontalAirflowSelect : public select::Select, public Parented { + public: + HorizontalAirflowSelect() = default; + + protected: + void control(const std::string &value) override; +}; + +} // namespace haier +} // namespace esphome diff --git a/components/haier/select/vertical_airflow.cpp b/components/haier/select/vertical_airflow.cpp new file mode 100644 index 0000000..dac0dc9 --- /dev/null +++ b/components/haier/select/vertical_airflow.cpp @@ -0,0 +1,12 @@ +#include "vertical_airflow.h" +#include + +namespace esphome { +namespace haier { + +void VerticalAirflowSelect::control(const std::string &value) { + this->publish_state(value); +} + +} // namespace haier +} // namespace esphome diff --git a/components/haier/select/vertical_airflow.h b/components/haier/select/vertical_airflow.h new file mode 100644 index 0000000..cfaf745 --- /dev/null +++ b/components/haier/select/vertical_airflow.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/select/select.h" +#include "../hon_climate.h" + +namespace esphome { +namespace haier { + +class VerticalAirflowSelect : public select::Select, public Parented { + public: + VerticalAirflowSelect() = default; + + protected: + void control(const std::string &value) override; +}; + +} // namespace haier +} // namespace esphome