diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index e4b6946116..8956227c17 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -1,3 +1,4 @@ +from esphome.cpp_generator import RawExpression import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import ( @@ -31,7 +32,9 @@ def to_code(config): yield cg.register_component(var, config) if config[CONF_SAFE_MODE]: - cg.add(var.start_safe_mode(config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT])) + condition = var.should_enter_safe_mode(config[CONF_NUM_ATTEMPTS], + config[CONF_REBOOT_TIMEOUT]) + cg.add(RawExpression(f"if ({condition}) return")) if CORE.is_esp8266: cg.add_library('Update', None) diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index b614139e07..c8221d1bdf 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -355,7 +355,7 @@ void OTAComponent::set_auth_password(const std::string &password) { this->passwo float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; } uint16_t OTAComponent::get_port() const { return this->port_; } void OTAComponent::set_port(uint16_t port) { this->port_ = port; } -void OTAComponent::start_safe_mode(uint8_t num_attempts, uint32_t enable_time) { +bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { this->has_safe_mode_ = true; this->safe_mode_start_time_ = millis(); this->safe_mode_enable_time_ = enable_time; @@ -380,12 +380,11 @@ void OTAComponent::start_safe_mode(uint8_t num_attempts, uint32_t enable_time) { ESP_LOGI(TAG, "Waiting for OTA attempt."); - while (true) { - App.loop(); - } + return true; } else { // increment counter this->write_rtc_(this->safe_mode_rtc_value_ + 1); + return false; } } void OTAComponent::write_rtc_(uint32_t val) { this->rtc_.save(&val); } diff --git a/esphome/components/ota/ota_component.h b/esphome/components/ota/ota_component.h index 65d44482b8..f16725e324 100644 --- a/esphome/components/ota/ota_component.h +++ b/esphome/components/ota/ota_component.h @@ -47,7 +47,7 @@ class OTAComponent : public Component { /// Manually set the port OTA should listen on. void set_port(uint16_t port); - void start_safe_mode(uint8_t num_attempts, uint32_t enable_time); + bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time); // ========== INTERNAL METHODS ========== // (In most use cases you won't need these)