mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Refactor global_ota_component usage
				
					
				
			This commit is contained in:
		| @@ -15,7 +15,6 @@ from esphome.const import ( | |||||||
|     CONF_ON_BLE_ADVERTISE, |     CONF_ON_BLE_ADVERTISE, | ||||||
|     CONF_ON_BLE_MANUFACTURER_DATA_ADVERTISE, |     CONF_ON_BLE_MANUFACTURER_DATA_ADVERTISE, | ||||||
|     CONF_ON_BLE_SERVICE_DATA_ADVERTISE, |     CONF_ON_BLE_SERVICE_DATA_ADVERTISE, | ||||||
|     CONF_OTA, |  | ||||||
|     CONF_SERVICE_UUID, |     CONF_SERVICE_UUID, | ||||||
|     CONF_TRIGGER_ID, |     CONF_TRIGGER_ID, | ||||||
|     KEY_CORE, |     KEY_CORE, | ||||||
| @@ -273,11 +272,6 @@ async def to_code(config): | |||||||
|             add_idf_sdkconfig_option("CONFIG_BTU_TASK_STACK_SIZE", 8192) |             add_idf_sdkconfig_option("CONFIG_BTU_TASK_STACK_SIZE", 8192) | ||||||
|         add_idf_sdkconfig_option("CONFIG_BT_ACL_CONNECTIONS", 9) |         add_idf_sdkconfig_option("CONFIG_BT_ACL_CONNECTIONS", 9) | ||||||
|  |  | ||||||
|     if CONF_OTA in CORE.config: |  | ||||||
|         for ota_config in CORE.config.get(CONF_OTA): |  | ||||||
|             ota_platform = await cg.get_variable(ota_config[CONF_ID]) |  | ||||||
|             cg.add(var.add_ota_component(ota_platform)) |  | ||||||
|  |  | ||||||
|     cg.add_define("USE_OTA_STATE_CALLBACK")  # To be notified when an OTA update starts |     cg.add_define("USE_OTA_STATE_CALLBACK")  # To be notified when an OTA update starts | ||||||
|     cg.add_define("USE_ESP32_BLE_CLIENT") |     cg.add_define("USE_ESP32_BLE_CLIENT") | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,6 +17,10 @@ | |||||||
| #include <nvs_flash.h> | #include <nvs_flash.h> | ||||||
| #include <cinttypes> | #include <cinttypes> | ||||||
|  |  | ||||||
|  | #ifdef USE_OTA | ||||||
|  | #include "esphome/components/ota/ota_backend.h" | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO | #ifdef USE_ARDUINO | ||||||
| #include <esp32-hal-bt.h> | #include <esp32-hal-bt.h> | ||||||
| #endif | #endif | ||||||
| @@ -54,13 +58,12 @@ void ESP32BLETracker::setup() { | |||||||
|   this->scanner_idle_ = true; |   this->scanner_idle_ = true; | ||||||
|  |  | ||||||
| #ifdef USE_OTA | #ifdef USE_OTA | ||||||
|   for (auto &ota_comp : this->ota_components_) { |   ota::global_ota_component->add_on_state_callback( | ||||||
|     ota_comp->add_on_state_callback([this](ota::OTAState state, float progress, uint8_t error) { |       [this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { | ||||||
|       if (state == ota::OTA_STARTED) { |         if (state == ota::OTA_STARTED) { | ||||||
|         this->stop_scan(); |           this->stop_scan(); | ||||||
|       } |         } | ||||||
|     }); |       }); | ||||||
|   } |  | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -11,10 +11,6 @@ | |||||||
|  |  | ||||||
| #ifdef USE_ESP32 | #ifdef USE_ESP32 | ||||||
|  |  | ||||||
| #ifdef USE_OTA |  | ||||||
| #include "esphome/components/ota/ota_backend.h" |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #include <esp_gap_ble_api.h> | #include <esp_gap_ble_api.h> | ||||||
| #include <esp_gattc_api.h> | #include <esp_gattc_api.h> | ||||||
| #include <esp_bt_defs.h> | #include <esp_bt_defs.h> | ||||||
| @@ -201,10 +197,6 @@ class ESP32BLETracker : public Component, | |||||||
|  |  | ||||||
|   void loop() override; |   void loop() override; | ||||||
|  |  | ||||||
| #ifdef USE_OTA |  | ||||||
|   void add_ota_component(ota::OTAComponent *ota_component) { this->ota_components_.push_back(ota_component); } |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|   void register_listener(ESPBTDeviceListener *listener); |   void register_listener(ESPBTDeviceListener *listener); | ||||||
|   void register_client(ESPBTClient *client); |   void register_client(ESPBTClient *client); | ||||||
|   void recalculate_advertisement_parser_types(); |   void recalculate_advertisement_parser_types(); | ||||||
| @@ -236,11 +228,6 @@ class ESP32BLETracker : public Component, | |||||||
|  |  | ||||||
|   int app_id_; |   int app_id_; | ||||||
|  |  | ||||||
| #ifdef USE_OTA |  | ||||||
|   /// Vector of OTA components we'll hook into so we can stop scanning when an OTA update begins |  | ||||||
|   std::vector<ota::OTAComponent *> ota_components_{}; |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|   /// Vector of addresses that have already been printed in print_bt_device_info |   /// Vector of addresses that have already been printed in print_bt_device_info | ||||||
|   std::vector<uint64_t> already_discovered_; |   std::vector<uint64_t> already_discovered_; | ||||||
|   std::vector<ESPBTDeviceListener *> listeners_; |   std::vector<ESPBTDeviceListener *> listeners_; | ||||||
|   | |||||||
| @@ -21,11 +21,11 @@ namespace esphome { | |||||||
| static const char *const TAG = "esphome.ota"; | static const char *const TAG = "esphome.ota"; | ||||||
| static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; | static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; | ||||||
|  |  | ||||||
| ESPHomeOTAComponent *global_ota_component = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) |  | ||||||
|  |  | ||||||
| ESPHomeOTAComponent::ESPHomeOTAComponent() { global_ota_component = this; } |  | ||||||
|  |  | ||||||
| void ESPHomeOTAComponent::setup() { | void ESPHomeOTAComponent::setup() { | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  |   ota::global_ota_component->register_ota(this); | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   server_ = socket::socket_ip(SOCK_STREAM, 0); |   server_ = socket::socket_ip(SOCK_STREAM, 0); | ||||||
|   if (server_ == nullptr) { |   if (server_ == nullptr) { | ||||||
|     ESP_LOGW(TAG, "Could not create socket"); |     ESP_LOGW(TAG, "Could not create socket"); | ||||||
|   | |||||||
| @@ -11,7 +11,6 @@ namespace esphome { | |||||||
| /// ESPHomeOTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. | /// ESPHomeOTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. | ||||||
| class ESPHomeOTAComponent : public ota::OTAComponent { | class ESPHomeOTAComponent : public ota::OTAComponent { | ||||||
|  public: |  public: | ||||||
|   ESPHomeOTAComponent(); |  | ||||||
| #ifdef USE_OTA_PASSWORD | #ifdef USE_OTA_PASSWORD | ||||||
|   void set_auth_password(const std::string &password) { password_ = password; } |   void set_auth_password(const std::string &password) { password_ = password; } | ||||||
| #endif  // USE_OTA_PASSWORD | #endif  // USE_OTA_PASSWORD | ||||||
| @@ -66,6 +65,4 @@ class ESPHomeOTAComponent : public ota::OTAComponent { | |||||||
|       0x5afe5afe;  ///< a magic number to indicate that safe mode should be entered on next boot |       0x5afe5afe;  ///< a magic number to indicate that safe mode should be entered on next boot | ||||||
| }; | }; | ||||||
|  |  | ||||||
| extern ESPHomeOTAComponent *global_ota_component;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) |  | ||||||
|  |  | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								esphome/components/ota/ota_backend.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								esphome/components/ota/ota_backend.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | #include "ota_backend.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace ota { | ||||||
|  |  | ||||||
|  | #ifdef USE_OTA_STATE_CALLBACK | ||||||
|  | OTAGlobalCallback *global_ota_component = | ||||||
|  |     new OTAGlobalCallback;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | }  // namespace ota | ||||||
|  | }  // namespace esphome | ||||||
| @@ -1,12 +1,13 @@ | |||||||
| #pragma once | #pragma once | ||||||
| #ifdef USE_OTA_STATE_CALLBACK |  | ||||||
| #include "esphome/core/automation.h" |  | ||||||
| #include "esphome/core/defines.h" |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.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 { | namespace ota { | ||||||
|  |  | ||||||
| @@ -64,6 +65,25 @@ class OTAComponent : public Component { | |||||||
| #endif | #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_{}; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | extern OTAGlobalCallback *global_ota_component;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | ||||||
|  | #endif | ||||||
| std::unique_ptr<ota::OTABackend> make_ota_backend(); | std::unique_ptr<ota::OTABackend> make_ota_backend(); | ||||||
|  |  | ||||||
| }  // namespace ota | }  // namespace ota | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user