ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ota_backend.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <cstdint>
8
9#ifdef USE_OTA_STATE_LISTENER
10#include <vector>
11#endif
12
13namespace esphome::ota {
14
51
59
65
72 public:
73 virtual ~OTAStateListener() = default;
74 virtual void on_ota_state(OTAState state, float progress, uint8_t error) = 0;
75};
76
77class OTAComponent : public Component {
78#ifdef USE_OTA_STATE_LISTENER
79 public:
80 void add_state_listener(OTAStateListener *listener) { this->state_listeners_.push_back(listener); }
81
82 protected:
83 void notify_state_(OTAState state, float progress, uint8_t error);
84
90 void notify_state_deferred_(OTAState state, float progress, uint8_t error);
91
92 std::vector<OTAStateListener *> state_listeners_;
93#endif
94};
95
96#ifdef USE_OTA_STATE_LISTENER
97
103 public:
104 virtual ~OTAGlobalStateListener() = default;
105 virtual void on_ota_global_state(OTAState state, float progress, uint8_t error, OTAComponent *component) = 0;
106};
107
114 public:
115 void add_global_state_listener(OTAGlobalStateListener *listener) { this->global_listeners_.push_back(listener); }
116
117 void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component) {
118 for (auto *listener : this->global_listeners_) {
119 listener->on_ota_global_state(state, progress, error, component);
120 }
121 }
122
123 protected:
124 std::vector<OTAGlobalStateListener *> global_listeners_;
125};
126
128
129// OTA implementations should use:
130// - notify_state_() when already in main loop (e.g., esphome OTA)
131// - notify_state_deferred_() when in separate task (e.g., web_server OTA)
132// This ensures proper listener execution in all contexts.
133#endif
134} // namespace esphome::ota
void notify_state_deferred_(OTAState state, float progress, uint8_t error)
Notify state with deferral to main loop (for thread safety).
void add_state_listener(OTAStateListener *listener)
Definition ota_backend.h:80
std::vector< OTAStateListener * > state_listeners_
Definition ota_backend.h:92
void notify_state_(OTAState state, float progress, uint8_t error)
Global callback that aggregates OTA state from all OTA components.
std::vector< OTAGlobalStateListener * > global_listeners_
void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component)
void add_global_state_listener(OTAGlobalStateListener *listener)
Listener interface for global OTA state changes (includes OTA component pointer).
virtual void on_ota_global_state(OTAState state, float progress, uint8_t error, OTAComponent *component)=0
virtual ~OTAGlobalStateListener()=default
Listener interface for OTA state changes.
Definition ota_backend.h:71
virtual ~OTAStateListener()=default
virtual void on_ota_state(OTAState state, float progress, uint8_t error)=0
const Component * component
Definition component.cpp:34
bool state
Definition fan.h:2
@ OTA_TYPE_UPDATE_BOOTLOADER
Definition ota_backend.h:63
@ OTA_TYPE_UPDATE_PARTITION_TABLE
Definition ota_backend.h:62
OTAGlobalCallback * get_global_ota_callback()
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Definition ota_backend.h:41
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
Definition ota_backend.h:39
@ OTA_RESPONSE_UPDATE_PREPARE_OK
Definition ota_backend.h:22
@ OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG
Definition ota_backend.h:37
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
Definition ota_backend.h:26
@ OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG
Definition ota_backend.h:36
@ OTA_RESPONSE_BIN_MD5_OK
Definition ota_backend.h:23
@ OTA_RESPONSE_UPDATE_END_OK
Definition ota_backend.h:25
@ OTA_RESPONSE_RECEIVE_OK
Definition ota_backend.h:24
@ OTA_RESPONSE_ERROR_BOOTLOADER_UPDATE
Definition ota_backend.h:48
@ OTA_RESPONSE_CHUNK_OK
Definition ota_backend.h:27
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:33
@ OTA_RESPONSE_FEATURE_FLAGS
Definition ota_backend.h:28
@ OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE
Definition ota_backend.h:38
@ OTA_RESPONSE_ERROR_PARTITION_TABLE_UPDATE
Definition ota_backend.h:46
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
Definition ota_backend.h:44
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:34
@ OTA_RESPONSE_ERROR_SIGNATURE_INVALID
Definition ota_backend.h:43
@ OTA_RESPONSE_ERROR_AUTH_INVALID
Definition ota_backend.h:32
@ OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE
Definition ota_backend.h:42
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:49
@ OTA_RESPONSE_REQUEST_SHA256_AUTH
Definition ota_backend.h:18
@ OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION
Definition ota_backend.h:40
@ OTA_RESPONSE_ERROR_MAGIC
Definition ota_backend.h:30
@ OTA_RESPONSE_ERROR_UPDATE_PREPARE
Definition ota_backend.h:31
@ OTA_RESPONSE_HEADER_OK
Definition ota_backend.h:20
@ OTA_RESPONSE_REQUEST_AUTH
Definition ota_backend.h:17
@ OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING
Definition ota_backend.h:35
@ OTA_RESPONSE_ERROR_PARTITION_TABLE_VERIFY
Definition ota_backend.h:45
@ OTA_RESPONSE_ERROR_BOOTLOADER_VERIFY
Definition ota_backend.h:47