This commit is contained in:
J. Nick Koston 2025-06-30 10:49:59 -05:00
parent 902f08c1bc
commit 36350f179e
No known key found for this signature in database
10 changed files with 95 additions and 61 deletions

View File

@ -18,7 +18,7 @@
#include <cinttypes>
#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();

View File

@ -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

View File

@ -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
}

View File

@ -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_();
}
});

View File

@ -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

View File

@ -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<void(ota_base::OTAState, float, uint8_t)> &&callback) {
this->state_callback_.add(std::move(callback));
}
protected:
CallbackManager<void(ota_base::OTAState, float, uint8_t)> 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<void(ota_base::OTAState, float, uint8_t, OTAComponent *)> &&callback) {
this->state_callback_.add(std::move(callback));
}
using ota_base::OTAGlobalCallback;
protected:
CallbackManager<void(ota_base::OTAState, float, uint8_t, OTAComponent *)> 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

View File

@ -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)

View File

@ -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

View File

@ -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<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 esphome

View File

@ -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();
}