Remove conditional compilation

This commit is contained in:
Mat931
2026-04-30 20:02:07 +02:00
parent 5535b05bf5
commit 68334cdd44
4 changed files with 7 additions and 36 deletions
+1 -12
View File
@@ -16,13 +16,11 @@ from esphome.const import (
CONF_SAFE_MODE,
CONF_VERSION,
)
from esphome.core import CORE, coroutine_with_priority
from esphome.core import coroutine_with_priority
from esphome.coroutine import CoroPriority
import esphome.final_validate as fv
from esphome.types import ConfigType
CONF_ALLOW_PARTITION_ACCESS = "allow_partition_access"
_LOGGER = logging.getLogger(__name__)
@@ -77,10 +75,6 @@ def ota_esphome_final_validate(config):
merged_ota_esphome_configs_by_port[conf_port] = merge_config(
merged_ota_esphome_configs_by_port[conf_port], ota_conf
)
if ota_conf.get(CONF_ALLOW_PARTITION_ACCESS) and not CORE.is_esp32:
raise cv.Invalid(
f"{CONF_ALLOW_PARTITION_ACCESS} is only supported on the esp32"
)
else:
new_ota_conf.append(ota_conf)
@@ -131,7 +125,6 @@ CONFIG_SCHEMA = cv.All(
ln882x=8820,
rtl87xx=8892,
): cv.port,
cv.Optional(CONF_ALLOW_PARTITION_ACCESS, default=False): cv.boolean,
cv.Optional(CONF_PASSWORD): cv.string,
cv.Optional(CONF_NUM_ATTEMPTS): cv.invalid(
f"'{CONF_SAFE_MODE}' (and its related configuration variables) has moved from 'ota' to its own component. See https://esphome.io/components/safe_mode"
@@ -166,10 +159,6 @@ async def to_code(config: ConfigType) -> None:
if config[CONF_PASSWORD]:
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
cg.add_define("USE_OTA_VERSION", config[CONF_VERSION])
if config.get(CONF_ALLOW_PARTITION_ACCESS):
cg.add_define("USE_OTA_PARTITIONS")
# Build flag so lwip_fast_select.c (a .c file that can't include defines.h) sees it.
cg.add_build_flag("-DUSE_OTA_PLATFORM_ESPHOME")
+3 -19
View File
@@ -100,9 +100,6 @@ void ESPHomeOTAComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Password configured");
}
#endif
#ifdef USE_OTA_PARTITIONS
ESP_LOGCONFIG(TAG, " Partition access allowed");
#endif
}
void ESPHomeOTAComponent::loop() {
@@ -211,19 +208,16 @@ void ESPHomeOTAComponent::handle_handshake_() {
const bool supports_compression =
(this->ota_features_ & CLIENT_FEATURE_SUPPORTS_COMPRESSION) != 0 && this->backend_->supports_compression();
// Compose the feature-ack response. When USE_OTA_PARTITIONS is enabled and the client
// Compose the feature-ack response. When the client
// negotiates the extended protocol we emit a 2-byte response (marker + server feature flags);
// otherwise we emit the single-byte legacy response. The #ifdef wraps only the extended-proto
// branch so the legacy branch reads as unconditional code in either build configuration.
#ifdef USE_OTA_PARTITIONS
this->extended_proto_ = (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL) != 0;
if (this->extended_proto_) {
this->handshake_buf_[0] = ota::OTA_RESPONSE_FEATURE_FLAGS;
this->handshake_buf_[1] =
SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS | (supports_compression ? SERVER_FEATURE_SUPPORTS_COMPRESSION : 0);
} else
#endif
{
} else {
this->handshake_buf_[0] =
supports_compression ? ota::OTA_RESPONSE_SUPPORTS_COMPRESSION : ota::OTA_RESPONSE_HEADER_OK;
}
@@ -231,11 +225,7 @@ void ESPHomeOTAComponent::handle_handshake_() {
}
case OTAState::FEATURE_ACK: {
#ifdef USE_OTA_PARTITIONS
const size_t ack_size = this->extended_proto_ ? 2 : 1;
#else
const size_t ack_size = 1;
#endif
if (!this->try_write_(ack_size, LOG_STR("ack feature"))) {
return;
}
@@ -322,9 +312,7 @@ void ESPHomeOTAComponent::handle_data_() {
uint8_t buf[OTA_BUFFER_SIZE];
char *sbuf = reinterpret_cast<char *>(buf);
size_t ota_size;
#ifdef USE_OTA_PARTITIONS
ota::OTAType ota_type = ota::OTA_TYPE_UPDATE_APP;
#endif
#if USE_OTA_VERSION == 2
size_t size_acknowledged = 0;
#endif
@@ -340,7 +328,6 @@ void ESPHomeOTAComponent::handle_data_() {
// Acknowledge auth OK - 1 byte
this->write_byte_(ota::OTA_RESPONSE_AUTH_OK);
#ifdef USE_OTA_PARTITIONS
if (this->extended_proto_) {
// Read ota type, 1 byte
if (!this->readall_(buf, 1)) {
@@ -350,7 +337,6 @@ void ESPHomeOTAComponent::handle_data_() {
ota_type = static_cast<ota::OTAType>(buf[0]);
}
ESP_LOGV(TAG, "OTA type is 0x%02x", ota_type);
#endif
// Read size, 4 bytes MSB first
if (!this->readall_(buf, 4)) {
@@ -371,13 +357,11 @@ void ESPHomeOTAComponent::handle_data_() {
this->notify_state_(ota::OTA_STARTED, 0.0f, 0);
#endif
// This will block for a few seconds as it locks flash
#ifdef USE_OTA_PARTITIONS
if (ota_type != ota::OTA_TYPE_UPDATE_APP) {
error_code = ota::OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}
#endif
// This will block for a few seconds as it locks flash
error_code = this->backend_->begin(ota_size);
if (error_code != ota::OTA_RESPONSE_OK)
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
@@ -91,9 +91,7 @@ class ESPHomeOTAComponent final : public ota::OTAComponent {
std::string password_;
std::unique_ptr<uint8_t[]> auth_buf_;
#endif // USE_OTA_PASSWORD
#ifdef USE_OTA_PARTITIONS
bool extended_proto_{false};
#endif
socket::ListenSocket *server_{nullptr};
std::unique_ptr<socket::Socket> client_;
+3 -3
View File
@@ -782,7 +782,7 @@ def test_perform_ota_version_differences(
def test_perform_ota_extended_protocol_app(
mock_socket: Mock, mock_file: io.BytesIO
) -> None:
"""Test OTA partition table update."""
"""Test OTA extended protocol app update."""
recv_responses = [
bytes([espota2.RESPONSE_OK]), # First byte of version response
bytes([espota2.OTA_VERSION_2_0]), # Version number
@@ -807,7 +807,7 @@ def test_perform_ota_extended_protocol_app(
mock_socket,
"testpass",
mock_file,
"partitions.bin",
"test.bin",
espota2.OTA_TYPE_UPDATE_APP,
)
@@ -850,6 +850,6 @@ def test_perform_ota_extended_protocol_unsupported_type(
mock_socket,
"testpass",
mock_file,
"partitions.bin",
"test.bin",
255,
)