Separate OTABackend from OTA component (#6459)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Keith Burzinski
2024-05-15 21:01:09 -05:00
committed by GitHub
parent f91c31f093
commit f46c499c4e
40 changed files with 505 additions and 391 deletions
+15 -10
View File
@@ -18,22 +18,23 @@ from esphome.const import (
CONF_BAUD_RATE,
CONF_BROKER,
CONF_DEASSERT_RTS_DTR,
CONF_DISABLED,
CONF_ESPHOME,
CONF_LOGGER,
CONF_MDNS,
CONF_MQTT,
CONF_NAME,
CONF_OTA,
CONF_MQTT,
CONF_MDNS,
CONF_DISABLED,
CONF_PASSWORD,
CONF_PORT,
CONF_ESPHOME,
CONF_PLATFORM,
CONF_PLATFORMIO_OPTIONS,
CONF_PORT,
CONF_SUBSTITUTIONS,
PLATFORM_BK72XX,
PLATFORM_RTL87XX,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_RP2040,
PLATFORM_RTL87XX,
SECRETS_FILES,
)
from esphome.core import CORE, EsphomeError, coroutine
@@ -330,15 +331,19 @@ def upload_program(config, args, host):
return 1 # Unknown target platform
if CONF_OTA not in config:
ota_conf = {}
for ota_item in config.get(CONF_OTA, []):
if ota_item[CONF_PLATFORM] == CONF_ESPHOME:
ota_conf = ota_item
break
if not ota_conf:
raise EsphomeError(
"Cannot upload Over the Air as the config does not include the ota: "
"component"
f"Cannot upload Over the Air as the {CONF_OTA} configuration is not present or does not include {CONF_PLATFORM}: {CONF_ESPHOME}"
)
from esphome import espota2
ota_conf = config[CONF_OTA]
remote_port = ota_conf[CONF_PORT]
password = ota_conf.get(CONF_PASSWORD, "")
@@ -18,7 +18,7 @@
#include <cinttypes>
#ifdef USE_OTA
#include "esphome/components/ota/ota_component.h"
#include "esphome/components/ota/ota_backend.h"
#endif
#ifdef USE_ARDUINO
@@ -61,11 +61,12 @@ void ESP32BLETracker::setup() {
this->scanner_idle_ = true;
#ifdef USE_OTA
ota::global_ota_component->add_on_state_callback([this](ota::OTAState state, float progress, uint8_t error) {
if (state == ota::OTA_STARTED) {
this->stop_scan();
}
});
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) {
this->stop_scan();
}
});
#endif
}
@@ -0,0 +1,72 @@
from esphome.cpp_generator import RawExpression
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components.ota import BASE_OTA_SCHEMA, ota_to_code, OTAComponent
from esphome.const import (
CONF_ID,
CONF_NUM_ATTEMPTS,
CONF_OTA,
CONF_PASSWORD,
CONF_PORT,
CONF_REBOOT_TIMEOUT,
CONF_SAFE_MODE,
CONF_VERSION,
KEY_PAST_SAFE_MODE,
)
from esphome.core import CORE, coroutine_with_priority
CODEOWNERS = ["@esphome/core"]
AUTO_LOAD = ["md5", "socket"]
DEPENDENCIES = ["network"]
esphome = cg.esphome_ns.namespace("esphome")
ESPHomeOTAComponent = esphome.class_("ESPHomeOTAComponent", OTAComponent)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(ESPHomeOTAComponent),
cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean,
cv.Optional(CONF_VERSION, default=2): cv.one_of(1, 2, int=True),
cv.SplitDefault(
CONF_PORT,
esp8266=8266,
esp32=3232,
rp2040=2040,
bk72xx=8892,
rtl87xx=8892,
): cv.port,
cv.Optional(CONF_PASSWORD): cv.string,
cv.Optional(
CONF_REBOOT_TIMEOUT, default="5min"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_NUM_ATTEMPTS, default="10"): cv.positive_not_null_int,
}
)
.extend(BASE_OTA_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
@coroutine_with_priority(50.0)
async def to_code(config):
CORE.data[CONF_OTA] = {}
var = cg.new_Pvariable(config[CONF_ID])
await ota_to_code(var, config)
cg.add(var.set_port(config[CONF_PORT]))
if CONF_PASSWORD in config:
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
cg.add_define("USE_OTA_PASSWORD")
cg.add_define("USE_OTA_VERSION", config[CONF_VERSION])
await cg.register_component(var, config)
if config[CONF_SAFE_MODE]:
condition = var.should_enter_safe_mode(
config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT]
)
cg.add(RawExpression(f"if ({condition}) return"))
CORE.data[CONF_OTA][KEY_PAST_SAFE_MODE] = True
@@ -1,49 +1,16 @@
#pragma once
#include "esphome/components/socket/socket.h"
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
#include "esphome/core/helpers.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
#include "esphome/components/ota/ota_backend.h"
#include "esphome/components/socket/socket.h"
namespace esphome {
namespace ota {
enum OTAResponseTypes {
OTA_RESPONSE_OK = 0x00,
OTA_RESPONSE_REQUEST_AUTH = 0x01,
OTA_RESPONSE_HEADER_OK = 0x40,
OTA_RESPONSE_AUTH_OK = 0x41,
OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42,
OTA_RESPONSE_BIN_MD5_OK = 0x43,
OTA_RESPONSE_RECEIVE_OK = 0x44,
OTA_RESPONSE_UPDATE_END_OK = 0x45,
OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46,
OTA_RESPONSE_CHUNK_OK = 0x47,
OTA_RESPONSE_ERROR_MAGIC = 0x80,
OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81,
OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82,
OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83,
OTA_RESPONSE_ERROR_UPDATE_END = 0x84,
OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85,
OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86,
OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87,
OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88,
OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89,
OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A,
OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B,
OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C,
OTA_RESPONSE_ERROR_UNKNOWN = 0xFF,
};
enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR };
/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA.
class OTAComponent : public Component {
/// ESPHomeOTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA.
class ESPHomeOTAComponent : public ota::OTAComponent {
public:
OTAComponent();
#ifdef USE_OTA_PASSWORD
void set_auth_password(const std::string &password) { password_ = password; }
#endif // USE_OTA_PASSWORD
@@ -57,10 +24,6 @@ class OTAComponent : public Component {
void set_safe_mode_pending(const bool &pending);
bool get_safe_mode_pending();
#ifdef USE_OTA_STATE_CALLBACK
void add_on_state_callback(std::function<void(OTAState, float, uint8_t)> &&callback);
#endif
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override;
@@ -91,22 +54,15 @@ class OTAComponent : public Component {
std::unique_ptr<socket::Socket> server_;
std::unique_ptr<socket::Socket> client_;
bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled.
uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled.
uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for.
bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled
uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled
uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for
uint32_t safe_mode_rtc_value_;
uint8_t safe_mode_num_attempts_;
ESPPreferenceObject rtc_;
static const uint32_t ENTER_SAFE_MODE_MAGIC =
0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot
#ifdef USE_OTA_STATE_CALLBACK
CallbackManager<void(OTAState, float, uint8_t)> state_callback_{};
#endif
};
extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
} // namespace ota
} // namespace esphome
+43 -63
View File
@@ -1,71 +1,67 @@
from esphome.cpp_generator import RawExpression
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import (
CONF_ID,
CONF_NUM_ATTEMPTS,
CONF_PASSWORD,
CONF_PORT,
CONF_REBOOT_TIMEOUT,
CONF_SAFE_MODE,
CONF_TRIGGER_ID,
CONF_OTA,
KEY_PAST_SAFE_MODE,
CONF_VERSION,
)
from esphome.core import CORE, coroutine_with_priority
CODEOWNERS = ["@esphome/core"]
DEPENDENCIES = ["network"]
AUTO_LOAD = ["socket", "md5"]
from esphome.const import CONF_ESPHOME, CONF_OTA, CONF_PLATFORM, CONF_TRIGGER_ID
CONF_ON_STATE_CHANGE = "on_state_change"
CODEOWNERS = ["@esphome/core"]
AUTO_LOAD = ["md5"]
IS_PLATFORM_COMPONENT = True
CONF_ON_ABORT = "on_abort"
CONF_ON_BEGIN = "on_begin"
CONF_ON_PROGRESS = "on_progress"
CONF_ON_END = "on_end"
CONF_ON_ERROR = "on_error"
CONF_ON_PROGRESS = "on_progress"
CONF_ON_STATE_CHANGE = "on_state_change"
ota_ns = cg.esphome_ns.namespace("ota")
OTAState = ota_ns.enum("OTAState")
OTAComponent = ota_ns.class_("OTAComponent", cg.Component)
OTAState = ota_ns.enum("OTAState")
OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template())
OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template())
OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template())
OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template())
OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template())
OTAStateChangeTrigger = ota_ns.class_(
"OTAStateChangeTrigger", automation.Trigger.template()
)
OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template())
OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template())
OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template())
OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template())
CONFIG_SCHEMA = cv.Schema(
def _ota_final_validate(config):
if len(config) < 1:
raise cv.Invalid(
f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality"
)
FINAL_VALIDATE_SCHEMA = _ota_final_validate
BASE_OTA_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(OTAComponent),
cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean,
cv.Optional(CONF_VERSION, default=2): cv.one_of(1, 2, int=True),
cv.SplitDefault(
CONF_PORT,
esp8266=8266,
esp32=3232,
rp2040=2040,
bk72xx=8892,
rtl87xx=8892,
): cv.port,
cv.Optional(CONF_PASSWORD): cv.string,
cv.Optional(
CONF_REBOOT_TIMEOUT, default="5min"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_NUM_ATTEMPTS, default="10"): cv.positive_not_null_int,
cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger),
}
),
cv.Optional(CONF_ON_ABORT): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger),
}
),
cv.Optional(CONF_ON_BEGIN): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger),
}
),
cv.Optional(CONF_ON_END): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger),
}
),
cv.Optional(CONF_ON_ERROR): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger),
@@ -76,35 +72,13 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger),
}
),
cv.Optional(CONF_ON_END): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger),
}
),
}
).extend(cv.COMPONENT_SCHEMA)
)
@coroutine_with_priority(50.0)
async def to_code(config):
CORE.data[CONF_OTA] = {}
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_port(config[CONF_PORT]))
cg.add_define("USE_OTA")
if CONF_PASSWORD in config:
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
cg.add_define("USE_OTA_PASSWORD")
cg.add_define("USE_OTA_VERSION", config[CONF_VERSION])
await cg.register_component(var, config)
if config[CONF_SAFE_MODE]:
condition = var.should_enter_safe_mode(
config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT]
)
cg.add(RawExpression(f"if ({condition}) return"))
CORE.data[CONF_OTA][KEY_PAST_SAFE_MODE] = True
if CORE.is_esp32 and CORE.using_arduino:
cg.add_library("Update", None)
@@ -112,11 +86,17 @@ async def to_code(config):
if CORE.is_rp2040 and CORE.using_arduino:
cg.add_library("Updater", None)
async def ota_to_code(var, config):
use_state_callback = False
for conf in config.get(CONF_ON_STATE_CHANGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(OTAState, "state")], conf)
use_state_callback = True
for conf in config.get(CONF_ON_ABORT, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
use_state_callback = True
for conf in config.get(CONF_ON_BEGIN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
+13 -6
View File
@@ -1,11 +1,8 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_OTA_STATE_CALLBACK
#include "ota_backend.h"
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/ota/ota_component.h"
namespace esphome {
namespace ota {
@@ -54,6 +51,17 @@ class OTAEndTrigger : public Trigger<> {
}
};
class OTAAbortTrigger : public Trigger<> {
public:
explicit OTAAbortTrigger(OTAComponent *parent) {
parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) {
if (state == OTA_ABORT && !parent->is_failed()) {
trigger();
}
});
}
};
class OTAErrorTrigger : public Trigger<uint8_t> {
public:
explicit OTAErrorTrigger(OTAComponent *parent) {
@@ -67,5 +75,4 @@ class OTAErrorTrigger : public Trigger<uint8_t> {
} // namespace ota
} // namespace esphome
#endif // USE_OTA_STATE_CALLBACK
#endif
+20
View File
@@ -0,0 +1,20 @@
#include "ota_backend.h"
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
} // namespace ota
} // namespace esphome
+78 -1
View File
@@ -1,9 +1,53 @@
#pragma once
#include "ota_component.h"
#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 {
enum OTAResponseTypes {
OTA_RESPONSE_OK = 0x00,
OTA_RESPONSE_REQUEST_AUTH = 0x01,
OTA_RESPONSE_HEADER_OK = 0x40,
OTA_RESPONSE_AUTH_OK = 0x41,
OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42,
OTA_RESPONSE_BIN_MD5_OK = 0x43,
OTA_RESPONSE_RECEIVE_OK = 0x44,
OTA_RESPONSE_UPDATE_END_OK = 0x45,
OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46,
OTA_RESPONSE_CHUNK_OK = 0x47,
OTA_RESPONSE_ERROR_MAGIC = 0x80,
OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81,
OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82,
OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83,
OTA_RESPONSE_ERROR_UPDATE_END = 0x84,
OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85,
OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86,
OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87,
OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88,
OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89,
OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A,
OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B,
OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C,
OTA_RESPONSE_ERROR_UNKNOWN = 0xFF,
};
enum OTAState {
OTA_COMPLETED = 0,
OTA_STARTED,
OTA_IN_PROGRESS,
OTA_ABORT,
OTA_ERROR,
};
class OTABackend {
public:
virtual ~OTABackend() = default;
@@ -15,5 +59,38 @@ class OTABackend {
virtual bool supports_compression() = 0;
};
class OTAComponent : public Component {
#ifdef USE_OTA_STATE_CALLBACK
public:
void add_on_state_callback(std::function<void(ota::OTAState, float, uint8_t)> &&callback) {
this->state_callback_.add(std::move(callback));
}
protected:
CallbackManager<void(ota::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
std::unique_ptr<ota::OTABackend> make_ota_backend();
} // namespace ota
} // namespace esphome
@@ -1,8 +1,7 @@
#include "esphome/core/defines.h"
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
#include "esphome/core/defines.h"
#include "ota_backend_arduino_esp32.h"
#include "ota_component.h"
#include "ota_backend.h"
#include <Update.h>
@@ -10,6 +9,8 @@
namespace esphome {
namespace ota {
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::ArduinoESP32OTABackend>(); }
OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) {
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
@@ -1,10 +1,10 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
#include "ota_component.h"
#include "ota_backend.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace ota {
@@ -1,10 +1,9 @@
#include "esphome/core/defines.h"
#ifdef USE_ARDUINO
#ifdef USE_ESP8266
#include "ota_backend_arduino_esp8266.h"
#include "ota_component.h"
#include "ota_backend.h"
#include "ota_backend_arduino_esp8266.h"
#include "esphome/core/defines.h"
#include "esphome/components/esp8266/preferences.h"
#include <Updater.h>
@@ -12,6 +11,8 @@
namespace esphome {
namespace ota {
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::ArduinoESP8266OTABackend>(); }
OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) {
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
@@ -1,10 +1,9 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_ARDUINO
#ifdef USE_ESP8266
#include "ota_component.h"
#include "ota_backend.h"
#include "esphome/core/defines.h"
#include "esphome/core/macros.h"
namespace esphome {
@@ -1,15 +1,16 @@
#include "esphome/core/defines.h"
#ifdef USE_LIBRETINY
#include "ota_backend_arduino_libretiny.h"
#include "ota_component.h"
#include "ota_backend.h"
#include "ota_backend_arduino_libretiny.h"
#include "esphome/core/defines.h"
#include <Update.h>
namespace esphome {
namespace ota {
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::ArduinoLibreTinyOTABackend>(); }
OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) {
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
@@ -1,10 +1,9 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_LIBRETINY
#include "ota_component.h"
#include "ota_backend.h"
#include "esphome/core/defines.h"
namespace esphome {
namespace ota {
@@ -1,17 +1,18 @@
#include "esphome/core/defines.h"
#ifdef USE_ARDUINO
#ifdef USE_RP2040
#include "esphome/components/rp2040/preferences.h"
#include "ota_backend.h"
#include "ota_backend_arduino_rp2040.h"
#include "ota_component.h"
#include "esphome/components/rp2040/preferences.h"
#include "esphome/core/defines.h"
#include <Updater.h>
namespace esphome {
namespace ota {
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::ArduinoRP2040OTABackend>(); }
OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) {
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
@@ -1,11 +1,10 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_ARDUINO
#ifdef USE_RP2040
#include "esphome/core/macros.h"
#include "ota_backend.h"
#include "ota_component.h"
#include "esphome/core/defines.h"
#include "esphome/core/macros.h"
namespace esphome {
namespace ota {
@@ -1,12 +1,11 @@
#include "esphome/core/defines.h"
#ifdef USE_ESP_IDF
#include <esp_task_wdt.h>
#include "ota_backend_esp_idf.h"
#include "ota_component.h"
#include <esp_ota_ops.h>
#include "esphome/components/md5/md5.h"
#include "esphome/core/defines.h"
#include <esp_ota_ops.h>
#include <esp_task_wdt.h>
#if ESP_IDF_VERSION_MAJOR >= 5
#include <spi_flash_mmap.h>
@@ -15,6 +14,8 @@
namespace esphome {
namespace ota {
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::IDFOTABackend>(); }
OTAResponseTypes IDFOTABackend::begin(size_t image_size) {
this->partition_ = esp_ota_get_next_update_partition(nullptr);
if (this->partition_ == nullptr) {
+4 -4
View File
@@ -1,11 +1,11 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_ESP_IDF
#include "ota_component.h"
#include "ota_backend.h"
#include <esp_ota_ops.h>
#include "esphome/components/md5/md5.h"
#include "esphome/core/defines.h"
#include <esp_ota_ops.h>
namespace esphome {
namespace ota {
@@ -1,18 +1,17 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button
from esphome.components.ota import OTAComponent
from esphome.components.esphome.ota import ESPHomeOTAComponent
from esphome.const import (
CONF_ID,
CONF_OTA,
CONF_ESPHOME,
DEVICE_CLASS_RESTART,
ENTITY_CATEGORY_CONFIG,
ICON_RESTART_ALERT,
)
from .. import safe_mode_ns
DEPENDENCIES = ["ota"]
DEPENDENCIES = ["ota.esphome"]
safe_mode_ns = cg.esphome_ns.namespace("safe_mode")
SafeModeButton = safe_mode_ns.class_("SafeModeButton", button.Button, cg.Component)
CONFIG_SCHEMA = (
@@ -22,15 +21,14 @@ CONFIG_SCHEMA = (
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
)
.extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)})
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(ESPHomeOTAComponent)})
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
var = await button.new_button(config)
await cg.register_component(var, config)
await button.register_button(var, config)
ota = await cg.get_variable(config[CONF_OTA])
ota = await cg.get_variable(config[CONF_ESPHOME])
cg.add(var.set_ota(ota))
@@ -8,7 +8,7 @@ namespace safe_mode {
static const char *const TAG = "safe_mode.button";
void SafeModeButton::set_ota(ota::OTAComponent *ota) { this->ota_ = ota; }
void SafeModeButton::set_ota(esphome::ESPHomeOTAComponent *ota) { this->ota_ = ota; }
void SafeModeButton::press_action() {
ESP_LOGI(TAG, "Restarting device in safe mode...");
@@ -1,8 +1,8 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/ota/ota_component.h"
#include "esphome/components/button/button.h"
#include "esphome/components/esphome/ota/ota_esphome.h"
#include "esphome/core/component.h"
namespace esphome {
namespace safe_mode {
@@ -10,10 +10,10 @@ namespace safe_mode {
class SafeModeButton : public button::Button, public Component {
public:
void dump_config() override;
void set_ota(ota::OTAComponent *ota);
void set_ota(esphome::ESPHomeOTAComponent *ota);
protected:
ota::OTAComponent *ota_;
esphome::ESPHomeOTAComponent *ota_;
void press_action() override;
};
@@ -1,26 +1,26 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import switch
from esphome.components.ota import OTAComponent
from esphome.components.esphome.ota import ESPHomeOTAComponent
from esphome.const import (
CONF_OTA,
CONF_ESPHOME,
ENTITY_CATEGORY_CONFIG,
ICON_RESTART_ALERT,
)
from .. import safe_mode_ns
DEPENDENCIES = ["ota"]
DEPENDENCIES = ["ota.esphome"]
SafeModeSwitch = safe_mode_ns.class_("SafeModeSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = (
switch.switch_schema(
SafeModeSwitch,
icon=ICON_RESTART_ALERT,
entity_category=ENTITY_CATEGORY_CONFIG,
block_inverted=True,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
)
.extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)})
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(ESPHomeOTAComponent)})
.extend(cv.COMPONENT_SCHEMA)
)
@@ -29,5 +29,5 @@ async def to_code(config):
var = await switch.new_switch(config)
await cg.register_component(var, config)
ota = await cg.get_variable(config[CONF_OTA])
ota = await cg.get_variable(config[CONF_ESPHOME])
cg.add(var.set_ota(ota))
@@ -1,14 +1,14 @@
#include "safe_mode_switch.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace safe_mode {
static const char *const TAG = "safe_mode_switch";
void SafeModeSwitch::set_ota(ota::OTAComponent *ota) { this->ota_ = ota; }
void SafeModeSwitch::set_ota(esphome::ESPHomeOTAComponent *ota) { this->ota_ = ota; }
void SafeModeSwitch::write_state(bool state) {
// Acknowledge
@@ -1,8 +1,8 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/ota/ota_component.h"
#include "esphome/components/esphome/ota/ota_esphome.h"
#include "esphome/components/switch/switch.h"
#include "esphome/core/component.h"
namespace esphome {
namespace safe_mode {
@@ -10,10 +10,10 @@ namespace safe_mode {
class SafeModeSwitch : public switch_::Switch, public Component {
public:
void dump_config() override;
void set_ota(ota::OTAComponent *ota);
void set_ota(esphome::ESPHomeOTAComponent *ota);
protected:
ota::OTAComponent *ota_;
esphome::ESPHomeOTAComponent *ota_;
void write_state(bool state) override;
};
+15 -6
View File
@@ -3,14 +3,16 @@ import logging
from esphome.const import (
CONF_DISABLED_BY_DEFAULT,
CONF_ENTITY_CATEGORY,
CONF_ESPHOME,
CONF_ICON,
CONF_INTERNAL,
CONF_NAME,
CONF_SETUP_PRIORITY,
CONF_UPDATE_INTERVAL,
CONF_TYPE_ID,
CONF_OTA,
CONF_PLATFORM,
CONF_SAFE_MODE,
CONF_SETUP_PRIORITY,
CONF_TYPE_ID,
CONF_UPDATE_INTERVAL,
KEY_PAST_SAFE_MODE,
)
@@ -139,9 +141,16 @@ async def build_registry_list(registry, config):
async def past_safe_mode():
safe_mode_enabled = (
CONF_OTA in CORE.config and CORE.config[CONF_OTA][CONF_SAFE_MODE]
)
ota_conf = {}
for ota_item in CORE.config.get(CONF_OTA, []):
if ota_item[CONF_PLATFORM] == CONF_ESPHOME:
ota_conf = ota_item
break
if not ota_conf:
return
safe_mode_enabled = ota_conf[CONF_SAFE_MODE]
if not safe_mode_enabled:
return
+3 -2
View File
@@ -153,10 +153,11 @@ def wizard_file(**kwargs):
# Configure OTA
config += "\nota:\n"
config += " - platform: esphome\n"
if "ota_password" in kwargs:
config += f" password: \"{kwargs['ota_password']}\""
config += f" password: \"{kwargs['ota_password']}\""
elif "password" in kwargs:
config += f" password: \"{kwargs['password']}\""
config += f" password: \"{kwargs['password']}\""
# Configuring wifi
config += "\n\nwifi:\n"
+26 -25
View File
@@ -3,28 +3,29 @@ wifi:
password: password1
ota:
safe_mode: true
password: "superlongpasswordthatnoonewillknow"
port: 3286
reboot_timeout: 2min
num_attempts: 5
on_begin:
then:
- logger.log: "OTA start"
on_progress:
then:
- logger.log:
format: "OTA progress %0.1f%%"
args: ["x"]
on_end:
then:
- logger.log: "OTA end"
on_error:
then:
- logger.log:
format: "OTA update error %d"
args: ["x"]
on_state_change:
then:
lambda: >-
ESP_LOGD("ota", "State %d", state);
- platform: esphome
safe_mode: true
password: "superlongpasswordthatnoonewillknow"
port: 3286
reboot_timeout: 2min
num_attempts: 5
on_begin:
then:
- logger.log: "OTA start"
on_progress:
then:
- logger.log:
format: "OTA progress %0.1f%%"
args: ["x"]
on_end:
then:
- logger.log: "OTA end"
on_error:
then:
- logger.log:
format: "OTA update error %d"
args: ["x"]
on_state_change:
then:
lambda: >-
ESP_LOGD("ota", "State %d", state);
+2
View File
@@ -3,6 +3,8 @@ wifi:
password: password1
ota:
- platform: esphome
safe_mode: true
button:
- platform: safe_mode
+2 -2
View File
@@ -5,7 +5,7 @@
#include <esphome/components/gpio/switch/gpio_switch.h>
#include <esphome/components/logger/logger.h>
#include <esphome/components/ota/ota_component.h>
#include <esphome/components/esphome/ota/ota_esphome.h>
#include <esphome/components/wifi/wifi_component.h>
#include <esphome/core/application.h>
@@ -25,7 +25,7 @@ void setup() {
ap.set_password("password1");
wifi->add_sta(ap);
auto *ota = new ota::OTAComponent(); // NOLINT
auto *ota = new esphome::ESPHomeOTAComponent(); // NOLINT
ota->set_port(8266);
App.setup();
+24 -23
View File
@@ -265,29 +265,30 @@ uart:
baud_rate: 9600
ota:
safe_mode: true
password: "superlongpasswordthatnoonewillknow"
port: 3286
reboot_timeout: 2min
num_attempts: 5
on_state_change:
then:
lambda: >-
ESP_LOGD("ota", "State %d", state);
on_begin:
then:
logger.log: OTA begin
on_progress:
then:
lambda: >-
ESP_LOGD("ota", "Got progress %f", x);
on_end:
then:
logger.log: OTA end
on_error:
then:
lambda: >-
ESP_LOGD("ota", "Got error code %d", x);
- platform: esphome
safe_mode: true
password: "superlongpasswordthatnoonewillknow"
port: 3286
reboot_timeout: 2min
num_attempts: 5
on_state_change:
then:
lambda: >-
ESP_LOGD("ota", "State %d", state);
on_begin:
then:
logger.log: OTA begin
on_progress:
then:
lambda: >-
ESP_LOGD("ota", "Got progress %f", x);
on_end:
then:
logger.log: OTA end
on_error:
then:
lambda: >-
ESP_LOGD("ota", "Got error code %d", x);
logger:
baud_rate: 0
+1
View File
@@ -31,6 +31,7 @@ network:
api:
ota:
- platform: esphome
logger:
+4 -3
View File
@@ -80,9 +80,10 @@ uart:
- lambda: UARTDebug::log_hex(direction, bytes, ':');
ota:
safe_mode: true
port: 3286
num_attempts: 15
- platform: esphome
safe_mode: true
port: 3286
num_attempts: 15
logger:
level: DEBUG
+2 -1
View File
@@ -49,7 +49,8 @@ spi:
number: GPIO14
ota:
version: 2
- platform: esphome
version: 2
logger:
+4 -3
View File
@@ -328,9 +328,10 @@ vbus:
uart_id: uart_4
ota:
safe_mode: true
port: 3286
reboot_timeout: 15min
- platform: esphome
safe_mode: true
port: 3286
reboot_timeout: 15min
logger:
hardware_uart: UART1
+3 -2
View File
@@ -103,8 +103,9 @@ uart:
parity: EVEN
ota:
safe_mode: true
port: 3286
- platform: esphome
safe_mode: true
port: 3286
logger:
level: DEBUG
+1
View File
@@ -28,6 +28,7 @@ network:
api:
ota:
- platform: esphome
logger:
+1
View File
@@ -22,6 +22,7 @@ network:
api:
ota:
- platform: esphome
logger:
+1
View File
@@ -12,6 +12,7 @@ esphome:
logger:
ota:
- platform: esphome
captive_portal:
+1
View File
@@ -12,6 +12,7 @@ esphome:
logger:
ota:
- platform: esphome
captive_portal: