mirror of
https://github.com/esphome/esphome.git
synced 2026-01-09 19:50:49 -07:00
[esphome OTA] Allow compilation on host platform (#11655)
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick@home-assistant.io>
This commit is contained in:
@@ -387,14 +387,14 @@ bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
|
|||||||
while (len - at > 0) {
|
while (len - at > 0) {
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
|
if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
|
||||||
ESP_LOGW(TAG, "Timeout reading %d bytes", len);
|
ESP_LOGW(TAG, "Timeout reading %zu bytes", len);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t read = this->client_->read(buf + at, len - at);
|
ssize_t read = this->client_->read(buf + at, len - at);
|
||||||
if (read == -1) {
|
if (read == -1) {
|
||||||
if (!this->would_block_(errno)) {
|
if (!this->would_block_(errno)) {
|
||||||
ESP_LOGW(TAG, "Read err %d bytes, errno %d", len, errno);
|
ESP_LOGW(TAG, "Read err %zu bytes, errno %d", len, errno);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (read == 0) {
|
} else if (read == 0) {
|
||||||
@@ -414,14 +414,14 @@ bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
|
|||||||
while (len - at > 0) {
|
while (len - at > 0) {
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
|
if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
|
||||||
ESP_LOGW(TAG, "Timeout writing %d bytes", len);
|
ESP_LOGW(TAG, "Timeout writing %zu bytes", len);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t written = this->client_->write(buf + at, len - at);
|
ssize_t written = this->client_->write(buf + at, len - at);
|
||||||
if (written == -1) {
|
if (written == -1) {
|
||||||
if (!this->would_block_(errno)) {
|
if (!this->would_block_(errno)) {
|
||||||
ESP_LOGW(TAG, "Write err %d bytes, errno %d", len, errno);
|
ESP_LOGW(TAG, "Write err %zu bytes, errno %d", len, errno);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from esphome import automation
|
from esphome import automation
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.config_helpers import filter_source_files_from_platform
|
from esphome.config_helpers import filter_source_files_from_platform
|
||||||
@@ -27,6 +29,8 @@ CONF_ON_PROGRESS = "on_progress"
|
|||||||
CONF_ON_STATE_CHANGE = "on_state_change"
|
CONF_ON_STATE_CHANGE = "on_state_change"
|
||||||
|
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ota_ns = cg.esphome_ns.namespace("ota")
|
ota_ns = cg.esphome_ns.namespace("ota")
|
||||||
OTAComponent = ota_ns.class_("OTAComponent", cg.Component)
|
OTAComponent = ota_ns.class_("OTAComponent", cg.Component)
|
||||||
OTAState = ota_ns.enum("OTAState")
|
OTAState = ota_ns.enum("OTAState")
|
||||||
@@ -45,6 +49,10 @@ def _ota_final_validate(config):
|
|||||||
raise cv.Invalid(
|
raise cv.Invalid(
|
||||||
f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality"
|
f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality"
|
||||||
)
|
)
|
||||||
|
if CORE.is_host:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"OTA not available for platform 'host'. OTA functionality disabled."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
FINAL_VALIDATE_SCHEMA = _ota_final_validate
|
FINAL_VALIDATE_SCHEMA = _ota_final_validate
|
||||||
|
|||||||
24
esphome/components/ota/ota_backend_host.cpp
Normal file
24
esphome/components/ota/ota_backend_host.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifdef USE_HOST
|
||||||
|
#include "ota_backend_host.h"
|
||||||
|
|
||||||
|
#include "esphome/core/defines.h"
|
||||||
|
|
||||||
|
namespace esphome::ota {
|
||||||
|
|
||||||
|
// Stub implementation - OTA is not supported on host platform.
|
||||||
|
// All methods return error codes to allow compilation of configs with OTA triggers.
|
||||||
|
|
||||||
|
std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::HostOTABackend>(); }
|
||||||
|
|
||||||
|
OTAResponseTypes HostOTABackend::begin(size_t image_size) { return OTA_RESPONSE_ERROR_UPDATE_PREPARE; }
|
||||||
|
|
||||||
|
void HostOTABackend::set_update_md5(const char *expected_md5) {}
|
||||||
|
|
||||||
|
OTAResponseTypes HostOTABackend::write(uint8_t *data, size_t len) { return OTA_RESPONSE_ERROR_WRITING_FLASH; }
|
||||||
|
|
||||||
|
OTAResponseTypes HostOTABackend::end() { return OTA_RESPONSE_ERROR_UPDATE_END; }
|
||||||
|
|
||||||
|
void HostOTABackend::abort() {}
|
||||||
|
|
||||||
|
} // namespace esphome::ota
|
||||||
|
#endif
|
||||||
21
esphome/components/ota/ota_backend_host.h
Normal file
21
esphome/components/ota/ota_backend_host.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifdef USE_HOST
|
||||||
|
#include "ota_backend.h"
|
||||||
|
|
||||||
|
namespace esphome::ota {
|
||||||
|
|
||||||
|
/// Stub OTA backend for host platform - allows compilation but does not implement OTA.
|
||||||
|
/// All operations return error codes immediately. This enables configurations with
|
||||||
|
/// OTA triggers to compile for host platform during development.
|
||||||
|
class HostOTABackend : public OTABackend {
|
||||||
|
public:
|
||||||
|
OTAResponseTypes begin(size_t image_size) override;
|
||||||
|
void set_update_md5(const char *md5) override;
|
||||||
|
OTAResponseTypes write(uint8_t *data, size_t len) override;
|
||||||
|
OTAResponseTypes end() override;
|
||||||
|
void abort() override;
|
||||||
|
bool supports_compression() override { return false; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace esphome::ota
|
||||||
|
#endif
|
||||||
4
tests/components/ota/test.host.yaml
Normal file
4
tests/components/ota/test.host.yaml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<<: !include common.yaml
|
||||||
|
|
||||||
|
#host platform does not support wifi / network is automatically included
|
||||||
|
wifi: !remove
|
||||||
Reference in New Issue
Block a user