From 36350f179eeb7625d390284b5c09701ed62a0dee Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 30 Jun 2025 10:49:59 -0500 Subject: [PATCH] split --- .../esp32_ble_tracker/esp32_ble_tracker.cpp | 8 +-- .../components/esphome/ota/ota_esphome.cpp | 2 +- .../http_request/ota/ota_http_request.cpp | 2 +- .../micro_wake_word/micro_wake_word.cpp | 10 ++-- esphome/components/ota/ota.cpp | 16 ++---- esphome/components/ota/ota.h | 53 ++++++++----------- esphome/components/ota_base/__init__.py | 5 ++ esphome/components/ota_base/ota_backend.cpp | 13 +++++ esphome/components/ota_base/ota_backend.h | 37 +++++++++++++ .../media_player/speaker_media_player.cpp | 10 ++-- 10 files changed, 95 insertions(+), 61 deletions(-) diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 290b2ead08..8e785da4be 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -18,7 +18,7 @@ #include #ifdef USE_OTA -#include "esphome/components/ota/ota.h" +#include "esphome/components/ota_base/ota_backend.h" #endif #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE @@ -61,9 +61,9 @@ void ESP32BLETracker::setup() { global_esp32_ble_tracker = this; #ifdef USE_OTA - ota::get_global_ota_callback()->add_on_state_callback( - [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { - if (state == ota::OTA_STARTED) { + ota_base::get_global_ota_callback()->add_on_state_callback( + [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { + if (state == ota_base::OTA_STARTED) { this->stop_scan(); for (auto *client : this->clients_) { client->disconnect(); diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index 5c662bbcfc..cfa8364059 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -24,7 +24,7 @@ static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; void ESPHomeOTAComponent::setup() { #ifdef USE_OTA_STATE_CALLBACK - ota::register_ota_platform(this); + ota_base::register_ota_platform(this); #endif this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections diff --git a/esphome/components/http_request/ota/ota_http_request.cpp b/esphome/components/http_request/ota/ota_http_request.cpp index ce7c5a6b88..57e65e6c03 100644 --- a/esphome/components/http_request/ota/ota_http_request.cpp +++ b/esphome/components/http_request/ota/ota_http_request.cpp @@ -20,7 +20,7 @@ static const char *const TAG = "http_request.ota"; void OtaHttpRequestComponent::setup() { #ifdef USE_OTA_STATE_CALLBACK - ota::register_ota_platform(this); + ota_base::register_ota_platform(this); #endif } diff --git a/esphome/components/micro_wake_word/micro_wake_word.cpp b/esphome/components/micro_wake_word/micro_wake_word.cpp index 5b5d92aa59..583a4b2fe2 100644 --- a/esphome/components/micro_wake_word/micro_wake_word.cpp +++ b/esphome/components/micro_wake_word/micro_wake_word.cpp @@ -9,7 +9,7 @@ #include "esphome/components/audio/audio_transfer_buffer.h" #ifdef USE_OTA -#include "esphome/components/ota/ota.h" +#include "esphome/components/ota_base/ota_backend.h" #endif namespace esphome { @@ -121,11 +121,11 @@ void MicroWakeWord::setup() { }); #ifdef USE_OTA - ota::get_global_ota_callback()->add_on_state_callback( - [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { - if (state == ota::OTA_STARTED) { + ota_base::get_global_ota_callback()->add_on_state_callback( + [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { + if (state == ota_base::OTA_STARTED) { this->suspend_task_(); - } else if (state == ota::OTA_ERROR) { + } else if (state == ota_base::OTA_ERROR) { this->resume_task_(); } }); diff --git a/esphome/components/ota/ota.cpp b/esphome/components/ota/ota.cpp index a98170ab1d..921f3769b9 100644 --- a/esphome/components/ota/ota.cpp +++ b/esphome/components/ota/ota.cpp @@ -3,18 +3,8 @@ namespace esphome { namespace ota { -#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 +// All functionality has been moved to ota_base +// This file remains for backward compatibility } // namespace ota -} // namespace esphome +} // namespace esphome \ No newline at end of file diff --git a/esphome/components/ota/ota.h b/esphome/components/ota/ota.h index c089cae006..654e87a173 100644 --- a/esphome/components/ota/ota.h +++ b/esphome/components/ota/ota.h @@ -1,52 +1,41 @@ #pragma once -#include "esphome/core/component.h" #include "esphome/core/defines.h" #include "esphome/components/ota_base/ota_backend.h" -#ifdef USE_OTA_STATE_CALLBACK -#include "esphome/core/automation.h" -#endif - namespace esphome { namespace ota { // Import types from ota_base namespace for backward compatibility using ota_base::OTABackend; +using ota_base::OTAComponent; using ota_base::OTAResponseTypes; using ota_base::OTAState; -class OTAComponent : public Component { -#ifdef USE_OTA_STATE_CALLBACK - public: - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -#endif -}; +// Re-export specific enum values for backward compatibility +// (in case external components use ota::OTA_STARTED, etc.) +static constexpr auto OTA_COMPLETED = ota_base::OTA_COMPLETED; +static constexpr auto OTA_STARTED = ota_base::OTA_STARTED; +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; #ifdef USE_OTA_STATE_CALLBACK -class 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 &&callback) { - this->state_callback_.add(std::move(callback)); - } +using ota_base::OTAGlobalCallback; - protected: - CallbackManager state_callback_{}; -}; +// Deprecated: Use ota_base::get_global_ota_callback() instead +// 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(); -void register_ota_platform(OTAComponent *ota_caller); +// Deprecated: Use ota_base::register_ota_platform() instead +// 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 } // namespace ota -} // namespace esphome +} // namespace esphome \ No newline at end of file diff --git a/esphome/components/ota_base/__init__.py b/esphome/components/ota_base/__init__.py index 1a562b3d2f..7a1f233d26 100644 --- a/esphome/components/ota_base/__init__.py +++ b/esphome/components/ota_base/__init__.py @@ -9,6 +9,11 @@ ota_base_ns = cg.esphome_ns.namespace("ota_base") @coroutine_with_priority(52.0) 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: cg.add_library("Update", None) diff --git a/esphome/components/ota_base/ota_backend.cpp b/esphome/components/ota_base/ota_backend.cpp index a2b2575f41..7cbc795866 100644 --- a/esphome/components/ota_base/ota_backend.cpp +++ b/esphome/components/ota_base/ota_backend.cpp @@ -5,5 +5,18 @@ namespace ota_base { // 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 esphome diff --git a/esphome/components/ota_base/ota_backend.h b/esphome/components/ota_base/ota_backend.h index b028f3605f..8e2831a063 100644 --- a/esphome/components/ota_base/ota_backend.h +++ b/esphome/components/ota_base/ota_backend.h @@ -1,8 +1,13 @@ #pragma once +#include "esphome/core/component.h" #include "esphome/core/defines.h" #include "esphome/core/helpers.h" +#ifdef USE_OTA_STATE_CALLBACK +#include "esphome/core/automation.h" +#endif + namespace esphome { namespace ota_base { @@ -56,5 +61,37 @@ class OTABackend { std::unique_ptr make_ota_backend(); +class OTAComponent : public Component { +#ifdef USE_OTA_STATE_CALLBACK + public: + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager 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 &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +}; + +OTAGlobalCallback *get_global_ota_callback(); +void register_ota_platform(OTAComponent *ota_caller); +#endif + } // namespace ota_base } // namespace esphome diff --git a/esphome/components/speaker/media_player/speaker_media_player.cpp b/esphome/components/speaker/media_player/speaker_media_player.cpp index 2cebebd523..c6f6c91760 100644 --- a/esphome/components/speaker/media_player/speaker_media_player.cpp +++ b/esphome/components/speaker/media_player/speaker_media_player.cpp @@ -6,7 +6,7 @@ #include "esphome/components/audio/audio.h" #ifdef USE_OTA -#include "esphome/components/ota/ota.h" +#include "esphome/components/ota_base/ota_backend.h" #endif namespace esphome { @@ -67,16 +67,16 @@ void SpeakerMediaPlayer::setup() { } #ifdef USE_OTA - ota::get_global_ota_callback()->add_on_state_callback( - [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { - if (state == ota::OTA_STARTED) { + ota_base::get_global_ota_callback()->add_on_state_callback( + [this](ota_base::OTAState state, float progress, uint8_t error, ota_base::OTAComponent *comp) { + if (state == ota_base::OTA_STARTED) { if (this->media_pipeline_ != nullptr) { this->media_pipeline_->suspend_tasks(); } if (this->announcement_pipeline_ != nullptr) { this->announcement_pipeline_->suspend_tasks(); } - } else if (state == ota::OTA_ERROR) { + } else if (state == ota_base::OTA_ERROR) { if (this->media_pipeline_ != nullptr) { this->media_pipeline_->resume_tasks(); }