ESPHome  2024.3.1
select_call.cpp
Go to the documentation of this file.
1 #include "select_call.h"
2 #include "select.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace select {
7 
8 static const char *const TAG = "select";
9 
10 SelectCall &SelectCall::set_option(const std::string &option) {
12 }
13 
15 
17 
19 
21 
23 
25  this->operation_ = operation;
26  return *this;
27 }
28 
30  this->cycle_ = cycle;
31  return *this;
32 }
33 
34 SelectCall &SelectCall::with_option(const std::string &option) {
35  this->option_ = option;
36  return *this;
37 }
38 
40  this->index_ = index;
41  return *this;
42 }
43 
45  auto *parent = this->parent_;
46  const auto *name = parent->get_name().c_str();
47  const auto &traits = parent->traits;
48  auto options = traits.get_options();
49 
50  if (this->operation_ == SELECT_OP_NONE) {
51  ESP_LOGW(TAG, "'%s' - SelectCall performed without selecting an operation", name);
52  return;
53  }
54  if (options.empty()) {
55  ESP_LOGW(TAG, "'%s' - Cannot perform SelectCall, select has no options", name);
56  return;
57  }
58 
59  std::string target_value;
60 
61  if (this->operation_ == SELECT_OP_SET) {
62  ESP_LOGD(TAG, "'%s' - Setting", name);
63  if (!this->option_.has_value()) {
64  ESP_LOGW(TAG, "'%s' - No option value set for SelectCall", name);
65  return;
66  }
67  target_value = this->option_.value();
68  } else if (this->operation_ == SELECT_OP_SET_INDEX) {
69  if (!this->index_.has_value()) {
70  ESP_LOGW(TAG, "'%s' - No index value set for SelectCall", name);
71  return;
72  }
73  if (this->index_.value() >= options.size()) {
74  ESP_LOGW(TAG, "'%s' - Index value %d out of bounds", name, this->index_.value());
75  return;
76  }
77  target_value = options[this->index_.value()];
78  } else if (this->operation_ == SELECT_OP_FIRST) {
79  target_value = options.front();
80  } else if (this->operation_ == SELECT_OP_LAST) {
81  target_value = options.back();
82  } else if (this->operation_ == SELECT_OP_NEXT || this->operation_ == SELECT_OP_PREVIOUS) {
83  auto cycle = this->cycle_;
84  ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name, this->operation_ == SELECT_OP_NEXT ? "next" : "previous",
85  cycle ? "" : "out");
86  if (!parent->has_state()) {
87  target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
88  } else {
89  auto index = parent->index_of(parent->state);
90  if (index.has_value()) {
91  auto size = options.size();
92  if (cycle) {
93  auto use_index = (size + index.value() + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
94  target_value = options[use_index];
95  } else {
96  if (this->operation_ == SELECT_OP_PREVIOUS && index.value() > 0) {
97  target_value = options[index.value() - 1];
98  } else if (this->operation_ == SELECT_OP_NEXT && index.value() < options.size() - 1) {
99  target_value = options[index.value() + 1];
100  } else {
101  return;
102  }
103  }
104  } else {
105  target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
106  }
107  }
108  }
109 
110  if (std::find(options.begin(), options.end(), target_value) == options.end()) {
111  ESP_LOGW(TAG, "'%s' - Option %s is not a valid option", name, target_value.c_str());
112  return;
113  }
114 
115  ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, target_value.c_str());
116  parent->control(target_value);
117 }
118 
119 } // namespace select
120 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
const char * name
Definition: stm32flash.h:78
SelectCall & select_next(bool cycle)
Definition: select_call.cpp:16
SelectCall & with_cycle(bool cycle)
Definition: select_call.cpp:29
SelectCall & set_index(size_t index)
Definition: select_call.cpp:14
SelectOperation operation_
Definition: select_call.h:42
SelectCall & with_option(const std::string &option)
Definition: select_call.cpp:34
bool has_value() const
Definition: optional.h:87
SelectCall & with_operation(SelectOperation operation)
Definition: select_call.cpp:24
SelectCall & with_index(size_t index)
Definition: select_call.cpp:39
SelectCall & select_previous(bool cycle)
Definition: select_call.cpp:18
uint8_t options
optional< size_t > index_
Definition: select_call.h:41
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
optional< std::string > option_
Definition: select_call.h:40
SelectCall & set_option(const std::string &option)
Definition: select_call.cpp:10