Skip to content

Commit

Permalink
Select for haier implementation changed to keep order of item names a…
Browse files Browse the repository at this point in the history
…nd values only in one place
  • Loading branch information
Pavlo Dudnytskyi committed Oct 23, 2024
1 parent bbbbfbc commit 3afba23
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 59 deletions.
57 changes: 56 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 22 additions & 0 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ using namespace esphome::uart;
namespace esphome {
namespace haier {

#ifdef USE_SELECT
const std::vector<hon_protocol::VerticalSwingMode> 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<hon_protocol::HorizontalSwingMode> 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;
Expand Down
5 changes: 5 additions & 0 deletions components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
namespace esphome {
namespace haier {

#ifdef USE_SELECT
extern const std::vector<hon_protocol::HorizontalSwingMode> HORIZONTAL_SWING_MODES_ORDER;
extern const std::vector<hon_protocol::VerticalSwingMode> VERTICAL_SWING_MODES_ORDER;
#endif

enum class CleaningState : uint8_t {
NO_CLEANING = 0,
SELF_CLEAN = 1,
Expand Down
48 changes: 20 additions & 28 deletions components/haier/select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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))

Expand Down
17 changes: 4 additions & 13 deletions components/haier/select/horizontal_airflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<hon_protocol::HorizontalSwingMode> current_state = this->parent_->get_horizontal_airflow();
if (!current_state.has_value() || (current_state.value() != state)) {
this->parent_->set_horizontal_airflow(state);
Expand Down
21 changes: 4 additions & 17 deletions components/haier/select/vertical_airflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<hon_protocol::VerticalSwingMode> current_state = this->parent_->get_vertical_airflow();
if (!current_state.has_value() || (current_state.value() != state)) {
this->parent_->set_vertical_airflow(state);
Expand Down

0 comments on commit 3afba23

Please sign in to comment.