ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "select.h"
6
7namespace esphome::select {
8
9class SelectStateTrigger : public Trigger<StringRef, size_t> {
10 public:
11 explicit SelectStateTrigger(Select *parent) : parent_(parent) {
13 [this](size_t index) { this->trigger(StringRef(this->parent_->option_at(index)), index); });
14 }
15
16 protected:
18};
19
20template<typename... Ts> class SelectSetAction : public Action<Ts...> {
21 public:
22 explicit SelectSetAction(Select *select) : select_(select) {}
23 TEMPLATABLE_VALUE(std::string, option)
24
25 void play(const Ts &...x) override {
26 auto call = this->select_->make_call();
27 call.set_option(this->option_.value(x...));
28 call.perform();
29 }
30
31 protected:
33};
34
35template<typename... Ts> class SelectSetIndexAction : public Action<Ts...> {
36 public:
37 explicit SelectSetIndexAction(Select *select) : select_(select) {}
38 TEMPLATABLE_VALUE(size_t, index)
39
40 void play(const Ts &...x) override {
41 auto call = this->select_->make_call();
42 call.set_index(this->index_.value(x...));
43 call.perform();
44 }
45
46 protected:
48};
49
50template<typename... Ts> class SelectOperationAction : public Action<Ts...> {
51 public:
52 explicit SelectOperationAction(Select *select) : select_(select) {}
53 TEMPLATABLE_VALUE(bool, cycle)
55
56 void play(const Ts &...x) override {
57 auto call = this->select_->make_call();
58 call.with_operation(this->operation_.value(x...));
59 if (this->cycle_.has_value()) {
60 call.with_cycle(this->cycle_.value(x...));
61 }
62 call.perform();
63 }
64
65 protected:
67};
68
69template<size_t N, typename... Ts> class SelectIsCondition : public Condition<Ts...> {
70 public:
71 SelectIsCondition(Select *parent, const char *const *option_list) : parent_(parent), option_list_(option_list) {}
72
73 bool check(const Ts &...x) override {
74 auto current = this->parent_->current_option();
75 for (size_t i = 0; i != N; i++) {
76 if (current == this->option_list_[i]) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 protected:
85 const char *const *option_list_;
86};
87
88template<typename... Ts> class SelectIsCondition<0, Ts...> : public Condition<Ts...> {
89 public:
90 SelectIsCondition(Select *parent, std::function<bool(const StringRef &, const Ts &...)> &&f)
91 : parent_(parent), f_(f) {}
92
93 bool check(const Ts &...x) override { return this->f_(this->parent_->current_option(), x...); }
94
95 protected:
97 std::function<bool(const StringRef &, const Ts &...)> f_;
98};
99} // namespace esphome::select
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:304
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
SelectCall & with_operation(SelectOperation operation)
SelectCall & set_index(size_t index)
SelectCall & set_option(const char *option, size_t len)
Base-class for all selects.
Definition select.h:29
const char * option_at(size_t index) const
Return the option value at the provided index offset (as const char* from flash).
Definition select.cpp:85
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
void add_on_state_callback(std::function< void(size_t)> &&callback)
Definition select.cpp:45
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:41
SelectIsCondition(Select *parent, std::function< bool(const StringRef &, const Ts &...)> &&f)
Definition automation.h:90
bool check(const Ts &...x) override
Definition automation.h:93
std::function< bool(const StringRef &, const Ts &...)> f_
Definition automation.h:97
bool check(const Ts &...x) override
Definition automation.h:73
const char *const * option_list_
Definition automation.h:85
SelectIsCondition(Select *parent, const char *const *option_list)
Definition automation.h:71
TEMPLATABLE_VALUE(bool, cycle) TEMPLATABLE_VALUE(SelectOperation
operation void play(const Ts &...x) override
Definition automation.h:56
TEMPLATABLE_VALUE(std::string, option) void play(const Ts &...x) override
Definition automation.h:23
TEMPLATABLE_VALUE(size_t, index) void play(const Ts &...x) override
Definition automation.h:38
if(written< 0)
Definition helpers.h:863
uint16_t x
Definition tt21100.cpp:5