From b1a318c0d75682c2a5f7ad910868bdfae1f23e07 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 28 Nov 2025 14:07:55 -0600 Subject: [PATCH] simplify, no more register needed --- .../components/esphome/ota/ota_esphome.cpp | 4 --- .../http_request/ota/ota_http_request.cpp | 6 +--- esphome/components/ota/ota_backend.cpp | 9 ++--- esphome/components/ota/ota_backend.h | 36 +++---------------- .../web_server/ota/ota_web_server.cpp | 4 --- 5 files changed, 11 insertions(+), 48 deletions(-) diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index 469c57211c5..521b3de15a9 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -42,10 +42,6 @@ static constexpr size_t SHA256_HEX_SIZE = 64; // SHA256 hash as hex string (32 #endif // USE_OTA_PASSWORD void ESPHomeOTAComponent::setup() { -#ifdef USE_OTA_STATE_LISTENER - ota::register_ota_platform(this); -#endif - this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections if (this->server_ == nullptr) { this->log_socket_error_(LOG_STR("creation")); diff --git a/esphome/components/http_request/ota/ota_http_request.cpp b/esphome/components/http_request/ota/ota_http_request.cpp index 2a52a0e2648..59bdeb9ceba 100644 --- a/esphome/components/http_request/ota/ota_http_request.cpp +++ b/esphome/components/http_request/ota/ota_http_request.cpp @@ -17,11 +17,7 @@ namespace http_request { static const char *const TAG = "http_request.ota"; -void OtaHttpRequestComponent::setup() { -#ifdef USE_OTA_STATE_LISTENER - ota::register_ota_platform(this); -#endif -} +void OtaHttpRequestComponent::setup() {} void OtaHttpRequestComponent::dump_config() { ESP_LOGCONFIG(TAG, "Over-The-Air updates via HTTP request"); }; diff --git a/esphome/components/ota/ota_backend.cpp b/esphome/components/ota/ota_backend.cpp index 5f510b4f8bb..8fb9f672145 100644 --- a/esphome/components/ota/ota_backend.cpp +++ b/esphome/components/ota/ota_backend.cpp @@ -13,10 +13,11 @@ OTAGlobalCallback *get_global_ota_callback() { return global_ota_callback; } -void register_ota_platform(OTAComponent *ota_caller) { get_global_ota_callback()->register_ota(ota_caller); } - -void OTAComponentBridge::on_ota_state(OTAState state, float progress, uint8_t error) { - this->global_callback_->notify_global_listeners(state, progress, error, this->component_); +void OTAComponent::notify_state_(OTAState state, float progress, uint8_t error) { + for (auto *listener : this->state_listeners_) { + listener->on_ota_state(state, progress, error); + } + get_global_ota_callback()->notify_ota_state(state, progress, error, this); } #endif diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h index 64fbbcfda7b..c00ecba9e68 100644 --- a/esphome/components/ota/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -76,11 +76,7 @@ class OTAComponent : public Component { void add_state_listener(OTAStateListener *listener) { this->state_listeners_.push_back(listener); } protected: - void notify_state_(OTAState state, float progress, uint8_t error) { - for (auto *listener : this->state_listeners_) { - listener->on_ota_state(state, progress, error); - } - } + void notify_state_(OTAState state, float progress, uint8_t error); /** Notify state with deferral to main loop (for thread safety). * @@ -96,7 +92,6 @@ class OTAComponent : public Component { }; #ifdef USE_OTA_STATE_LISTENER -class OTAGlobalCallback; /** Listener interface for global OTA state changes (includes OTA component pointer). * @@ -107,36 +102,16 @@ class OTAGlobalStateListener { virtual void on_ota_global_state(OTAState state, float progress, uint8_t error, OTAComponent *component) = 0; }; -/** Helper class to bridge per-component OTA state to global listeners. +/** Global callback that aggregates OTA state from all OTA components. * - * Each OTA component gets one of these registered as a listener. When that - * component fires state events, this bridge forwards them to all global listeners - * along with the component pointer. + * OTA components call notify_ota_state() directly with their pointer, + * which forwards the event to all registered global listeners. */ -class OTAComponentBridge : public OTAStateListener { - public: - OTAComponentBridge(OTAGlobalCallback *global_callback, OTAComponent *component) - : global_callback_(global_callback), component_(component) {} - - void on_ota_state(OTAState state, float progress, uint8_t error) override; - - private: - OTAGlobalCallback *global_callback_; - OTAComponent *component_; -}; - class OTAGlobalCallback { public: - void register_ota(OTAComponent *ota_caller) { - // Create a bridge that forwards this component's events to global listeners. - // Intentionally never deleted - these objects live for the lifetime of the device. - auto *bridge = new OTAComponentBridge(this, ota_caller); // NOLINT(cppcoreguidelines-owning-memory) - ota_caller->add_state_listener(bridge); - } - void add_global_state_listener(OTAGlobalStateListener *listener) { this->global_listeners_.push_back(listener); } - void notify_global_listeners(OTAState state, float progress, uint8_t error, OTAComponent *component) { + void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component) { for (auto *listener : this->global_listeners_) { listener->on_ota_global_state(state, progress, error, component); } @@ -147,7 +122,6 @@ class OTAGlobalCallback { }; OTAGlobalCallback *get_global_ota_callback(); -void register_ota_platform(OTAComponent *ota_caller); // OTA implementations should use: // - notify_state_() when already in main loop (e.g., esphome OTA) diff --git a/esphome/components/web_server/ota/ota_web_server.cpp b/esphome/components/web_server/ota/ota_web_server.cpp index 30c4a59b8b3..f612aa056c0 100644 --- a/esphome/components/web_server/ota/ota_web_server.cpp +++ b/esphome/components/web_server/ota/ota_web_server.cpp @@ -232,10 +232,6 @@ void WebServerOTAComponent::setup() { // AsyncWebServer takes ownership of the handler and will delete it when the server is destroyed base->add_handler(new OTARequestHandler(this)); // NOLINT -#ifdef USE_OTA_STATE_LISTENER - // Register with global OTA callback system - ota::register_ota_platform(this); -#endif } void WebServerOTAComponent::dump_config() { ESP_LOGCONFIG(TAG, "Web Server OTA"); }