mirror of
https://github.com/esphome/esphome.git
synced 2025-04-06 19:00:29 +01:00
Rename class, adjust namespace
This commit is contained in:
parent
cc8dd92854
commit
af6dffd150
@ -27,31 +27,31 @@ CONF_ON_ERROR = "on_error"
|
||||
CONF_ON_PROGRESS = "on_progress"
|
||||
CONF_ON_STATE_CHANGE = "on_state_change"
|
||||
|
||||
ota_esphome = cg.esphome_ns.namespace("ota_esphome")
|
||||
esphome = cg.esphome_ns.namespace("esphome")
|
||||
|
||||
OTAESPHomeComponent = ota_esphome.class_("OTAESPHomeComponent", cg.Component)
|
||||
OTAESPHomeEndTrigger = ota_esphome.class_(
|
||||
"OTAESPHomeEndTrigger", automation.Trigger.template()
|
||||
ESPHomeOTAComponent = esphome.class_("ESPHomeOTAComponent", cg.Component)
|
||||
ESPHomeOTAEndTrigger = esphome.class_(
|
||||
"ESPHomeOTAEndTrigger", automation.Trigger.template()
|
||||
)
|
||||
OTAESPHomeErrorTrigger = ota_esphome.class_(
|
||||
"OTAESPHomeErrorTrigger", automation.Trigger.template()
|
||||
ESPHomeOTAErrorTrigger = esphome.class_(
|
||||
"ESPHomeOTAErrorTrigger", automation.Trigger.template()
|
||||
)
|
||||
OTAESPHomeProgressTrigger = ota_esphome.class_(
|
||||
"OTAESPHomeProgressTrigger", automation.Trigger.template()
|
||||
ESPHomeOTAProgressTrigger = esphome.class_(
|
||||
"ESPHomeOTAProgressTrigger", automation.Trigger.template()
|
||||
)
|
||||
OTAESPHomeStartTrigger = ota_esphome.class_(
|
||||
"OTAESPHomeStartTrigger", automation.Trigger.template()
|
||||
ESPHomeOTAStartTrigger = esphome.class_(
|
||||
"ESPHomeOTAStartTrigger", automation.Trigger.template()
|
||||
)
|
||||
OTAESPHomeStateChangeTrigger = ota_esphome.class_(
|
||||
"OTAESPHomeStateChangeTrigger", automation.Trigger.template()
|
||||
ESPHomeOTAStateChangeTrigger = esphome.class_(
|
||||
"ESPHomeOTAStateChangeTrigger", automation.Trigger.template()
|
||||
)
|
||||
|
||||
OTAESPHomeState = ota_esphome.enum("OTAESPHomeState")
|
||||
ESPHomeOTAState = esphome.enum("ESPHomeOTAState")
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(OTAESPHomeComponent),
|
||||
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(
|
||||
@ -70,29 +70,29 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
OTAESPHomeStateChangeTrigger
|
||||
ESPHomeOTAStateChangeTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_BEGIN): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAESPHomeStartTrigger),
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPHomeOTAStartTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_END): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAESPHomeEndTrigger),
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPHomeOTAEndTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_ERROR): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAESPHomeErrorTrigger),
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPHomeOTAErrorTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PROGRESS): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
OTAESPHomeProgressTrigger
|
||||
ESPHomeOTAProgressTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
@ -130,7 +130,7 @@ async def to_code(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, [(OTAESPHomeState, "state")], conf)
|
||||
await automation.build_automation(trigger, [(ESPHomeOTAState, "state")], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_BEGIN, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ota_esphome {
|
||||
|
||||
class OTAESPHomeStateChangeTrigger : public Trigger<OTAESPHomeState> {
|
||||
public:
|
||||
@ -65,7 +64,6 @@ class OTAESPHomeErrorTrigger : public Trigger<uint8_t> {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ota_esphome
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_OTA_STATE_CALLBACK
|
||||
|
@ -17,16 +17,15 @@
|
||||
#include <cstdio>
|
||||
|
||||
namespace esphome {
|
||||
namespace ota_esphome {
|
||||
|
||||
static const char *const TAG = "esphome.ota";
|
||||
static constexpr u_int16_t OTA_BLOCK_SIZE = 8192;
|
||||
|
||||
OTAESPHomeComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
ESPHomeOTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
OTAESPHomeComponent::OTAESPHomeComponent() { global_ota_component = this; }
|
||||
ESPHomeOTAComponent::ESPHomeOTAComponent() { global_ota_component = this; }
|
||||
|
||||
void OTAESPHomeComponent::setup() {
|
||||
void ESPHomeOTAComponent::setup() {
|
||||
server_ = socket::socket_ip(SOCK_STREAM, 0);
|
||||
if (server_ == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not create socket");
|
||||
@ -70,7 +69,7 @@ void OTAESPHomeComponent::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void OTAESPHomeComponent::dump_config() {
|
||||
void ESPHomeOTAComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Over-The-Air updates:");
|
||||
ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_);
|
||||
ESP_LOGCONFIG(TAG, " OTA version: %d", USE_OTA_VERSION);
|
||||
@ -80,13 +79,13 @@ void OTAESPHomeComponent::dump_config() {
|
||||
}
|
||||
#endif
|
||||
if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 &&
|
||||
this->safe_mode_rtc_value_ != OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
this->safe_mode_rtc_value_ != ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
ESP_LOGW(TAG, "Last reset occurred too quickly; safe mode will be invoked in %" PRIu32 " restarts",
|
||||
this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_);
|
||||
}
|
||||
}
|
||||
|
||||
void OTAESPHomeComponent::loop() {
|
||||
void ESPHomeOTAComponent::loop() {
|
||||
this->handle_();
|
||||
|
||||
if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) {
|
||||
@ -99,7 +98,7 @@ void OTAESPHomeComponent::loop() {
|
||||
|
||||
static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
|
||||
|
||||
void OTAESPHomeComponent::handle_() {
|
||||
void ESPHomeOTAComponent::handle_() {
|
||||
ota::OTAResponseTypes error_code = ota::OTA_RESPONSE_ERROR_UNKNOWN;
|
||||
bool update_started = false;
|
||||
size_t total = 0;
|
||||
@ -362,7 +361,7 @@ error:
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OTAESPHomeComponent::readall_(uint8_t *buf, size_t len) {
|
||||
bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
|
||||
uint32_t start = millis();
|
||||
uint32_t at = 0;
|
||||
while (len - at > 0) {
|
||||
@ -393,7 +392,7 @@ bool OTAESPHomeComponent::readall_(uint8_t *buf, size_t len) {
|
||||
|
||||
return true;
|
||||
}
|
||||
bool OTAESPHomeComponent::writeall_(const uint8_t *buf, size_t len) {
|
||||
bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
|
||||
uint32_t start = millis();
|
||||
uint32_t at = 0;
|
||||
while (len - at > 0) {
|
||||
@ -421,31 +420,31 @@ bool OTAESPHomeComponent::writeall_(const uint8_t *buf, size_t len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
float OTAESPHomeComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
|
||||
uint16_t OTAESPHomeComponent::get_port() const { return this->port_; }
|
||||
void OTAESPHomeComponent::set_port(uint16_t port) { this->port_ = port; }
|
||||
float ESPHomeOTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
|
||||
uint16_t ESPHomeOTAComponent::get_port() const { return this->port_; }
|
||||
void ESPHomeOTAComponent::set_port(uint16_t port) { this->port_ = port; }
|
||||
|
||||
void OTAESPHomeComponent::set_safe_mode_pending(const bool &pending) {
|
||||
void ESPHomeOTAComponent::set_safe_mode_pending(const bool &pending) {
|
||||
if (!this->has_safe_mode_)
|
||||
return;
|
||||
|
||||
uint32_t current_rtc = this->read_rtc_();
|
||||
|
||||
if (pending && current_rtc != OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
if (pending && current_rtc != ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
ESP_LOGI(TAG, "Device will enter safe mode on next boot");
|
||||
this->write_rtc_(OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC);
|
||||
this->write_rtc_(ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC);
|
||||
}
|
||||
|
||||
if (!pending && current_rtc == OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
if (!pending && current_rtc == ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC) {
|
||||
ESP_LOGI(TAG, "Safe mode pending has been cleared");
|
||||
this->clean_rtc();
|
||||
}
|
||||
}
|
||||
bool OTAESPHomeComponent::get_safe_mode_pending() {
|
||||
return this->has_safe_mode_ && this->read_rtc_() == OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC;
|
||||
bool ESPHomeOTAComponent::get_safe_mode_pending() {
|
||||
return this->has_safe_mode_ && this->read_rtc_() == ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC;
|
||||
}
|
||||
|
||||
bool OTAESPHomeComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) {
|
||||
bool ESPHomeOTAComponent::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;
|
||||
@ -453,7 +452,7 @@ bool OTAESPHomeComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t
|
||||
this->rtc_ = global_preferences->make_preference<uint32_t>(233825507UL, false);
|
||||
this->safe_mode_rtc_value_ = this->read_rtc_();
|
||||
|
||||
bool is_manual_safe_mode = this->safe_mode_rtc_value_ == OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC;
|
||||
bool is_manual_safe_mode = this->safe_mode_rtc_value_ == ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC;
|
||||
|
||||
if (is_manual_safe_mode) {
|
||||
ESP_LOGI(TAG, "Safe mode has been entered manually");
|
||||
@ -487,27 +486,26 @@ bool OTAESPHomeComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void OTAESPHomeComponent::write_rtc_(uint32_t val) {
|
||||
void ESPHomeOTAComponent::write_rtc_(uint32_t val) {
|
||||
this->rtc_.save(&val);
|
||||
global_preferences->sync();
|
||||
}
|
||||
uint32_t OTAESPHomeComponent::read_rtc_() {
|
||||
uint32_t ESPHomeOTAComponent::read_rtc_() {
|
||||
uint32_t val;
|
||||
if (!this->rtc_.load(&val))
|
||||
return 0;
|
||||
return val;
|
||||
}
|
||||
void OTAESPHomeComponent::clean_rtc() { this->write_rtc_(0); }
|
||||
void OTAESPHomeComponent::on_safe_shutdown() {
|
||||
if (this->has_safe_mode_ && this->read_rtc_() != OTAESPHomeComponent::ENTER_SAFE_MODE_MAGIC)
|
||||
void ESPHomeOTAComponent::clean_rtc() { this->write_rtc_(0); }
|
||||
void ESPHomeOTAComponent::on_safe_shutdown() {
|
||||
if (this->has_safe_mode_ && this->read_rtc_() != ESPHomeOTAComponent::ENTER_SAFE_MODE_MAGIC)
|
||||
this->clean_rtc();
|
||||
}
|
||||
|
||||
#ifdef USE_OTA_STATE_CALLBACK
|
||||
void OTAESPHomeComponent::add_on_state_callback(std::function<void(OTAESPHomeState, float, uint8_t)> &&callback) {
|
||||
void ESPHomeOTAComponent::add_on_state_callback(std::function<void(OTAESPHomeState, float, uint8_t)> &&callback) {
|
||||
this->state_callback_.add(std::move(callback));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace ota_esphome
|
||||
} // namespace esphome
|
||||
|
@ -7,14 +7,13 @@
|
||||
#include "esphome/core/preferences.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ota_esphome {
|
||||
|
||||
enum OTAESPHomeState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR };
|
||||
|
||||
/// OTAESPHomeComponent 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 Component {
|
||||
public:
|
||||
OTAESPHomeComponent();
|
||||
ESPHomeOTAComponent();
|
||||
#ifdef USE_OTA_PASSWORD
|
||||
void set_auth_password(const std::string &password) { password_ = password; }
|
||||
#endif // USE_OTA_PASSWORD
|
||||
@ -77,7 +76,6 @@ class ESPHomeOTAComponent : public Component {
|
||||
#endif
|
||||
};
|
||||
|
||||
extern OTAESPHomeComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern ESPHomeOTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
} // namespace ota_esphome
|
||||
} // namespace esphome
|
||||
|
@ -1,7 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import button
|
||||
from esphome.components.esphome.ota import OTAESPHomeComponent
|
||||
from esphome.components.esphome.ota import ESPHomeOTAComponent
|
||||
from esphome.const import (
|
||||
CONF_ESPHOME,
|
||||
DEVICE_CLASS_RESTART,
|
||||
@ -21,7 +21,7 @@ CONFIG_SCHEMA = (
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_RESTART_ALERT,
|
||||
)
|
||||
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(OTAESPHomeComponent)})
|
||||
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(ESPHomeOTAComponent)})
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
@ -8,7 +8,7 @@ namespace safe_mode {
|
||||
|
||||
static const char *const TAG = "safe_mode.button";
|
||||
|
||||
void SafeModeButton::set_ota(ota_esphome::OTAESPHomeComponent *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...");
|
||||
|
@ -10,10 +10,10 @@ namespace safe_mode {
|
||||
class SafeModeButton : public button::Button, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_ota(ota_esphome::OTAESPHomeComponent *ota);
|
||||
void set_ota(esphome::ESPHomeOTAComponent *ota);
|
||||
|
||||
protected:
|
||||
ota_esphome::OTAESPHomeComponent *ota_;
|
||||
esphome::ESPHomeOTAComponent *ota_;
|
||||
void press_action() override;
|
||||
};
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import switch
|
||||
from esphome.components.esphome.ota import OTAESPHomeComponent
|
||||
from esphome.components.esphome.ota import ESPHomeOTAComponent
|
||||
from esphome.const import (
|
||||
CONF_ESPHOME,
|
||||
ENTITY_CATEGORY_CONFIG,
|
||||
@ -20,7 +20,7 @@ CONFIG_SCHEMA = (
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_RESTART_ALERT,
|
||||
)
|
||||
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(OTAESPHomeComponent)})
|
||||
.extend({cv.GenerateID(CONF_ESPHOME): cv.use_id(ESPHomeOTAComponent)})
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
@ -8,7 +8,7 @@ namespace safe_mode {
|
||||
|
||||
static const char *const TAG = "safe_mode_switch";
|
||||
|
||||
void SafeModeSwitch::set_ota(ota_esphome::OTAESPHomeComponent *ota) { this->ota_ = ota; }
|
||||
void SafeModeSwitch::set_ota(esphome::ESPHomeOTAComponent *ota) { this->ota_ = ota; }
|
||||
|
||||
void SafeModeSwitch::write_state(bool state) {
|
||||
// Acknowledge
|
||||
|
@ -10,10 +10,10 @@ namespace safe_mode {
|
||||
class SafeModeSwitch : public switch_::Switch, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_ota(ota_esphome::OTAESPHomeComponent *ota);
|
||||
void set_ota(esphome::ESPHomeOTAComponent *ota);
|
||||
|
||||
protected:
|
||||
ota_esphome::OTAESPHomeComponent *ota_;
|
||||
esphome::ESPHomeOTAComponent *ota_;
|
||||
void write_state(bool state) override;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user