mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 19:44:08 +00:00
[select] Cover select.is in the component test (#20112)
This commit is contained in:
@@ -33,6 +33,20 @@ button:
|
||||
cycle: false
|
||||
- select.first: select_test_select
|
||||
- select.last: select_test_select
|
||||
- if:
|
||||
condition:
|
||||
select.is:
|
||||
id: select_test_select
|
||||
options: [one, two]
|
||||
then:
|
||||
- select.next: select_test_select
|
||||
- if:
|
||||
condition:
|
||||
select.is:
|
||||
id: select_test_select
|
||||
lambda: return current == "three";
|
||||
then:
|
||||
- select.first: select_test_select
|
||||
- select.operation:
|
||||
id: select_test_select
|
||||
operation: next
|
||||
@@ -41,3 +55,47 @@ button:
|
||||
id: select_test_select
|
||||
operation: !lambda return SELECT_OP_PREVIOUS;
|
||||
cycle: !lambda return true;
|
||||
- platform: template
|
||||
name: "Test Select Options Lambdas"
|
||||
on_press:
|
||||
- lambda: |-
|
||||
auto &options = id(select_test_select).traits.get_options();
|
||||
for (size_t i = 0; i < options.size(); i++)
|
||||
ESP_LOGD("test", "%s", options[i]);
|
||||
- lambda: |-
|
||||
std::string wanted = "two";
|
||||
const auto &opts = id(select_test_select).traits.get_options();
|
||||
if (opts.empty())
|
||||
return;
|
||||
for (int i = 0; i < (int) opts.size(); i++) {
|
||||
if (opts[i] == wanted)
|
||||
ESP_LOGD("test", "found at %d", i);
|
||||
}
|
||||
if (id(select_test_select).traits.get_options()[0] == wanted)
|
||||
ESP_LOGD("test", "first");
|
||||
auto it = std::find(opts.begin(), opts.end(), std::string("three"));
|
||||
ESP_LOGD("test", "index %d", (int) (it - opts.begin()));
|
||||
- lambda: |-
|
||||
static std::vector<std::string> names{"red", "green"};
|
||||
FixedVector<const char *> fresh;
|
||||
fresh.init(names.size());
|
||||
for (auto &name : names)
|
||||
fresh.push_back(name.c_str());
|
||||
if (id(select_test_select).traits.get_options().size() != fresh.size())
|
||||
id(select_test_select).traits.set_options(fresh);
|
||||
id(select_test_select).traits.set_options({"one", "two", "three"});
|
||||
- lambda: |-
|
||||
const char *value = "two";
|
||||
const auto &options = id(select_test_select).traits.get_options();
|
||||
const auto *it = std::find_if(options.begin(), options.end(),
|
||||
[value](const char *option) { return strcmp(option, value) == 0; });
|
||||
if (it == options.end())
|
||||
return;
|
||||
uint32_t index = (uint32_t) (it - options.begin());
|
||||
ESP_LOGD("test", "index %u", (unsigned) index);
|
||||
const auto *it2 = std::find(options.begin(), options.end(), options[0]);
|
||||
ESP_LOGD("test", "first at %d", (int) (it2 - options.begin()));
|
||||
- lambda: |-
|
||||
auto &options = id(select_test_select).traits.get_options();
|
||||
for (size_t i = 0; i < options.size(); i++)
|
||||
ESP_LOGD("test", "%u: %s", (unsigned) i, options.at(i));
|
||||
|
||||
Reference in New Issue
Block a user