mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 23:21:54 +00:00 
			
		
		
		
	split
This commit is contained in:
		| @@ -18,7 +18,7 @@ | |||||||
| #include <cinttypes> | #include <cinttypes> | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
| #include "esphome/components/ota/ota.h" | #include "esphome/components/ota_base/ota_backend.h" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE | #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE | ||||||
| @@ -61,9 +61,9 @@ void ESP32BLETracker::setup() { | |||||||
|   global_esp32_ble_tracker = this; |   global_esp32_ble_tracker = this; | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
|   ota::get_global_ota_callback()->add_on_state_callback( |   ota_base::get_global_ota_callback()->add_on_state_callback( | ||||||
|       [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { |       [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { | ||||||
|         if (state == ota::OTA_STARTED) { |         if (state == ota_base::OTA_STARTED) { | ||||||
|           this->stop_scan(); |           this->stop_scan(); | ||||||
|           for (auto *client : this->clients_) { |           for (auto *client : this->clients_) { | ||||||
|             client->disconnect(); |             client->disconnect(); | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; | |||||||
|  |  | ||||||
| void ESPHomeOTAComponent::setup() { | void ESPHomeOTAComponent::setup() { | ||||||
| #ifdef USE_OTA_STATE_CALLBACK | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|   ota::register_ota_platform(this); |   ota_base::register_ota_platform(this); | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0);  // monitored for incoming connections |   this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0);  // monitored for incoming connections | ||||||
|   | |||||||
| @@ -20,7 +20,7 @@ static const char *const TAG = "http_request.ota"; | |||||||
|  |  | ||||||
| void OtaHttpRequestComponent::setup() { | void OtaHttpRequestComponent::setup() { | ||||||
| #ifdef USE_OTA_STATE_CALLBACK | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|   ota::register_ota_platform(this); |   ota_base::register_ota_platform(this); | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ | |||||||
| #include "esphome/components/audio/audio_transfer_buffer.h" | #include "esphome/components/audio/audio_transfer_buffer.h" | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
| #include "esphome/components/ota/ota.h" | #include "esphome/components/ota_base/ota_backend.h" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -121,11 +121,11 @@ void MicroWakeWord::setup() { | |||||||
|   }); |   }); | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
|   ota::get_global_ota_callback()->add_on_state_callback( |   ota_base::get_global_ota_callback()->add_on_state_callback( | ||||||
|       [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { |       [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { | ||||||
|         if (state == ota::OTA_STARTED) { |         if (state == ota_base::OTA_STARTED) { | ||||||
|           this->suspend_task_(); |           this->suspend_task_(); | ||||||
|         } else if (state == ota::OTA_ERROR) { |         } else if (state == ota_base::OTA_ERROR) { | ||||||
|           this->resume_task_(); |           this->resume_task_(); | ||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
|   | |||||||
| @@ -3,18 +3,8 @@ | |||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace ota { | namespace ota { | ||||||
|  |  | ||||||
| #ifdef USE_OTA_STATE_CALLBACK | // All functionality has been moved to ota_base | ||||||
| OTAGlobalCallback *global_ota_callback{nullptr};  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | // This file remains for backward compatibility | ||||||
|  |  | ||||||
| OTAGlobalCallback *get_global_ota_callback() { |  | ||||||
|   if (global_ota_callback == nullptr) { |  | ||||||
|     global_ota_callback = new OTAGlobalCallback();  // NOLINT(cppcoreguidelines-owning-memory) |  | ||||||
|   } |  | ||||||
|   return global_ota_callback; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void register_ota_platform(OTAComponent *ota_caller) { get_global_ota_callback()->register_ota(ota_caller); } |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| }  // namespace ota | }  // namespace ota | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
| @@ -1,51 +1,40 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" |  | ||||||
| #include "esphome/core/defines.h" | #include "esphome/core/defines.h" | ||||||
| #include "esphome/components/ota_base/ota_backend.h" | #include "esphome/components/ota_base/ota_backend.h" | ||||||
|  |  | ||||||
| #ifdef USE_OTA_STATE_CALLBACK |  | ||||||
| #include "esphome/core/automation.h" |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace ota { | namespace ota { | ||||||
|  |  | ||||||
| // Import types from ota_base namespace for backward compatibility | // Import types from ota_base namespace for backward compatibility | ||||||
| using ota_base::OTABackend; | using ota_base::OTABackend; | ||||||
|  | using ota_base::OTAComponent; | ||||||
| using ota_base::OTAResponseTypes; | using ota_base::OTAResponseTypes; | ||||||
| using ota_base::OTAState; | using ota_base::OTAState; | ||||||
|  |  | ||||||
| class OTAComponent : public Component { | // Re-export specific enum values for backward compatibility | ||||||
| #ifdef USE_OTA_STATE_CALLBACK | // (in case external components use ota::OTA_STARTED, etc.) | ||||||
|  public: | static constexpr auto OTA_COMPLETED = ota_base::OTA_COMPLETED; | ||||||
|   void add_on_state_callback(std::function<void(ota_base::OTAState, float, uint8_t)> &&callback) { | static constexpr auto OTA_STARTED = ota_base::OTA_STARTED; | ||||||
|     this->state_callback_.add(std::move(callback)); | static constexpr auto OTA_IN_PROGRESS = ota_base::OTA_IN_PROGRESS; | ||||||
|   } | static constexpr auto OTA_ABORT = ota_base::OTA_ABORT; | ||||||
|  | static constexpr auto OTA_ERROR = ota_base::OTA_ERROR; | ||||||
|  protected: |  | ||||||
|   CallbackManager<void(ota_base::OTAState, float, uint8_t)> state_callback_{}; |  | ||||||
| #endif |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| #ifdef USE_OTA_STATE_CALLBACK | #ifdef USE_OTA_STATE_CALLBACK | ||||||
| class OTAGlobalCallback { | using ota_base::OTAGlobalCallback; | ||||||
|  public: |  | ||||||
|   void register_ota(OTAComponent *ota_caller) { |  | ||||||
|     ota_caller->add_on_state_callback([this, ota_caller](ota_base::OTAState state, float progress, uint8_t error) { |  | ||||||
|       this->state_callback_.call(state, progress, error, ota_caller); |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
|   void add_on_state_callback(std::function<void(ota_base::OTAState, float, uint8_t, OTAComponent *)> &&callback) { |  | ||||||
|     this->state_callback_.add(std::move(callback)); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|  protected: | // Deprecated: Use ota_base::get_global_ota_callback() instead | ||||||
|   CallbackManager<void(ota_base::OTAState, float, uint8_t, OTAComponent *)> state_callback_{}; | // Will be removed after 2025-12-30 (6 months from 2025-06-30) | ||||||
| }; | [[deprecated("Use ota_base::get_global_ota_callback() instead")]] inline OTAGlobalCallback *get_global_ota_callback() { | ||||||
|  |   return ota_base::get_global_ota_callback(); | ||||||
|  | } | ||||||
|  |  | ||||||
| OTAGlobalCallback *get_global_ota_callback(); | // Deprecated: Use ota_base::register_ota_platform() instead | ||||||
| void register_ota_platform(OTAComponent *ota_caller); | // Will be removed after 2025-12-30 (6 months from 2025-06-30) | ||||||
|  | [[deprecated("Use ota_base::register_ota_platform() instead")]] inline void register_ota_platform( | ||||||
|  |     OTAComponent *ota_caller) { | ||||||
|  |   ota_base::register_ota_platform(ota_caller); | ||||||
|  | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| }  // namespace ota | }  // namespace ota | ||||||
|   | |||||||
| @@ -9,6 +9,11 @@ ota_base_ns = cg.esphome_ns.namespace("ota_base") | |||||||
|  |  | ||||||
| @coroutine_with_priority(52.0) | @coroutine_with_priority(52.0) | ||||||
| async def to_code(config): | async def to_code(config): | ||||||
|  |     # Note: USE_OTA_STATE_CALLBACK is not defined here | ||||||
|  |     # Components that need OTA callbacks (like esp32_ble_tracker, speaker, etc.) | ||||||
|  |     # define USE_OTA_STATE_CALLBACK themselves in their own __init__.py files | ||||||
|  |     # This ensures the callback functionality is only compiled when actually needed | ||||||
|  |  | ||||||
|     if CORE.is_esp32 and CORE.using_arduino: |     if CORE.is_esp32 and CORE.using_arduino: | ||||||
|         cg.add_library("Update", None) |         cg.add_library("Update", None) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,5 +5,18 @@ namespace ota_base { | |||||||
|  |  | ||||||
| // The make_ota_backend() implementation is provided by each platform-specific backend | // The make_ota_backend() implementation is provided by each platform-specific backend | ||||||
|  |  | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  | OTAGlobalCallback *global_ota_callback{nullptr};  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | ||||||
|  |  | ||||||
|  | OTAGlobalCallback *get_global_ota_callback() { | ||||||
|  |   if (global_ota_callback == nullptr) { | ||||||
|  |     global_ota_callback = new OTAGlobalCallback();  // NOLINT(cppcoreguidelines-owning-memory) | ||||||
|  |   } | ||||||
|  |   return global_ota_callback; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void register_ota_platform(OTAComponent *ota_caller) { get_global_ota_callback()->register_ota(ota_caller); } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| }  // namespace ota_base | }  // namespace ota_base | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -1,8 +1,13 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/core/component.h" | ||||||
| #include "esphome/core/defines.h" | #include "esphome/core/defines.h" | ||||||
| #include "esphome/core/helpers.h" | #include "esphome/core/helpers.h" | ||||||
|  |  | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  | #include "esphome/core/automation.h" | ||||||
|  | #endif | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace ota_base { | namespace ota_base { | ||||||
|  |  | ||||||
| @@ -56,5 +61,37 @@ class OTABackend { | |||||||
|  |  | ||||||
| std::unique_ptr<OTABackend> make_ota_backend(); | std::unique_ptr<OTABackend> make_ota_backend(); | ||||||
|  |  | ||||||
|  | class OTAComponent : public Component { | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  |  public: | ||||||
|  |   void add_on_state_callback(std::function<void(OTAState, float, uint8_t)> &&callback) { | ||||||
|  |     this->state_callback_.add(std::move(callback)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   CallbackManager<void(OTAState, float, uint8_t)> state_callback_{}; | ||||||
|  | #endif | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  | class OTAGlobalCallback { | ||||||
|  |  public: | ||||||
|  |   void register_ota(OTAComponent *ota_caller) { | ||||||
|  |     ota_caller->add_on_state_callback([this, ota_caller](OTAState state, float progress, uint8_t error) { | ||||||
|  |       this->state_callback_.call(state, progress, error, ota_caller); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |   void add_on_state_callback(std::function<void(OTAState, float, uint8_t, OTAComponent *)> &&callback) { | ||||||
|  |     this->state_callback_.add(std::move(callback)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   CallbackManager<void(OTAState, float, uint8_t, OTAComponent *)> state_callback_{}; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | OTAGlobalCallback *get_global_ota_callback(); | ||||||
|  | void register_ota_platform(OTAComponent *ota_caller); | ||||||
|  | #endif | ||||||
|  |  | ||||||
| }  // namespace ota_base | }  // namespace ota_base | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| #include "esphome/components/audio/audio.h" | #include "esphome/components/audio/audio.h" | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
| #include "esphome/components/ota/ota.h" | #include "esphome/components/ota_base/ota_backend.h" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -67,16 +67,16 @@ void SpeakerMediaPlayer::setup() { | |||||||
|   } |   } | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
|   ota::get_global_ota_callback()->add_on_state_callback( |   ota_base::get_global_ota_callback()->add_on_state_callback( | ||||||
|       [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { |       [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { | ||||||
|         if (state == ota::OTA_STARTED) { |         if (state == ota_base::OTA_STARTED) { | ||||||
|           if (this->media_pipeline_ != nullptr) { |           if (this->media_pipeline_ != nullptr) { | ||||||
|             this->media_pipeline_->suspend_tasks(); |             this->media_pipeline_->suspend_tasks(); | ||||||
|           } |           } | ||||||
|           if (this->announcement_pipeline_ != nullptr) { |           if (this->announcement_pipeline_ != nullptr) { | ||||||
|             this->announcement_pipeline_->suspend_tasks(); |             this->announcement_pipeline_->suspend_tasks(); | ||||||
|           } |           } | ||||||
|         } else if (state == ota::OTA_ERROR) { |         } else if (state == ota_base::OTA_ERROR) { | ||||||
|           if (this->media_pipeline_ != nullptr) { |           if (this->media_pipeline_ != nullptr) { | ||||||
|             this->media_pipeline_->resume_tasks(); |             this->media_pipeline_->resume_tasks(); | ||||||
|           } |           } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user