Skip to content

Commit 0f62010

Browse files
committed
Airflow control select improvements 2
1 parent 85fafc6 commit 0f62010

10 files changed

Lines changed: 86 additions & 57 deletions

File tree

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Turn off health mode.
291291
``climate.haier.set_vertical_airflow`` Action
292292
*********************************************
293293

294-
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Auto``, ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
294+
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
295295

296296
.. code-block:: yaml
297297
@@ -304,7 +304,7 @@ Turn off health mode.
304304
``climate.haier.set_horizontal_airflow`` Action
305305
***********************************************
306306

307-
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Auto``, ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
307+
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
308308

309309
.. code-block:: yaml
310310
@@ -538,9 +538,9 @@ Configuration variables:
538538
------------------------
539539

540540
- **haier_id** (**Required**, `ID <https://esphome.io/guides/configuration-types.html#config-id>`_): The id of Haier climate component
541-
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). The current state becomes ``Auto`` while horizontal swing is enabled.
541+
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). This select has no state while horizontal swing is enabled.
542542
All options from `Select <https://esphome.io/components/select/index.html#base-select-configuration>`_.
543-
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). The current state becomes ``Auto`` while vertical swing is enabled.
543+
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). This select has no state while vertical swing is enabled.
544544
All options from `Select <https://esphome.io/components/select/index.html#base-select-configuration>`_.
545545

546546
.. Generated from esphome-docs/switch/haier.rst

components/haier/hon_climate.cpp

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <chrono>
44
#include <string>
55
#include "esphome/components/climate/climate.h"
6+
#include "esphome/core/controller_registry.h"
67
#include "esphome/components/uart/uart.h"
78
#include "esphome/core/helpers.h"
89
#include "hon_climate.h"
@@ -16,7 +17,6 @@ namespace haier {
1617

1718
#ifdef USE_SELECT
1819
const std::vector<hon_protocol::VerticalSwingMode> VERTICAL_SWING_MODES_ORDER = {
19-
hon_protocol::VerticalSwingMode::AUTO,
2020
hon_protocol::VerticalSwingMode::HEALTH_UP,
2121
hon_protocol::VerticalSwingMode::MAX_UP,
2222
hon_protocol::VerticalSwingMode::UP,
@@ -27,38 +27,29 @@ const std::vector<hon_protocol::VerticalSwingMode> VERTICAL_SWING_MODES_ORDER =
2727
};
2828

2929
const std::vector<hon_protocol::HorizontalSwingMode> HORIZONTAL_SWING_MODES_ORDER = {
30-
hon_protocol::HorizontalSwingMode::AUTO,
3130
hon_protocol::HorizontalSwingMode::MAX_LEFT,
3231
hon_protocol::HorizontalSwingMode::LEFT,
3332
hon_protocol::HorizontalSwingMode::CENTER,
3433
hon_protocol::HorizontalSwingMode::RIGHT,
3534
hon_protocol::HorizontalSwingMode::MAX_RIGHT,
3635
};
3736

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"};
37+
static bool vertical_swing_mode_is_selectable_(hon_protocol::VerticalSwingMode mode) {
38+
return (mode != hon_protocol::VerticalSwingMode::AUTO) && (mode != hon_protocol::VerticalSwingMode::AUTO_SPECIAL);
39+
}
4240

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()];
41+
static bool horizontal_swing_mode_is_selectable_(hon_protocol::HorizontalSwingMode mode) {
42+
return mode != hon_protocol::HorizontalSwingMode::AUTO;
5343
}
5444

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];
45+
static void clear_airflow_select_state_(select::Select *sel) {
46+
if (sel == nullptr) {
47+
return;
6048
}
61-
return HORIZONTAL_SWING_MODE_OPTIONS[mode_it - HORIZONTAL_SWING_MODES_ORDER.begin()];
49+
sel->set_has_state(false);
50+
#if defined(USE_SELECT) && defined(USE_CONTROLLER_REGISTRY)
51+
ControllerRegistry::notify_select_update(sel);
52+
#endif
6253
}
6354
#endif // USE_SELECT
6455

@@ -872,17 +863,51 @@ void HonClimate::set_quiet_mode_switch(switch_::Switch *sw) {
872863
#ifdef USE_SELECT
873864
void HonClimate::set_vertical_airflow_select(select::Select *sel) {
874865
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-
}
866+
this->update_vertical_airflow_select_state_();
878867
}
879868

880869
void HonClimate::set_horizontal_airflow_select(select::Select *sel) {
881870
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()));
871+
this->update_horizontal_airflow_select_state_();
872+
}
873+
874+
void HonClimate::update_vertical_airflow_select_state_() {
875+
if (this->vertical_airflow_select_ == nullptr) {
876+
return;
885877
}
878+
if (!this->current_vertical_swing_.has_value() ||
879+
!vertical_swing_mode_is_selectable_(this->current_vertical_swing_.value())) {
880+
clear_airflow_select_state_(this->vertical_airflow_select_);
881+
return;
882+
}
883+
auto mode_it = std::find(VERTICAL_SWING_MODES_ORDER.begin(), VERTICAL_SWING_MODES_ORDER.end(),
884+
this->current_vertical_swing_.value());
885+
if (mode_it == VERTICAL_SWING_MODES_ORDER.end()) {
886+
ESP_LOGW(TAG, "Unsupported vertical airflow mode: 0x%X", static_cast<uint8_t>(this->current_vertical_swing_.value()));
887+
clear_airflow_select_state_(this->vertical_airflow_select_);
888+
return;
889+
}
890+
this->vertical_airflow_select_->publish_state(static_cast<size_t>(mode_it - VERTICAL_SWING_MODES_ORDER.begin()));
891+
}
892+
893+
void HonClimate::update_horizontal_airflow_select_state_() {
894+
if (this->horizontal_airflow_select_ == nullptr) {
895+
return;
896+
}
897+
if (!this->current_horizontal_swing_.has_value() ||
898+
!horizontal_swing_mode_is_selectable_(this->current_horizontal_swing_.value())) {
899+
clear_airflow_select_state_(this->horizontal_airflow_select_);
900+
return;
901+
}
902+
auto mode_it = std::find(HORIZONTAL_SWING_MODES_ORDER.begin(), HORIZONTAL_SWING_MODES_ORDER.end(),
903+
this->current_horizontal_swing_.value());
904+
if (mode_it == HORIZONTAL_SWING_MODES_ORDER.end()) {
905+
ESP_LOGW(TAG, "Unsupported horizontal airflow mode: 0x%X",
906+
static_cast<uint8_t>(this->current_horizontal_swing_.value()));
907+
clear_airflow_select_state_(this->horizontal_airflow_select_);
908+
return;
909+
}
910+
this->horizontal_airflow_select_->publish_state(static_cast<size_t>(mode_it - HORIZONTAL_SWING_MODES_ORDER.begin()));
886911
}
887912
#endif // USE_SELECT
888913

@@ -1131,13 +1156,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
11311156
this->hon_rtc_.save(&this->settings_);
11321157
}
11331158
#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-
}
1159+
this->update_vertical_airflow_select_state_();
1160+
this->update_horizontal_airflow_select_state_();
11411161
#endif // USE_SELECT
11421162
should_publish = should_publish || (old_swing_mode != this->swing_mode);
11431163
}

components/haier/hon_climate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class HonClimate : public HaierClimateBase {
116116
void set_horizontal_airflow_select(select::Select *sel);
117117

118118
protected:
119+
void update_vertical_airflow_select_state_();
120+
void update_horizontal_airflow_select_state_();
119121
select::Select *vertical_airflow_select_{nullptr};
120122
select::Select *horizontal_airflow_select_{nullptr};
121123
#endif

components/haier/select/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
ICON_ARROW_VERTICAL = "mdi:arrow-expand-vertical"
2222

2323
AIRFLOW_VERTICAL_DIRECTION_OPTIONS = [
24-
"Auto",
2524
"Health Up",
2625
"Max Up",
2726
"Up",
@@ -32,7 +31,6 @@
3231
]
3332

3433
AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = [
35-
"Auto",
3634
"Max Left",
3735
"Left",
3836
"Center",

docs/esphome-docs/climate/haier.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Turn off health mode.
291291
``climate.haier.set_vertical_airflow`` Action
292292
*********************************************
293293

294-
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Auto``, ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
294+
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
295295

296296
.. code-block:: yaml
297297
@@ -304,7 +304,7 @@ Turn off health mode.
304304
``climate.haier.set_horizontal_airflow`` Action
305305
***********************************************
306306

307-
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Auto``, ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
307+
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
308308

309309
.. code-block:: yaml
310310

docs/esphome-docs/select/haier.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Configuration variables:
3333
------------------------
3434

3535
- **haier_id** (**Required**, :ref:`config-id`): The id of Haier climate component
36-
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). The current state becomes ``Auto`` while horizontal swing is enabled.
36+
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). This select has no state while horizontal swing is enabled.
3737
All options from :ref:`Select <config-select>`.
38-
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). The current state becomes ``Auto`` while vertical swing is enabled.
38+
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). This select has no state while vertical swing is enabled.
3939
All options from :ref:`Select <config-select>`.
4040

4141
See Also

docs/examples/climate_haier.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Turn off health mode.
242242
``climate.haier.set_vertical_airflow`` Action
243243
*********************************************
244244

245-
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Auto``, ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
245+
(supported only by hOn) Set direction for vertical airflow if the vertical swing is disabled. Possible values: ``Health_Up``, ``Max_Up``, ``Up``, ``Center``, ``Down``, ``Max_Down``, ``Health_Down``.
246246

247247
.. code-block:: yaml
248248
@@ -255,7 +255,7 @@ Turn off health mode.
255255
``climate.haier.set_horizontal_airflow`` Action
256256
***********************************************
257257

258-
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Auto``, ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
258+
(supported only by hOn) Set direction for horizontal airflow if the horizontal swing is disabled. Possible values: ``Max_Left``, ``Left``, ``Center``, ``Right``, ``Max_Right``.
259259

260260
.. code-block:: yaml
261261

docs/examples/select_haier.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Configuration variables:
1919
------------------------
2020

2121
- **haier_id** (**Required**, :ref:`config-id`): The id of Haier climate component
22-
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). The current state becomes ``Auto`` while horizontal swing is enabled.
22+
- **horizontal_airflow** (*Optional*): (supported only by hOn) Select component to control horizontal airflow mode (if supported by AC). This select has no state while horizontal swing is enabled.
2323
All options from :ref:`Select <config-select>`.
24-
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). The current state becomes ``Auto`` while vertical swing is enabled.
24+
- **vertical_airflow** (*Optional*): (supported only by hOn) Select component to control vertical airflow mode (if supported by AC). This select has no state while vertical swing is enabled.
2525
All options from :ref:`Select <config-select>`.
2626

2727
See Also

docs/update_docs.cmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ echo Updaiting ESPHome haier climate doc
66
echo ===============================================================================================================
77
python %base_path%script/process_examples.py %base_path%examples/climate_haier.rst %base_path%esphome-docs/climate/haier.rst no_header
88
echo ===============================================================================================================
9-
echo Updaiting README.rst
10-
echo ===============================================================================================================
11-
python %base_path%script/make_doc.py README.rst
12-
echo ===============================================================================================================
139
echo Updaiting ESPHome haier select doc
1410
echo ===============================================================================================================
1511
python %base_path%script/process_examples.py %base_path%examples/select_haier.rst %base_path%esphome-docs/select/haier.rst no_header
1612
echo ===============================================================================================================
13+
echo Updaiting README.rst
14+
echo ===============================================================================================================
15+
python %base_path%script/make_doc.py README.rst
16+
echo ===============================================================================================================
1717
echo Updaiting docs/hon_example.rst
1818
echo ===============================================================================================================
1919
python %base_path%script/process_examples.py %base_path%examples/hon_example.rst %base_path%hon_example.rst

tests/.simple-hon.yaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
climate:
2-
- platform: haier
3-
protocol: hon
4-
name: ${device_name}
1+
climate:
2+
- platform: haier
3+
protocol: hon
4+
id: haier_ac
5+
name: ${device_name}
6+
7+
select:
8+
- platform: haier
9+
haier_id: haier_ac
10+
vertical_airflow:
11+
name: ${device_name} airflow vertical
12+
horizontal_airflow:
13+
name: ${device_name} airflow horizontal

0 commit comments

Comments
 (0)