ESPHome  2024.12.2
automation.h
Go to the documentation of this file.
1 #pragma once
3 #include "nextion.h"
4 
5 namespace esphome {
6 namespace nextion {
7 
8 class BufferOverflowTrigger : public Trigger<> {
9  public:
10  explicit BufferOverflowTrigger(Nextion *nextion) {
11  nextion->add_buffer_overflow_event_callback([this]() { this->trigger(); });
12  }
13 };
14 
15 class SetupTrigger : public Trigger<> {
16  public:
17  explicit SetupTrigger(Nextion *nextion) {
18  nextion->add_setup_state_callback([this]() { this->trigger(); });
19  }
20 };
21 
22 class SleepTrigger : public Trigger<> {
23  public:
24  explicit SleepTrigger(Nextion *nextion) {
25  nextion->add_sleep_state_callback([this]() { this->trigger(); });
26  }
27 };
28 
29 class WakeTrigger : public Trigger<> {
30  public:
31  explicit WakeTrigger(Nextion *nextion) {
32  nextion->add_wake_state_callback([this]() { this->trigger(); });
33  }
34 };
35 
36 class PageTrigger : public Trigger<uint8_t> {
37  public:
38  explicit PageTrigger(Nextion *nextion) {
39  nextion->add_new_page_callback([this](const uint8_t page_id) { this->trigger(page_id); });
40  }
41 };
42 
43 class TouchTrigger : public Trigger<uint8_t, uint8_t, bool> {
44  public:
45  explicit TouchTrigger(Nextion *nextion) {
46  nextion->add_touch_event_callback([this](uint8_t page_id, uint8_t component_id, bool touch_event) {
47  this->trigger(page_id, component_id, touch_event);
48  });
49  }
50 };
51 
52 template<typename... Ts> class NextionPublishFloatAction : public Action<Ts...> {
53  public:
54  explicit NextionPublishFloatAction(NextionComponent *component) : component_(component) {}
55 
56  TEMPLATABLE_VALUE(float, state)
57  TEMPLATABLE_VALUE(bool, publish_state)
58  TEMPLATABLE_VALUE(bool, send_to_nextion)
59 
60  void play(Ts... x) override {
61  this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
62  this->send_to_nextion_.value(x...));
63  }
64 
65  void set_state(std::function<void(Ts..., float)> state) { this->state_ = state; }
66  void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
67  void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
68  this->send_to_nextion_ = send_to_nextion;
69  }
70 
71  protected:
73 };
74 
75 template<typename... Ts> class NextionPublishTextAction : public Action<Ts...> {
76  public:
77  explicit NextionPublishTextAction(NextionComponent *component) : component_(component) {}
78 
79  TEMPLATABLE_VALUE(const char *, state)
80  TEMPLATABLE_VALUE(bool, publish_state)
81  TEMPLATABLE_VALUE(bool, send_to_nextion)
82 
83  void play(Ts... x) override {
84  this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
85  this->send_to_nextion_.value(x...));
86  }
87 
88  void set_state(std::function<void(Ts..., const char *)> state) { this->state_ = state; }
89  void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
90  void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
91  this->send_to_nextion_ = send_to_nextion;
92  }
93 
94  protected:
96 };
97 
98 template<typename... Ts> class NextionPublishBoolAction : public Action<Ts...> {
99  public:
100  explicit NextionPublishBoolAction(NextionComponent *component) : component_(component) {}
101 
102  TEMPLATABLE_VALUE(bool, state)
103  TEMPLATABLE_VALUE(bool, publish_state)
104  TEMPLATABLE_VALUE(bool, send_to_nextion)
105 
106  void play(Ts... x) override {
107  this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
108  this->send_to_nextion_.value(x...));
109  }
110 
111  void set_state(std::function<void(Ts..., bool)> state) { this->state_ = state; }
112  void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
113  void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
114  this->send_to_nextion_ = send_to_nextion;
115  }
116 
117  protected:
119 };
120 
121 } // namespace nextion
122 } // namespace esphome
void add_new_page_callback(std::function< void(uint8_t)> &&callback)
Add a callback to be notified when the nextion changes pages.
Definition: nextion.cpp:185
void set_publish_state(std::function< void(Ts..., bool)> publish_state)
Definition: automation.h:66
void add_wake_state_callback(std::function< void()> &&callback)
Add a callback to be notified of wake state changes.
Definition: nextion.cpp:177
uint16_t x
Definition: tt21100.cpp:17
void add_setup_state_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion completes its initialize setup.
Definition: nextion.cpp:181
void set_state(std::function< void(Ts..., const char *)> state)
Definition: automation.h:88
void add_buffer_overflow_event_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion reports a buffer overflow.
Definition: nextion.cpp:193
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void set_state(std::function< void(Ts..., float)> state)
Definition: automation.h:65
void add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
Definition: nextion.cpp:173
void set_send_to_nextion(std::function< void(Ts..., bool)> send_to_nextion)
Definition: automation.h:90
WakeTrigger(Nextion *nextion)
Definition: automation.h:31
void set_publish_state(std::function< void(Ts..., bool)> publish_state)
Definition: automation.h:112
NextionPublishBoolAction(NextionComponent *component)
Definition: automation.h:100
void set_send_to_nextion(std::function< void(Ts..., bool)> send_to_nextion)
Definition: automation.h:113
NextionPublishTextAction(NextionComponent *component)
Definition: automation.h:77
void set_state(std::function< void(Ts..., bool)> state)
Definition: automation.h:111
PageTrigger(Nextion *nextion)
Definition: automation.h:38
void add_touch_event_callback(std::function< void(uint8_t, uint8_t, bool)> &&callback)
Add a callback to be notified when Nextion has a touch event.
Definition: nextion.cpp:189
void set_publish_state(std::function< void(Ts..., bool)> publish_state)
Definition: automation.h:89
NextionPublishFloatAction(NextionComponent *component)
Definition: automation.h:54
SleepTrigger(Nextion *nextion)
Definition: automation.h:24
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
SetupTrigger(Nextion *nextion)
Definition: automation.h:17
TouchTrigger(Nextion *nextion)
Definition: automation.h:45
void set_send_to_nextion(std::function< void(Ts..., bool)> send_to_nextion)
Definition: automation.h:67
bool state
Definition: fan.h:34