ESPHome  2024.12.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 #ifdef USE_OTA_STATE_CALLBACK
3 #include "ota_backend.h"
4 
6 
7 namespace esphome {
8 namespace ota {
9 
10 class OTAStateChangeTrigger : public Trigger<OTAState> {
11  public:
12  explicit OTAStateChangeTrigger(OTAComponent *parent) {
13  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
14  if (!parent->is_failed()) {
15  trigger(state);
16  }
17  });
18  }
19 };
20 
21 class OTAStartTrigger : public Trigger<> {
22  public:
23  explicit OTAStartTrigger(OTAComponent *parent) {
24  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
25  if (state == OTA_STARTED && !parent->is_failed()) {
26  trigger();
27  }
28  });
29  }
30 };
31 
32 class OTAProgressTrigger : public Trigger<float> {
33  public:
34  explicit OTAProgressTrigger(OTAComponent *parent) {
35  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
36  if (state == OTA_IN_PROGRESS && !parent->is_failed()) {
37  trigger(progress);
38  }
39  });
40  }
41 };
42 
43 class OTAEndTrigger : public Trigger<> {
44  public:
45  explicit OTAEndTrigger(OTAComponent *parent) {
46  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
47  if (state == OTA_COMPLETED && !parent->is_failed()) {
48  trigger();
49  }
50  });
51  }
52 };
53 
54 class OTAAbortTrigger : public Trigger<> {
55  public:
56  explicit OTAAbortTrigger(OTAComponent *parent) {
57  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
58  if (state == OTA_ABORT && !parent->is_failed()) {
59  trigger();
60  }
61  });
62  }
63 };
64 
65 class OTAErrorTrigger : public Trigger<uint8_t> {
66  public:
67  explicit OTAErrorTrigger(OTAComponent *parent) {
68  parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
69  if (state == OTA_ERROR && !parent->is_failed()) {
70  trigger(error);
71  }
72  });
73  }
74 };
75 
76 } // namespace ota
77 } // namespace esphome
78 #endif
bool is_failed() const
Definition: component.cpp:143
OTAProgressTrigger(OTAComponent *parent)
Definition: automation.h:34
OTAStartTrigger(OTAComponent *parent)
Definition: automation.h:23
void add_on_state_callback(std::function< void(ota::OTAState, float, uint8_t)> &&callback)
Definition: ota_backend.h:65
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
OTAErrorTrigger(OTAComponent *parent)
Definition: automation.h:67
OTAEndTrigger(OTAComponent *parent)
Definition: automation.h:45
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
OTAAbortTrigger(OTAComponent *parent)
Definition: automation.h:56
OTAStateChangeTrigger(OTAComponent *parent)
Definition: automation.h:12
bool state
Definition: fan.h:34