Compare commits

...
Author SHA1 Message Date
Jesse Hills 84079c07e2 Merge pull request #22804 from esphome/bump-2026.11.0b5
2026.11.0b5
2026-11-17 10:27:35 +13:00
J. Nick Koston b7dbf17469 [core] Brace the fast loop check in the unit test main (#22640) 2026-11-17 09:54:53 +13:00
Jesse Hills b91750c325 [tests] Add RawApiClient.read_frame for the serial proxy backpressure test
The serial proxy test from #22687 calls RawApiClient.read_frame, which was
added on dev as part of the store_yaml feature (#16445) and is not on this
release branch. Take tests/integration/raw_api_client.py from that PR so the
test can run; no firmware code changes.
2026-11-17 09:33:39 +13:00
J. Nick Koston 92d2a457fa [epaper_spi] Stop the fast loop after the fill tests (#22638) 2026-11-17 09:00:52 +13:00
Jesse Hills a1fecebb09 Bump version to 2026.11.0b5 2026-11-17 07:30:12 +13:00
J. Nick Koston 32b9d4cbe7 [web_server_base] Bump ESPAsyncWebServer to 3.12.1 (#22682) 2026-11-17 07:30:09 +13:00
Chris c53401a28d [ethernet] Notify IP state listeners from the main loop (#22657) 2026-11-17 07:30:09 +13:00
Miguel Angel Nubla 6b7d17c762 [wifi] Leave the AP before restarting on ESP32 (#22715) 2026-11-17 07:30:09 +13:00
J. Nick Koston 93a3306d82 [libretiny] Free closed TCP connections after about 1 s instead of 120 s (#22696) 2026-11-17 07:30:09 +13:00
J. Nick Koston 54d3477240 [bluetooth_connection] Keep the top half of 32-bit GATT UUIDs on RP2 (#22679) 2026-11-17 07:30:09 +13:00
J. Nick Koston 5f9d83d293 [async_tcp] Use the esphome-libs RPAsyncTCP fork so SSE streams do not stall on RP2040 (#22686) 2026-11-17 07:30:09 +13:00
J. Nick Koston 43864283b4 [web_server_base] Check Basic auth without linking the library's Digest code (#22684) 2026-11-17 07:30:09 +13:00
J. Nick Koston 4efbb38b86 [core] Lock the scheduler against lwIP callbacks on RP2 (#22688) 2026-11-17 07:30:09 +13:00
Keith Burzinski 045374965f [serial_proxy] Keep received bytes when the client's connection is blocked (#22687) 2026-11-17 07:30:09 +13:00
Craig Andrews 39a939720f [epaper_spi] Point the UC8279 gate-count error at the height (#22628) 2026-11-17 07:30:09 +13:00
esphome[bot] a79e51e784 Bump bundled esphome-device-builder to 1.28.1 (#22630)
Co-authored-by: esphome[bot] <115708604+esphome[bot]@users.noreply.github.com>
2026-11-17 07:29:59 +13:00
Chris 517f0d6d48 [usb_host] Respect explicit control transfer size (#22629) 2026-11-17 07:29:14 +13:00
Craig Andrews 1327d0ddc5 [epaper_spi] Mark the frame drawn after fill() (#21505) 2026-11-17 07:29:14 +13:00
J. Nick Koston 2adb544e7a [uart] Stop ESP8266 software serial TX from tripping the Wi-Fi watchdog above 19200 baud (#22603) 2026-11-17 07:29:14 +13:00
J. Nick Koston 48905beb96 [zephyr] Add ISR pin_mode so dht links on nRF52 2026-11-17 07:29:14 +13:00
J. Nick Koston ed401814bf [dht] Negate sign-and-magnitude negative temperatures instead of inverting them (#22615) 2026-11-17 07:29:14 +13:00
J. Nick Koston 0245db6285 [host] Add the empty ISRInternalGPIOPin::pin_mode (#22616) 2026-11-17 07:29:14 +13:00
J. Nick Koston 07ee0f9f04 [logger] Fix LibreTiny task log messages landing unaligned and out of step (#22594) 2026-11-17 07:29:14 +13:00
Jesse HillsandJ. Nick Koston 3d34ad572b [api] Send the platform of each wizard entity (#22580)
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-11-17 07:29:13 +13:00
65 changed files with 1399 additions and 161 deletions

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ PROJECT_NAME = ESPHome
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = 2026.11.0b4
PROJECT_NUMBER = 2026.11.0b5
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
+1 -1
View File
@@ -22,7 +22,7 @@ RUN \
-r /requirements.txt
# Install the ESPHome Device Builder dashboard.
RUN uv pip install --no-cache-dir esphome-device-builder==1.26.0
RUN uv pip install --no-cache-dir esphome-device-builder==1.28.1
RUN \
platformio settings set enable_telemetry No \
+6 -1
View File
@@ -452,7 +452,8 @@ message DeviceWizardRequest {
//
// {"version": 1,
// "pages": [{"title": "...", "description": "...",
// "entities": [{"key": 123, "device_id": 456, "description": "...", "role": "..."}],
// "entities": [{"key": 123, "platform": "sensor", "device_id": 456,
// "description": "...", "role": "..."}],
// "inputs": [{"key": 789, "description": "...", "multiple": true, "max_length": 1023,
// "entity_filters": [{"integration": "...", "domain": ["..."],
// "device_class": ["..."], "supported_features": ["..."]}]}]}]}
@@ -465,6 +466,10 @@ message DeviceWizardRequest {
// An entity is an entity of this device that the page shows to the user (for example to
// turn it on or off). Its key is the key ListEntitiesResponse sends for it. Its
// device_id is the device_id ListEntitiesResponse sends, and is left out when that is 0.
// Its platform is the platform of the entity (sensor, switch and so on; date, time or
// datetime for a datetime entity). A key is only unique within its platform and device, so
// a client matches an entity by platform, device_id and key together. A client that does
// not know platform ignores it, so the version stays 1.
// Its role, when set, is a stable name that the device's firmware gives the entity
// (always <component>.<name>), so a client can recognise it. The user cannot change it,
// and no two entities of a wizard share one.
+57 -3
View File
@@ -257,6 +257,47 @@ WIZARD_DOMAIN_LIMITED_PLATFORMS = (
)
# The C++ base class of each kind of entity a wizard page can show, and the platform an API client
# knows it by. An entity key is only unique within its platform, so the wizard sends both.
WIZARD_ENTITY_PLATFORMS: dict[str, str] = {
"alarm_control_panel::AlarmControlPanel": "alarm_control_panel",
"binary_sensor::BinarySensor": "binary_sensor",
"button::Button": "button",
"climate::Climate": "climate",
"cover::Cover": "cover",
"datetime::DateEntity": "date",
"datetime::DateTimeEntity": "datetime",
"datetime::TimeEntity": "time",
"event::Event": "event",
"fan::Fan": "fan",
"infrared::Infrared": "infrared",
"light::LightState": "light",
"lock::Lock": "lock",
"number::Number": "number",
"radio_frequency::RadioFrequency": "radio_frequency",
"select::Select": "select",
"sensor::Sensor": "sensor",
"switch_::Switch": "switch",
"text::Text": "text",
"text_sensor::TextSensor": "text_sensor",
"update::UpdateEntity": "update",
"valve::Valve": "valve",
"water_heater::WaterHeater": "water_heater",
}
def _wizard_entity_platform(
config: fv.FinalValidateConfig, entity_id: ID
) -> str | None:
"""The platform of an entity, from the C++ class it is declared with, as some entities are not
declared under their platform (the sensors of gps:, for example)."""
declared: ID = config.get_config_for_path(config.get_path_for_id(entity_id))
for cls in (declared.type, *declared.type.parents):
if (platform := WIZARD_ENTITY_PLATFORMS.get(str(cls))) is not None:
return platform
return None
def wizard_input_ids(api_config: ConfigType) -> set[str]:
"""The IDs of the entities that are linked inputs of the wizard in the given api config."""
if (wizard := api_config.get(CONF_WIZARD)) is None:
@@ -361,7 +402,8 @@ def _validate_wizard_input(conf: ConfigType) -> ConfigType:
def _validate_wizard_entity_exposed(value: ID) -> ID:
"""Reject entities that are internal, as they are not exposed over the API, or have no name.
"""Reject entities that are internal, as they are not exposed over the API, have no name, or
are of a platform the wizard does not know.
The key a client knows an entity by is a hash of its name. Without a name of its own, the
device works the name out at runtime from its friendly name, which can add the MAC address,
@@ -378,6 +420,10 @@ def _validate_wizard_entity_exposed(value: ID) -> ID:
f"Entity '{value.id}' has no name of its own, so its key is not known "
"when the wizard is built. Give it a name to use it in the wizard"
)
if _wizard_entity_platform(fv.full_config.get(), value) is None:
raise cv.Invalid(
f"Entity '{value.id}' is not of a platform the wizard can show"
)
return value
@@ -446,7 +492,10 @@ def _entity_document(
) -> ConfigType:
"""An entity of the device that the page shows, keyed as ListEntitiesResponse keys it."""
_, declaration = _wizard_input_declaration(config, conf[CONF_ID])
document: ConfigType = {"key": fnv1_hash_object_id(declaration[CONF_NAME])}
document: ConfigType = {
"key": fnv1_hash_object_id(declaration[CONF_NAME]),
"platform": _wizard_entity_platform(config, conf[CONF_ID]),
}
if (device := declaration.get(CONF_DEVICE_ID)) is not None:
document["device_id"] = fnv1a_32bit_hash(device.id)
if description := conf.get(CONF_DESCRIPTION):
@@ -476,7 +525,8 @@ def wizard_document(wizard: ConfigType, config: fv.FinalValidateConfig) -> Confi
{"version": 1,
"pages": [{"title": "...", "description": "...",
"entities": [{"key": 123, "device_id": 456, "description": "...", "role": "..."}],
"entities": [{"key": 123, "platform": "sensor", "device_id": 456,
"description": "...", "role": "..."}],
"inputs": [{"key": 789, "description": "...", "multiple": true, "max_length": 1023,
"entity_filters": [{"integration": "...", "domain": ["..."],
"device_class": ["..."], "supported_features": ["..."]}]}]}]}
@@ -487,6 +537,10 @@ def wizard_document(wizard: ConfigType, config: fv.FinalValidateConfig) -> Confi
- An entity key is the key ListEntitiesResponse sends for the entity: the FNV-1 hash of the
object id made from its name (entity_helpers). device_id is the hash of the ESPHome id of
the device it belongs to (esphome/core/config.py), and is left out for the main device.
- An entity platform is the platform of the entity: sensor, switch and so on, or date, time or
datetime for an entity of the datetime component. A key is only unique within its platform
and device, so a client matches an entity by platform, device_id and key together. A client
that does not know platform ignores it, so the version stays 1.
- An entity role is a stable name, always <component>.<name>, that the component of the entity
gives it with register_entity_role, so Home Assistant can recognise the entity. The user
cannot set or change it, and no two entities of a wizard share one. A client that does not
+13 -5
View File
@@ -35,15 +35,23 @@ async def to_code(config: ConfigType) -> None:
add_idf_component(name="esp32async/asynctcp", ref="3.4.91")
elif CORE.is_libretiny:
# https://github.com/ESP32Async/AsyncTCP
cg.add_library("ESP32Async/AsyncTCP", "3.4.5")
cg.add_library("ESP32Async/AsyncTCP", "3.5.0")
# AsyncTCP 3.5.0 debug logs call xPortGetCoreID(), which LibreTiny (single core) lacks; they are compiled
# in by LibreTiny's debug level or CONFIG_ASYNC_TCP_DEBUG, so the call always expands to int(), which is 0
# (a function-like -D does not survive PlatformIO's build flags)
cg.add_build_flag("-DxPortGetCoreID=int")
elif CORE.is_esp8266:
# https://github.com/ESP32Async/ESPAsyncTCP
cg.add_library("ESP32Async/ESPAsyncTCP", "2.0.0")
elif CORE.is_rp2:
# https://github.com/ayushsharma82/RPAsyncTCP
# RPAsyncTCP is a drop-in replacement for AsyncTCP_RP2040W with better
# ESPAsyncWebServer compatibility
cg.add_library("ayushsharma82/RPAsyncTCP", "1.3.2")
# https://github.com/esphome-libs/RPAsyncTCP, a fork of ayushsharma82/RPAsyncTCP
# 1.3.2 that counts queued bytes before lwIP can send and ACK them, so
# canSend() does not stay false and SSE streams do not stall (wired Ethernet)
cg.add_library(
"RPAsyncTCP",
"1.3.2",
"https://github.com/esphome-libs/RPAsyncTCP#1.3.2-esphome.1",
)
# Other platforms (host, etc) use socket-based implementation
@@ -12,6 +12,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#ifdef USE_ESP32
#include <esp_err.h>
@@ -136,6 +137,18 @@ inline void fill_128bit_uuid_array(std::array<uint64_t, 2> &out, const ble_devic
out[1] = 0x800000805F9B34FBULL; // Base UUID bytes 0-7
}
/// A 128-bit UUID in big-endian (printable) order, as its 16 or 32-bit form when it is on the Bluetooth base UUID.
inline ble_device_base::ESPBTUUID uuid_from_be128(const uint8_t *uuid128) {
using ble_device_base::ESPBTUUID;
// 0000xxxx-0000-1000-8000-00805F9B34FB without its first 4 bytes
static constexpr uint8_t BASE_UUID_TAIL[12] = {0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
if (memcmp(uuid128 + 4, BASE_UUID_TAIL, sizeof(BASE_UUID_TAIL)) != 0)
return ESPBTUUID::from_raw_reversed(uuid128);
const uint32_t value = encode_uint32(uuid128[0], uuid128[1], uuid128[2], uuid128[3]);
return value <= UINT16_MAX ? ESPBTUUID::from_uint16(value) : ESPBTUUID::from_uint32(value);
}
/// Fill the UUID in the appropriate wire format based on client support and
/// UUID type (128-bit array for old clients or 128-bit UUIDs, short form
/// otherwise).
@@ -16,7 +16,6 @@ namespace esphome::bluetooth_connection {
ESPHOME_LOG_TAG(TAG, "bluetooth_connection");
using ble_device_base::ESPBTUUID;
using ble_device_base::GATT_ERR_NOT_CONNECTED;
using ble_device_base::GATT_ERR_NO_MEMORY;
@@ -63,14 +62,6 @@ btstack_packet_callback_registration_t RP2GattClient::sm_event_registration = {}
RP2GattClient *RP2GattClient::connect_owner = nullptr;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
static ESPBTUUID uuid_from_btstack(uint16_t uuid16, const uint8_t uuid128[16]) {
if (uuid16 != 0) {
return ESPBTUUID::from_uint16(uuid16);
}
// BTstack structs carry the 128-bit form big-endian (printable order).
return ESPBTUUID::from_raw_reversed(uuid128);
}
void RP2GattClient::setup() {
// Pre-create every pool entry so the packet handlers' allocate() calls are
// always a free-list pop -- the IRQ path must never reach malloc().
@@ -300,8 +291,9 @@ void RP2GattClient::handle_gatt_event_irq_(uint8_t event_type, const uint8_t *pa
}
gatt_client_service_t service;
gatt_event_service_query_result_get_service(packet, &service);
// Not uuid16: BTstack stores a 32-bit base UUID there truncated to its low 16 bits
auto &dst = this->arena_->services[this->service_count_];
dst.uuid = uuid_from_btstack(service.uuid16, service.uuid128);
dst.uuid = uuid_from_be128(service.uuid128);
dst.start_handle = service.start_group_handle;
dst.end_handle = service.end_group_handle;
dst.first_characteristic = 0;
@@ -320,7 +312,7 @@ void RP2GattClient::handle_gatt_event_irq_(uint8_t event_type, const uint8_t *pa
gatt_client_characteristic_t characteristic;
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
auto &dst = this->arena_->characteristics[this->char_count_];
dst.uuid = uuid_from_btstack(characteristic.uuid16, characteristic.uuid128);
dst.uuid = uuid_from_be128(characteristic.uuid128);
dst.value_handle = characteristic.value_handle;
dst.end_handle = characteristic.end_handle;
dst.properties = static_cast<uint8_t>(characteristic.properties);
@@ -340,7 +332,7 @@ void RP2GattClient::handle_gatt_event_irq_(uint8_t event_type, const uint8_t *pa
gatt_client_characteristic_descriptor_t descriptor;
gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &descriptor);
auto &dst = this->arena_->descriptors[this->desc_count_];
dst.uuid = uuid_from_btstack(descriptor.uuid16, descriptor.uuid128);
dst.uuid = uuid_from_be128(descriptor.uuid128);
dst.handle = descriptor.handle;
this->desc_count_++;
break;
+2 -9
View File
@@ -195,14 +195,7 @@ bool HOT IRAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, bool r
}
} else {
uint16_t raw_humidity = encode_uint16(data[0], data[1]);
uint16_t raw_temperature = encode_uint16(data[2], data[3]);
if (raw_temperature & 0x8000) {
if (!(raw_temperature & 0x4000))
raw_temperature = ~(raw_temperature & 0x7FFF);
} else if (raw_temperature & 0x800) {
raw_temperature |= 0xf000;
}
const int16_t raw_temperature = decode_dht22_temperature(encode_uint16(data[2], data[3]));
if (raw_temperature == 1 && raw_humidity == 10) {
if (report_errors) {
@@ -214,7 +207,7 @@ bool HOT IRAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, bool r
*humidity = static_cast<float>(raw_humidity) * 0.1f;
if (*humidity > 100.0f)
*humidity = NAN;
*temperature = static_cast<int16_t>(raw_temperature) * 0.1f;
*temperature = raw_temperature * 0.1f;
}
if (*temperature == 0.0f && (*humidity == 1.0f || *humidity == 2.0f)) {
+12
View File
@@ -17,6 +17,18 @@ enum DHTModel : uint8_t {
DHT_MODEL_DHT22_TYPE2
};
/// Convert a DHT22-family temperature reading to tenths of a degree. Sensors encode negative values in one of three
/// ways: sign and magnitude (bit 15 set, bit 14 clear), 16-bit two's complement, or 12-bit two's complement (bit 11).
inline int16_t decode_dht22_temperature(uint16_t raw) {
if (raw & 0x8000) {
if (!(raw & 0x4000))
return static_cast<int16_t>(-static_cast<int16_t>(raw & 0x7FFF));
} else if (raw & 0x800) {
raw |= 0xF000;
}
return static_cast<int16_t>(raw);
}
/// Component for reading temperature/humidity measurements from DHT11/DHT22 sensors.
class DHT final : public PollingComponent {
SUB_SENSOR(temperature)
@@ -30,7 +30,7 @@ void EPaperJD79660::fill(Color color) {
// We store 4 pixels per byte
this->buffer_.fill(pixel_color | (pixel_color << 2) | (pixel_color << 4) | (pixel_color << 6));
this->drawn_since_hash_ = true;
this->mark_all_drawn_();
}
void HOT EPaperJD79660::draw_pixel_at(int x, int y, Color color) {
@@ -232,7 +232,7 @@ void EPaperT133A01::fill(Color color) {
}
auto pixel_color = color_to_index(color);
this->buffer_.fill(pixel_color + (pixel_color << 4));
this->drawn_since_hash_ = true;
this->mark_all_drawn_();
}
void EPaperT133A01::draw_pixel_at(int x, int y, Color color) {
@@ -12,7 +12,12 @@ height of them; offset_height (under dimensions) is a property of the panel.
from typing import Any
import esphome.config_validation as cv
from esphome.const import CONF_DATA_RATE, CONF_DIMENSIONS, CONF_OFFSET_HEIGHT
from esphome.const import (
CONF_DATA_RATE,
CONF_DIMENSIONS,
CONF_HEIGHT,
CONF_OFFSET_HEIGHT,
)
from esphome.types import ConfigType
from . import EpaperModel, ultrachip_resolution
@@ -39,10 +44,11 @@ class UC8279(EpaperModel):
def validate_config(self, config: ConfigType) -> ConfigType:
_width, height = self.get_dimensions(config)
# The models' defaults fit, so this fails only with explicit dimensions, which always have a height
if self.get_offset_height(config) + height > 0xFFFF:
raise cv.Invalid(
f"{CONF_OFFSET_HEIGHT} plus the panel height must not exceed {0xFFFF} gates",
path=[CONF_DIMENSIONS, CONF_OFFSET_HEIGHT],
path=[CONF_DIMENSIONS, CONF_HEIGHT],
)
return config
@@ -7,6 +7,8 @@
#include "esphome/core/automation.h"
#include "esphome/components/network/ip_address.h"
#include <atomic>
#ifdef USE_ETHERNET
#ifdef USE_ESP32
@@ -366,6 +368,9 @@ class EthernetComponent final : public Component {
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
StaticVector<EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS> ip_state_listeners_;
#ifdef USE_ESP32
std::atomic<uint8_t> ip_state_pending_{0};
#endif
#endif
#ifdef USE_ETHERNET_CONNECT_TRIGGER
@@ -129,6 +129,14 @@ void EthernetComponent::log_error_and_mark_failed_(esp_err_t err, const char *me
void EthernetComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
// The IP events arrive on the event task; the listeners publish states, so notify them here
if (this->ip_state_pending_.load(std::memory_order_acquire) != 0) {
this->ip_state_pending_.store(0, std::memory_order_relaxed);
this->notify_ip_state_listeners_();
}
#endif
switch (this->state_) {
case EthernetComponentState::STOPPED:
if (this->started_) {
@@ -770,7 +778,8 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
event_name = "ETH connected";
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
global_eth_component->notify_ip_state_listeners_();
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
global_eth_component->enable_loop_soon_any_context();
#endif
#if USE_NETWORK_IPV6
// Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
@@ -798,6 +807,9 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
global_eth_component->got_ipv4_address_ = true;
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
@@ -805,9 +817,6 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
global_eth_component->connected_ = true;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif /* USE_NETWORK_IPV6 */
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#if USE_NETWORK_IPV6
@@ -819,6 +828,9 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
// after a link flap fires another event for the same address.
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
global_eth_component->ipv6_count_ = esp_netif_get_all_ip6(global_eth_component->eth_netif_, if_ip6s);
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ =
global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
@@ -827,9 +839,6 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#endif /* USE_NETWORK_IPV6 */
+3
View File
@@ -50,6 +50,9 @@ void IRAM_ATTR ISRInternalGPIOPin::clear_interrupt() {
auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
ESP_LOGD(TAG, "Clearing interrupt for pin %d", arg->pin);
}
void IRAM_ATTR ISRInternalGPIOPin::pin_mode(gpio::Flags flags) {
// pass
}
} // namespace esphome
+7
View File
@@ -387,6 +387,7 @@ def _configure_lwip(config: dict) -> None:
MEMP_NUM_NETCONN 0 10 38 4***** =sum =sum
MEMP_NUM_NETBUF 0 2 16 2***** 8 4
MEMP_NUM_TCPIP_MSG_INPKT 4 8 16 8***** 12 8
TCP_MSL (ms) 60000† 60000 250 60000 60000 250
* ESP8266/ESP32/LN882H use MEM_LIBC_MALLOC=1 (system heap, no dedicated pool).
ESP8266/ESP32 also use MEMP_MEM_MALLOC=1 (MEMP pools from heap, not static).
@@ -394,6 +395,8 @@ def _configure_lwip(config: dict) -> None:
*** BK LT overlay: MAX_SOCKETS_UDP+2+1 = 25.
**** RTL/LN LT overlay overrides to flat 7.
***** Not defined in RTL SDK — lwIP opt.h defaults shown.
† ESP8266's lwIP also caps TIME_WAIT at 5 PCBs; LibreTiny's stock lwIP has no
cap once MEMP_MEM_MALLOC=1.
"dynamic" = auto-calculated from component socket registrations via
socket.get_socket_counts() with minimums of 8 TCP / 6 UDP.
"""
@@ -470,6 +473,10 @@ def _configure_lwip(config: dict) -> None:
# both ship with MEMP_MEM_MALLOC=1.
lwip_opts.append("MEM_LIBC_MALLOC=1")
lwip_opts.append("MEMP_MEM_MALLOC=1")
# With MEMP_MEM_MALLOC=1, MEMP_NUM_TCP_PCB no longer limits PCBs, so every
# connection the device closes keeps its ~176 B PCB on the heap for TIME_WAIT
# (2 * TCP_MSL, 120 s by default). BK's lwIP port already uses 250.
lwip_opts.append("TCP_MSL=250")
# BK72XX-specific: PBUF_POOL_SIZE override
# BK SDK "reduced plan" sets this to only 3 — too few for multiple
@@ -25,7 +25,7 @@ size_t TaskLogBuffer::available_contiguous_space_() const {
// head is ahead of or equal to tail
// Available space is from head to end, plus from start to tail
// But for contiguous, just from head to end (minus 1 to avoid head==tail ambiguity)
size_t space_to_end = ESPHOME_TASK_LOG_BUFFER_SIZE - this->head_;
size_t space_to_end = CAPACITY - this->head_;
if (this->tail_ == 0) {
// Can't use the last byte or head would equal tail
return space_to_end > 0 ? space_to_end - 1 : 0;
@@ -54,19 +54,15 @@ bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &tex
return false;
}
// Read message header from tail
LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + this->tail_);
// Check for padding marker (indicates wrap-around)
// We check the level field since valid levels are 0-7, and 0xFF indicates padding
if (msg->level == PADDING_MARKER_LEVEL) {
// Skip to start of buffer and re-read
// Less than a header before the end means the writer wrapped without a marker
if (CAPACITY - this->tail_ < sizeof(LogMessage) ||
reinterpret_cast<LogMessage *>(this->storage_ + this->tail_)->level == PADDING_MARKER_LEVEL) {
this->tail_ = 0;
msg = reinterpret_cast<LogMessage *>(this->storage_);
}
LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + this->tail_);
message = msg;
text_length = msg->text_length;
this->current_message_size_ = message_total_size(msg->text_length);
text_length = msg->text_length;
// Keep mutex held until release_message_main_loop()
return true;
@@ -77,7 +73,7 @@ void TaskLogBuffer::release_message_main_loop() {
this->tail_ += this->current_message_size_;
// Handle wrap-around if we've reached the end
if (this->tail_ >= ESPHOME_TASK_LOG_BUFFER_SIZE) {
if (this->tail_ >= CAPACITY) {
this->tail_ = 0;
}
@@ -116,34 +112,22 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
return false; // Mutex busy, fall back to direct output
}
// Check if we have enough contiguous space
size_t contiguous = this->available_contiguous_space_();
if (contiguous < total_size) {
// Not enough contiguous space at end
// Check if we can wrap around
size_t space_at_start = (this->head_ >= this->tail_) ? this->tail_ : 0;
if (space_at_start > 0) {
space_at_start--; // Leave 1 byte gap to distinguish full from empty
}
// Need at least enough space to safely write padding marker (level field is at end of struct)
constexpr size_t padding_marker_min_space = offsetof(LogMessage, level) + 1;
if (space_at_start >= total_size && this->head_ > 0 && contiguous >= padding_marker_min_space) {
// Add padding marker (set level field to indicate this is padding, not a real message)
LogMessage *padding = reinterpret_cast<LogMessage *>(this->storage_ + this->head_);
padding->level = PADDING_MARKER_LEVEL;
this->head_ = 0;
} else {
// Not enough space anywhere, or can't safely write padding marker
size_t write_pos = this->head_;
if (this->available_contiguous_space_() < total_size) {
// Wrap only if the start has room; head == tail would mean empty
if (this->head_ < this->tail_ || this->head_ == 0 || this->tail_ <= total_size) {
xSemaphoreGive(this->mutex_);
return false;
}
// Mark the skipped end when a header fits there
if (CAPACITY - this->head_ >= sizeof(LogMessage)) {
reinterpret_cast<LogMessage *>(this->storage_ + this->head_)->level = PADDING_MARKER_LEVEL;
}
write_pos = 0;
}
// Write message header
LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + this->head_);
LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + write_pos);
msg->level = level;
msg->tag = tag;
msg->line = line;
@@ -165,18 +149,15 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
return false;
}
// Remove trailing newlines
// Trim before storing so the next message reuses the freed bytes
while (text_length > 0 && text_area[text_length - 1] == '\n') {
text_length--;
}
text_area[text_length] = '\0';
msg->text_length = text_length;
// Advance head
this->head_ += total_size;
// Handle wrap-around (shouldn't happen due to contiguous space check, but be safe)
if (this->head_ >= ESPHOME_TASK_LOG_BUFFER_SIZE) {
this->head_ = write_pos + message_total_size(text_length);
if (this->head_ >= CAPACITY) {
this->head_ = 0;
}
@@ -38,7 +38,7 @@ namespace esphome::logger {
* - Variable-size messages with header + text stored contiguously (NOSPLIT style)
* - FreeRTOS mutex protects all buffer operations
* - Volatile counter enables fast has_messages() without lock overhead
* - If message doesn't fit at end, padding is added and message wraps to start
* - A message that doesn't fit at the end wraps to the start, after a padding marker if a header fits there
*/
class TaskLogBuffer {
public:
@@ -77,16 +77,22 @@ class TaskLogBuffer {
inline bool HOT has_messages() const { return this->message_count_ != 0; }
// Get the total buffer size in bytes
static constexpr size_t size() { return ESPHOME_TASK_LOG_BUFFER_SIZE; }
static constexpr size_t size() { return CAPACITY; }
private:
// Calculate total size needed for a message (header + text + null terminator)
static inline size_t message_total_size(size_t text_length) { return sizeof(LogMessage) + text_length + 1; }
// Keep headers aligned: BK72xx silently misreads unaligned words instead of faulting
static constexpr size_t ALIGNMENT = alignof(LogMessage);
static constexpr size_t CAPACITY = ESPHOME_TASK_LOG_BUFFER_SIZE & ~(ALIGNMENT - 1);
// Header + text + null, rounded up to keep the next header aligned
static constexpr size_t message_total_size(size_t text_length) {
return (sizeof(LogMessage) + text_length + 1 + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
}
// Calculate available contiguous space at write position
size_t available_contiguous_space_() const;
uint8_t storage_[ESPHOME_TASK_LOG_BUFFER_SIZE]; // Embedded in Logger (no separate heap allocation)
alignas(LogMessage) uint8_t storage_[CAPACITY]; // Embedded in Logger (no separate heap allocation)
size_t head_{0}; // Write position
size_t tail_{0}; // Read position
@@ -112,6 +112,16 @@ void SerialProxy::loop() {
#ifdef USE_API
void __attribute__((noinline)) SerialProxy::read_and_send_(size_t available) {
// A message the subscriber's connection cannot take is lost, so the bytes stay in the
// UART's buffer until it can. A tap is still served: its answers must not wait on the network.
if (this->api_connection_ != nullptr &&
#ifdef USE_SERIAL_PROXY_TAP
!this->tap_observing_() &&
#endif
!this->api_connection_->try_to_clear_buffer(false)) {
return;
}
// Read in chunks up to SERIAL_PROXY_MAX_READ_SIZE
uint8_t buffer[SERIAL_PROXY_MAX_READ_SIZE];
size_t to_read = std::min(available, sizeof(buffer));
@@ -19,6 +19,10 @@ namespace esphome::uart {
ESPHOME_LOG_TAG(TAG, "uart");
// Edge decoder up to this baud rate; at 38400 WiFi interrupt latency corrupts bytes.
static constexpr uint32_t SW_SERIAL_EDGE_MODE_MAX_BAUD = 19200;
// TX locks interrupts per bit up to this baud rate and per byte above it. At 38400 byte locks still trip the Wi-Fi
// watchdog when every byte is below 0x80, while an interrupt between bits leaves the bytes intact; at 115200 it
// stretches bits enough to corrupt them.
static constexpr uint32_t SW_SERIAL_TX_BIT_LOCK_MAX_BAUD = 38400;
// Edge decoder time base: 1/16 us from the 1 MHz system timer, which keeps its rate during a
// CpuFrequencyBoost; the cycle counter does not.
static constexpr uint32_t RX_TICKS_PER_SECOND = 16000000;
@@ -180,9 +184,11 @@ void ESP8266UartComponent::dump_config() {
this->baud_rate_ <= SW_SERIAL_EDGE_MODE_MAX_BAUD ? LOG_STR_LITERAL("edge")
: LOG_STR_LITERAL("start bit sampler"));
}
if (this->tx_pin_ != nullptr && this->baud_rate_ <= SW_SERIAL_EDGE_MODE_MAX_BAUD &&
if (this->tx_pin_ != nullptr && this->baud_rate_ <= SW_SERIAL_TX_BIT_LOCK_MAX_BAUD &&
this->sw_serial_->locks_whole_frame()) {
ESP_LOGW(TAG, " TX locks interrupts for whole bytes: another software UART receives above %u baud",
ESP_LOGW(TAG,
" TX locks interrupts for whole bytes because a software UART receives above %u baud; Wi-Fi "
"watchdog resets are possible under load",
(unsigned) SW_SERIAL_EDGE_MODE_MAX_BAUD);
}
}
@@ -270,7 +276,7 @@ void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_p
if (this->gpio_rx_pin_ != nullptr)
this->gpio_rx_pin_->detach_interrupt();
// This instance counted itself if its RX ran the whole-byte interrupt
if (this->lock_frame_ && this->gpio_rx_pin_ != nullptr)
if (this->blocking_rx_ && this->gpio_rx_pin_ != nullptr)
blocking_rx_count--;
// RX disabled: forget the old pin, so a later reload does not remove this instance from the count again
if (rx_pin == nullptr)
@@ -280,9 +286,8 @@ void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_p
this->data_bits_ = data_bits;
this->parity_ = parity;
const bool edge_mode = baud_rate <= SW_SERIAL_EDGE_MODE_MAX_BAUD;
// Above the edge mode limit a bit is 52 us or less, so an interrupt between bits would stretch one; the whole frame
// is locked instead, well under 1 ms
this->lock_frame_ = !edge_mode;
this->blocking_rx_ = !edge_mode;
this->lock_frame_ = baud_rate > SW_SERIAL_TX_BIT_LOCK_MAX_BAUD;
// to_isr() allocates, so call it only for a new or changed pin
if (tx_pin != nullptr) {
if (tx_pin != this->gpio_tx_pin_)
@@ -400,19 +405,24 @@ void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
this->tx_pin_.digital_write(false);
start = arch_get_cpu_cycle_count();
}
uint32_t wait = bit_time;
for (int i = 0; i < this->data_bits_; i++) {
bool bit = data & (1 << i);
this->write_bit_(bit, &wait, start, bit_time);
if (need_parity_bit)
parity_bit ^= bit;
// Data bits LSB first, then the parity bit
uint32_t frame = data & ((1u << this->data_bits_) - 1);
uint32_t num_bits = this->data_bits_;
if (need_parity_bit) {
parity_bit ^= __builtin_parity(frame);
frame |= uint32_t(parity_bit) << num_bits++;
}
if (need_parity_bit)
this->write_bit_(parity_bit, &wait, start, bit_time);
// Stop bits: write_bit_ waits before its edge, so the stop bit is held with wait_
// From the edge after the last 0 the line stays high through the stop bits
const uint32_t zeros = ~frame & ((1u << num_bits) - 1);
const uint32_t high_from = zeros == 0 ? 0 : 32 - __builtin_clz(zeros);
uint32_t wait = bit_time;
for (uint32_t i = 0; i < high_from; i++)
this->write_bit_((frame >> i) & 1, &wait, start, bit_time);
this->write_bit_(true, &wait, start, bit_time);
this->wait_(&wait, start, bit_time);
if (this->stop_bits_ == 2)
// Hold that high run with interrupts on: one that runs past it only delays the next start bit, and back-to-back
// byte locks with no gap between them trip the Wi-Fi watchdog
frame_lock.reset();
for (uint32_t i = high_from; i < num_bits + this->stop_bits_; i++)
this->wait_(&wait, start, bit_time);
}
void IRAM_ATTR ESP8266SoftwareSerial::wait_(uint32_t *wait, const uint32_t &start, uint32_t bit_time) {
@@ -26,8 +26,8 @@ class ESP8266SoftwareSerial {
size_t available();
/// TX holds interrupts off for whole frames: above the edge mode limit, or while any instance's RX interrupt reads
/// whole bytes (it must not run between TX bits).
/// TX holds interrupts off for each byte up to the edge into its trailing run of 1 bits, rather than per bit: above
/// the per-bit lock limit, or while any instance's RX interrupt reads whole bytes (it must not run between TX edges).
bool locks_whole_frame() const { return this->lock_frame_ || blocking_rx_count != 0; }
protected:
@@ -56,6 +56,7 @@ class ESP8266SoftwareSerial {
uint8_t stop_bits_;
uint8_t data_bits_;
bool lock_frame_{false};
bool blocking_rx_{false}; // this instance's RX reads whole bytes in its interrupt
static inline uint8_t blocking_rx_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
};
+2 -1
View File
@@ -16,6 +16,7 @@ from esphome.components.esp32 import (
idf_version,
only_on_variant,
require_idf_version,
set_idf_sdkconfig_default,
)
import esphome.config_validation as cv
from esphome.const import CONF_DEVICES, CONF_FRAMEWORK, CONF_ID
@@ -304,7 +305,7 @@ async def to_code(config: ConfigType) -> None:
# IDF 6.0 moved USB host to an external component
if idf_version() >= cv.Version(6, 0, 0):
add_idf_component(name="espressif/usb", ref="1.4.1")
add_idf_sdkconfig_option("CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE", 1024)
set_idf_sdkconfig_default("CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE", 1024)
if config.get(CONF_ENABLE_HUBS):
add_idf_sdkconfig_option("CONFIG_USB_HOST_HUBS_SUPPORTED", True)
+36 -1
View File
@@ -220,6 +220,34 @@ EntityMatchResult UrlMatch::match_entity(EntityBase *entity) const {
}
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
// BEGIN ESPAsyncWebServer 3.12.1 workaround: remove once a release containing upstream commit
// 880c97068abb0f0e63f803cd3c0cac84fa5cd7bb is out and we bump to it.
// 3.12.1 creates the SSE client before the head is ACKed and credits the head's ACK to its first queued messages,
// dropping or truncating them. Create the client once the head is ACKed instead, as 3.9.6 did.
class HeadAckedEventSourceResponse final : public AsyncEventSourceResponse {
public:
HeadAckedEventSourceResponse(DeferredUpdateEventSourceList *list, WebServer *ws)
: AsyncEventSourceResponse(nullptr), list_(list), ws_(ws) {}
void _respond(AsyncWebServerRequest *request) override {
String head;
this->_assembleHead(head, request->version());
request->client()->write(head.c_str(), this->_headLength);
}
// Every ACK until clientRelease() is the head's, and clientRelease() unhooks this. _state must stay
// RESPONSE_SETUP: past RESPONSE_WAIT_ACK the request would close its released (null) client after this returns.
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time) override {
this->_ackedLength += len;
if (this->_ackedLength >= this->_headLength)
this->list_->add_client_(this->ws_, request->clientRelease());
return 0;
}
private:
DeferredUpdateEventSourceList *list_;
WebServer *ws_;
};
// END ESPAsyncWebServer 3.12.1 workaround
// helper for allowing only unique entries in the queue
void __attribute__((flatten)) DeferredUpdateEventSource::deq_push_back_with_dedup_(
void *source, message_generator_t *message_generator) {
@@ -328,7 +356,13 @@ void DeferredUpdateEventSourceList::try_send_nodefer(const char *message, size_t
}
}
// BEGIN ESPAsyncWebServer 3.12.1 workaround (see HeadAckedEventSourceResponse): once removed, add_new_client creates
// the source as add_client_ does and ends with es->handleRequest(request); add_client_ goes away.
void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) {
request->send(new HeadAckedEventSourceResponse(this, ws)); // NOLINT
}
void DeferredUpdateEventSourceList::add_client_(WebServer *ws, AsyncClient *client) {
DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, ESPHOME_F("/events"));
this->push_back(es);
@@ -336,9 +370,10 @@ void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServer
es->onDisconnect([this, es](AsyncEventSourceClient *client) { this->on_client_disconnect_(es); });
es->handleRequest(request);
new AsyncEventSourceClient(client, es); // NOLINT: owned by es
ws->enable_loop_soon_any_context();
}
// END ESPAsyncWebServer 3.12.1 workaround
void DeferredUpdateEventSourceList::on_client_connect_(DeferredUpdateEventSource *source) {
WebServer *ws = source->web_server_;
@@ -171,9 +171,12 @@ class DeferredUpdateEventSource final : public AsyncEventSource {
};
class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEventSource *> {
friend class HeadAckedEventSourceResponse; // ESPAsyncWebServer 3.12.1 workaround, remove with it
protected:
void on_client_connect_(DeferredUpdateEventSource *source);
void on_client_disconnect_(DeferredUpdateEventSource *source);
void add_client_(WebServer *ws, AsyncClient *client); // ESPAsyncWebServer 3.12.1 workaround, remove with it
public:
/// Returns true if there are event sources remaining (including pending cleanup).
@@ -132,7 +132,7 @@ async def to_code(config: ConfigType) -> None:
)
cg.add_platformio_option("extra_scripts", ["pre:fix_rp2040_hash.py"])
# https://github.com/ESP32Async/ESPAsyncWebServer/blob/main/library.json
cg.add_library("ESP32Async/ESPAsyncWebServer", "3.9.6")
cg.add_library("ESP32Async/ESPAsyncWebServer", "3.12.1")
# The DNS server used for captive portals on ESP32; other platforms use the Arduino
@@ -1,4 +1,9 @@
#include "web_server_base.h"
#include <cstring>
#include "esphome/core/helpers.h"
#if defined(USE_NETWORK) && !defined(USE_ZEPHYR) && !defined(USE_HOST)
namespace esphome::web_server_base {
@@ -47,18 +52,23 @@ bool GuardMiddlewareHandler::check_auth_(AsyncWebServerRequest *request) {
// compiled out. On ESP32 our own server picks the scheme internally.
#if USE_ESP32 || defined(USE_WEBSERVER_AUTH_DIGEST)
bool success = request->authenticate(credentials.username, credentials.password);
#else
bool success = request->authenticate(credentials.basic_auth_hash);
#endif
if (!success) {
#if USE_ESP32
if (!success)
request->requestAuthentication();
#elif defined(USE_WEBSERVER_AUTH_DIGEST)
request->requestAuthentication(nullptr, true);
#else
request->requestAuthentication(nullptr, false);
#endif
// Checked here, not with the library's authenticate()/requestAuthentication(): those also link its
// Digest code, since the scheme is only known at run time there
const String &challenge = request->authChallenge();
const size_t hash_len = strlen(credentials.basic_auth_hash);
const bool success = request->authType() == AsyncAuthType::AUTH_BASIC && challenge.length() == hash_len &&
constant_time_equals(reinterpret_cast<const uint8_t *>(challenge.c_str()),
reinterpret_cast<const uint8_t *>(credentials.basic_auth_hash), hash_len);
if (!success) {
// The challenge the library sent, with the strings kept in flash on ESP8266
AsyncWebServerResponse *response = request->beginResponse(401);
response->addHeader(String(F("WWW-Authenticate")), String(F("Basic realm=\"Login Required\"")));
request->send(response);
}
#endif
return success;
#else
return true;
+1 -1
View File
@@ -564,7 +564,7 @@ class WiFiComponent final : public Component {
// (In most use cases you won't need these)
/// Setup WiFi interface.
void setup() override;
#if defined(USE_LN882X) || defined(USE_BK72XX)
#if defined(USE_ESP32) || defined(USE_LN882X) || defined(USE_BK72XX)
void on_powerdown() override;
#endif
void start();
@@ -1635,6 +1635,21 @@ bool WiFiComponent::wifi_dpp_start_listen_() {
void WiFiComponent::wifi_dpp_stop_listen_() { esp_supp_dpp_stop_listen(); }
#endif
void WiFiComponent::on_powerdown() {
// Leave the AP before esp_restart() or deep sleep stops Wi-Fi. esp_wifi_stop() while associated
// makes the ESP32 retransmit its Disassociation up to the retry limit although the AP ACKs it
// (32 copies with protected management frames), which some APs handle badly.
// Upstream bug: https://github.com/espressif/esp-idf/issues/19443 - remove this once it is fixed
// in the ESP-IDF versions we support.
// Ask the driver: s_sta_connected lags until the main loop has handled the connect event.
wifi_ap_record_t ap_info;
if (esp_wifi_sta_get_ap_info(&ap_info) != ESP_OK)
return;
ESP_LOGD(TAG, "Disconnecting before powerdown");
esp_wifi_disconnect();
delay(100); // NOLINT: once per reboot or deep sleep, lets the disassociation go out
}
bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
bssid_t WiFiComponent::wifi_bssid() {
+15 -8
View File
@@ -10,18 +10,14 @@ namespace zephyr {
ESPHOME_LOG_TAG(TAG, "zephyr");
static gpio_flags_t flags_to_mode(gpio::Flags flags, bool inverted, bool value) {
// Without a GPIO_OUTPUT_INIT_* bit, Zephyr leaves the output level as it is.
static gpio_flags_t flags_to_mode(gpio::Flags flags) {
gpio_flags_t ret = 0;
if (flags & gpio::FLAG_INPUT) {
ret |= GPIO_INPUT;
}
if (flags & gpio::FLAG_OUTPUT) {
ret |= GPIO_OUTPUT;
if (value != inverted) {
ret |= GPIO_OUTPUT_INIT_HIGH;
} else {
ret |= GPIO_OUTPUT_INIT_LOW;
}
}
if (flags & gpio::FLAG_PULLUP) {
ret |= GPIO_PULL_UP;
@@ -123,8 +119,11 @@ void ZephyrGPIOPin::pin_mode(gpio::Flags flags) {
if (nullptr == this->gpio_) {
return;
}
auto ret = gpio_pin_configure(this->gpio_, this->pin_ % this->gpio_size_,
flags_to_mode(flags, this->inverted_, this->value_));
gpio_flags_t mode = flags_to_mode(flags);
if (flags & gpio::FLAG_OUTPUT) {
mode |= this->value_ != this->inverted_ ? GPIO_OUTPUT_INIT_HIGH : GPIO_OUTPUT_INIT_LOW;
}
auto ret = gpio_pin_configure(this->gpio_, this->pin_ % this->gpio_size_, mode);
if (ret != 0) {
ESP_LOGE(TAG, "gpio %u cannot be configured %d.", this->pin_, ret);
}
@@ -182,6 +181,14 @@ void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
gpio_pin_set(arg->gpio, arg->pin % arg->gpio_size, value != arg->inverted ? 1 : 0);
}
void IRAM_ATTR ISRInternalGPIOPin::pin_mode(gpio::Flags flags) {
auto *arg = (zephyr::ISRPinArg *) this->arg_;
if (arg == nullptr || arg->gpio == nullptr) {
return;
}
gpio_pin_configure(arg->gpio, arg->pin % arg->gpio_size, zephyr::flags_to_mode(flags));
}
} // namespace esphome
#endif
+1 -1
View File
@@ -4,7 +4,7 @@ from enum import Enum
from esphome.enum import StrEnum
__version__ = "2026.11.0b4"
__version__ = "2026.11.0b5"
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
VALID_SUBSTITUTIONS_CHARACTERS = (
+2
View File
@@ -294,6 +294,8 @@ void Application::reboot() {
for (auto &component : std::ranges::reverse_view(this->components_)) {
component->on_shutdown();
}
// No teardown on a forced reboot, but still power hardware down (Wi-Fi leaves the AP)
this->run_powerdown_hooks();
arch_restart();
}
void Application::safe_reboot() {
+4 -2
View File
@@ -185,10 +185,12 @@ class Component {
*/
virtual bool teardown() { return true; }
/** Called after teardown is complete to power down hardware.
/** Called last before a reboot or deep sleep to power down hardware.
*
* This is called after all components have finished their teardown process,
* On a safe shutdown this is called after all components have finished their teardown process,
* making it safe to power down hardware like ethernet PHY.
* On a forced reboot (Application::reboot()) it is called right after on_shutdown() with no
* teardown, so implementations must not assume teardown() has run.
*/
virtual void on_powerdown() {}
+32 -14
View File
@@ -9,11 +9,29 @@
#include <algorithm>
#include <cinttypes>
#include <cstring>
#if defined(USE_RP2) && (defined(USE_WIFI) || defined(USE_ETHERNET))
#include <hardware/sync.h>
#endif
namespace esphome {
ESPHOME_LOG_TAG(TAG, "scheduler");
#if defined(USE_RP2) && (defined(USE_WIFI) || defined(USE_ETHERNET))
// lwIP callbacks add and cancel items from a low-priority IRQ on RP2, where lock_ is an empty Mutex: hold interrupts
// off instead. Not the lwIP lock: each release re-arms arduino-pico's 20 ms Ethernet poll, starving reception.
class SchedulerLockGuard {
public:
explicit SchedulerLockGuard(Mutex & /*mutex*/) : state_(save_and_disable_interrupts()) {}
~SchedulerLockGuard() { restore_interrupts(this->state_); }
private:
uint32_t state_;
};
#else
using SchedulerLockGuard = LockGuard;
#endif
// Maximum number of logically deleted (cancelled) items before forcing cleanup.
// Empirically chosen to balance cleanup overhead against tombstone accumulation in items_.
static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
@@ -118,7 +136,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
if (delay == SCHEDULER_DONT_RUN) {
// Still need to cancel existing timer if we have a name/id
if (!skip_cancel) {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, /* find_first= */ true);
}
return;
@@ -140,7 +158,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
uint32_t offset = 0;
// Take lock early to protect scheduler_item_pool_head_ access
{
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
// Create and populate the scheduler item
SchedulerItem *item = this->get_item_from_pool_locked_();
@@ -317,7 +335,7 @@ void Scheduler::full_cleanup_removed_items_() {
// 2. Other threads must see either the old state or the new state, not intermediate states
// 3. The operation is already expensive (O(n)), so lock overhead is negligible
// 4. No operations inside can block or take other locks, so no deadlock risk
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
// Compact in-place: move valid items forward, recycle removed ones
size_t write = 0;
@@ -440,7 +458,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
while (!this->items_.empty()) {
SchedulerItem *item;
{
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
item = this->pop_raw_locked_();
}
@@ -457,7 +475,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
ESP_LOGD(TAG, "\n");
{
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
this->items_ = std::move(old_items);
// Rebuild heap after moving items back
std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
@@ -494,7 +512,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
// Multi-threaded platforms without atomics: must take lock to safely read remove flag
{
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
if (is_item_removed_locked_(item)) {
this->recycle_item_main_loop_(this->pop_raw_locked_());
this->to_remove_decrement_locked_();
@@ -504,7 +522,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
#else
// Single-threaded or multi-threaded with atomics: can check without lock
if (is_item_removed_(item)) {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
this->recycle_item_main_loop_(this->pop_raw_locked_());
this->to_remove_decrement_locked_();
continue;
@@ -513,7 +531,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
// Don't run on failed components (is_item_failed_ exempts SELF_POINTER delays).
if (this->is_item_failed_(item)) {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
this->recycle_item_main_loop_(this->pop_raw_locked_());
continue;
}
@@ -533,7 +551,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
// - timeouts/intervals get cancelled
now = this->execute_item_(item, now);
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
// Only pop after function call, this ensures we were reachable
// during the function call and know if we were cancelled.
@@ -583,7 +601,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
// every item has been returned to a container (items_, to_add_, or pool).
// Lock needed to get a consistent snapshot of all containers.
{
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
this->debug_verify_no_leak_();
}
#endif
@@ -592,7 +610,7 @@ uint32_t HOT Scheduler::call(uint32_t now) {
return now;
}
void HOT Scheduler::process_to_add_slow_path_() {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
for (auto *&it : this->to_add_) {
if (is_item_removed_locked_(it)) {
// Recycle cancelled items
@@ -616,7 +634,7 @@ bool HOT Scheduler::cleanup_slow_path_() {
// 4. We need a consistent view of items_ and to_remove_ throughout the operation
// Without the lock, we could access items_ while another thread is reading it,
// leading to race conditions
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
while (!this->items_.empty()) {
SchedulerItem *item = this->items_[0];
if (!this->is_item_removed_locked_(item))
@@ -664,7 +682,7 @@ uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
// Common implementation for cancel operations - handles locking
bool HOT Scheduler::cancel_item_(Component *component, NameType name_type, const char *static_name, uint32_t hash_or_id,
SchedulerItem::Type type) {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
// Public cancel path uses default find_first=false to cancel ALL matches because
// DelayAction parallel mode (skip_cancel=true) can create multiple items with the same key.
return this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type);
@@ -773,7 +791,7 @@ void __attribute__((noinline)) Scheduler::shrink_scheduler_vector_(std::vector<S
}
void Scheduler::trim_freelist() {
LockGuard guard{this->lock_};
SchedulerLockGuard guard{this->lock_};
SchedulerItem *item = this->scheduler_item_pool_head_;
size_t freed = 0;
while (item != nullptr) {
+2 -1
View File
@@ -518,7 +518,8 @@ class Scheduler {
uint32_t hash_or_id,
SchedulerItem::Type type, bool find_first);
// Empty on single-threaded platforms (ESP8266, RP2), so it takes no space there
// Empty on single-threaded platforms (ESP8266, RP2), so it takes no space there; RP2 with networking locks by
// holding interrupts off (SchedulerLockGuard in scheduler.cpp)
[[no_unique_address]] Mutex lock_;
std::vector<SchedulerItem *> items_;
std::vector<SchedulerItem *> to_add_;
+5 -5
View File
@@ -125,7 +125,7 @@ lib_deps =
ESP8266WiFi ; wifi (Arduino built-in)
Update ; ota (Arduino built-in)
ESP32Async/ESPAsyncTCP@2.0.0 ; async_tcp
ESP32Async/ESPAsyncWebServer@3.9.6 ; web_server_base
ESP32Async/ESPAsyncWebServer@3.12.1 ; web_server_base
makuna/NeoPixelBus@2.7.3 ; neopixelbus
ESP8266HTTPClient ; http_request (Arduino built-in)
ESP8266mDNS ; mdns (Arduino built-in)
@@ -216,8 +216,8 @@ framework = arduino
lib_deps =
${common:arduino.lib_deps}
${common:idf-component-libs.lib_deps}
ayushsharma82/RPAsyncTCP@1.3.2 ; async_tcp
ESP32Async/ESPAsyncWebServer@3.9.6 ; web_server_base
https://github.com/esphome-libs/RPAsyncTCP.git#1.3.2-esphome.1 ; async_tcp
ESP32Async/ESPAsyncWebServer@3.12.1 ; web_server_base
WiFi ; wifi (arduino-pico built-in)
lwIP_CYW43 ; wifi (arduino-pico built-in, WiFi dependency)
HTTPClient ; http_request (arduino-pico built-in)
@@ -245,10 +245,10 @@ lib_compat_mode = soft
lib_deps =
${common.lib_deps_base}
${common:idf-component-libs.lib_deps}
ESP32Async/ESPAsyncWebServer@3.9.6 ; web_server_base
ESP32Async/ESPAsyncWebServer@3.12.1 ; web_server_base
esphome/wireguard@0.4.8 ; wireguard
esphome/noise-c@0.1.31 ; noise (api, ota)
ESP32Async/AsyncTCP@3.4.5 ; async_tcp
ESP32Async/AsyncTCP@3.5.0 ; async_tcp
DNSServer ; captive_portal
heman/AsyncMqttClient-esphome@2.0.0 ; mqtt
improv/Improv@1.2.7 ; improv_serial
+205 -2
View File
@@ -1,6 +1,7 @@
"""Tests for the api wizard: schema, final validation and generated tables."""
from collections.abc import Callable
import importlib
import json
from pathlib import Path
import random
@@ -8,13 +9,20 @@ import re
import string
import textwrap
from aioesphomeapi.model import COMPONENT_TYPE_TO_INFO
import pytest
from esphome.components.api import wizard
from esphome.components.homeassistant.switch import SUPPORTED_DOMAINS as SWITCH_DOMAINS
from esphome.config import load_config
from esphome.core import CORE, ID
from esphome.helpers import fnv1_hash, fnv1a_32bit_hash, zstd_module
from esphome.cpp_generator import MockObjClass
from esphome.helpers import (
fnv1_hash,
fnv1_hash_object_id,
fnv1a_32bit_hash,
zstd_module,
)
from tests.component_tests.helpers import get_define_value
ESP32_HEADER = """
@@ -774,9 +782,14 @@ def test_the_blob_is_the_exact_json_document(
"description": "Pick",
"entities": [
# No device_id for the main device, and no description when unset
{"key": entity_hash(main_cpp, "sw"), "description": "Enable"},
{
"key": entity_hash(main_cpp, "sw"),
"platform": "switch",
"description": "Enable",
},
{
"key": entity_hash(main_cpp, "sw2"),
"platform": "switch",
"device_id": fnv1a_32bit_hash("kitchen_dev"),
},
],
@@ -1074,16 +1087,206 @@ def test_an_entity_role_is_sent_with_the_entity(
assert wizard_entities(main_cpp) == [
{
"key": entity_hash(main_cpp, "sw"),
"platform": "switch",
"description": "Enable",
"role": "example.enable",
},
{
"key": entity_hash(main_cpp, "sw2"),
"platform": "switch",
"device_id": fnv1a_32bit_hash("kitchen_dev"),
},
]
def test_entities_sharing_a_name_are_told_apart_by_platform(
tmp_path: Path, generate_main: Callable[[str | Path], str]
) -> None:
entities = """
sensor:
- platform: template
id: power_sensor
name: Power
switch:
- platform: template
id: power_switch
name: Power
optimistic: true
"""
pages = """
- entities:
- id: power_sensor
- id: power_switch
"""
main_cpp = generate_main(
write_config(tmp_path, ESP32_HEADER, wizard_api(pages), entities)
)
# The key is the same, so the platform is what says which entity is meant
key = fnv1_hash_object_id("Power")
assert wizard_entities(main_cpp) == [
{"key": key, "platform": "sensor"},
{"key": key, "platform": "switch"},
]
def test_a_datetime_entity_has_the_platform_of_its_type(
tmp_path: Path, generate_main: Callable[[str | Path], str]
) -> None:
entities = """
datetime:
- platform: template
id: dt_date
name: Day
type: date
optimistic: true
- platform: template
id: dt_time
name: Hour
type: time
optimistic: true
- platform: template
id: dt_datetime
name: Moment
type: datetime
optimistic: true
"""
pages = """
- entities:
- id: dt_date
- id: dt_time
- id: dt_datetime
"""
main_cpp = generate_main(
write_config(tmp_path, ESP32_HEADER, wizard_api(pages), entities)
)
assert wizard_entities(main_cpp) == [
{"key": fnv1_hash_object_id("Day"), "platform": "date"},
{"key": fnv1_hash_object_id("Hour"), "platform": "time"},
{"key": fnv1_hash_object_id("Moment"), "platform": "datetime"},
]
def test_an_entity_declared_in_its_component_has_the_platform_of_its_class(
tmp_path: Path, generate_main: Callable[[str | Path], str]
) -> None:
entities = """
uart:
tx_pin: 17
rx_pin: 16
baud_rate: 9600
gps:
latitude:
id: gps_lat
name: Latitude
"""
pages = """
- entities:
- id: gps_lat
"""
main_cpp = generate_main(
write_config(tmp_path, ESP32_HEADER, wizard_api(pages), entities)
)
assert wizard_entities(main_cpp) == [
{"key": fnv1_hash_object_id("Latitude"), "platform": "sensor"}
]
def test_an_entity_of_a_platform_the_wizard_does_not_know_is_rejected(
tmp_path: Path,
) -> None:
entities = """
psram:
i2c:
id: camera_bus
sda: 25
scl: 23
esp32_camera:
id: test_camera
name: Camera
data_pins: [17, 35, 34, 5, 39, 18, 36, 19]
vsync_pin: 22
href_pin: 26
pixel_clock_pin: 21
external_clock:
pin: 27
i2c_id: camera_bus
"""
pages = """
- entities:
- id: test_camera
"""
errors = config_errors(
write_config(tmp_path, ESP32_HEADER, wizard_api(pages), entities)
)
assert any(
"Entity 'test_camera' is not of a platform the wizard can show" in error
for error in errors
), errors
@pytest.mark.parametrize(
("class_name", "platform"), wizard.WIZARD_ENTITY_PLATFORMS.items()
)
def test_every_wizard_entity_platform_names_a_real_entity_class(
class_name: str, platform: str
) -> None:
namespace, _ = class_name.split("::")
component = importlib.import_module(
f"esphome.components.{namespace.removesuffix('_')}"
)
# Light keeps its classes in a submodule
modules = [component, *(getattr(component, "types", None),)]
classes = {
str(value)
for module in modules
if module is not None
for value in vars(module).values()
if isinstance(value, MockObjClass)
}
assert class_name in classes
# The name the API client knows the entity type by
assert platform in COMPONENT_TYPE_TO_INFO
# Entity platforms of the API that a wizard page cannot show
WIZARD_UNSUPPORTED_PLATFORMS: set[str] = {
# ESP32Camera derives from EntityBase directly, with no camera base class to recognise
"camera",
# MediaPlayer does not declare EntityBase, so the page's use_id(EntityBase) already rejects it
"media_player",
# No ESPHome component provides one
"siren",
}
def test_every_api_entity_platform_is_known_to_the_wizard() -> None:
"""A new entity platform needs a WIZARD_ENTITY_PLATFORMS entry, or a reason to be left out."""
proto = (
Path(wizard.__file__).parent.joinpath("api.proto").read_text(encoding="utf-8")
)
messages = re.findall(r"message ListEntities(?!Done|Services)\w+Response \{", proto)
# The ifdef of each ListEntities message names its platform: USE_SENSOR, USE_DATETIME_DATE, ...
ifdefs = re.findall(
r"message ListEntities(?!Done|Services)\w+Response \{[^}]*?"
r'option \(ifdef\) = "USE_(\w+)";',
proto,
)
assert len(ifdefs) == len(messages)
proto_platforms = {ifdef.lower().removeprefix("datetime_") for ifdef in ifdefs}
assert (
set(wizard.WIZARD_ENTITY_PLATFORMS.values()) | WIZARD_UNSUPPORTED_PLATFORMS
== proto_platforms
)
def test_an_entity_with_a_role_may_be_in_the_wizard_twice(tmp_path: Path) -> None:
wizard.register_entity_role(ID("sw", is_declaration=True), "example.enable")
Whitespace-only changes.
@@ -0,0 +1,20 @@
"""LibreTiny always gets the xPortGetCoreID stand-in, whatever its log level."""
from collections.abc import Callable
from pathlib import Path
from esphome.core import CORE
def test_libretiny_adds_core_id_flag(
generate_main: Callable[[str | Path], str], tmp_path: Path
) -> None:
config = tmp_path / "test.yaml"
config.write_text(
"esphome:\n name: test\n\n"
"bk72xx:\n board: generic-bk7231n-qfn32-tuya\n\n"
"wifi:\n ssid: test\n password: testtest12\n\n"
"async_tcp:\n"
)
generate_main(config)
assert "-DxPortGetCoreID=int" in CORE.build_flags
+12 -2
View File
@@ -818,9 +818,18 @@ def test_uc8279_offset_height_configurable(
assert resolution == (0x61, 0x00, 0x10, 0x00, 0x0A)
@pytest.mark.parametrize(
"dimensions",
[
{"width": 16, "height": 2, "offset_height": 0xFFFF},
# offset_height not written: the default of 0 applies, and the error must not point at a missing key
{"width": 16, "height": 0x10000},
],
)
def test_uc8279_offset_height_plus_height_must_fit_16_bits(
set_core_config: SetCoreConfigCallable,
set_component_config: Callable[[str, Any], None],
dimensions: dict[str, int],
) -> None:
"""offset_height plus the panel height is the gate count, which the controller holds in 16 bits."""
set_core_config(
@@ -829,15 +838,16 @@ def test_uc8279_offset_height_plus_height_must_fit_16_bits(
)
set_component_config("spi", {"id": "spi_bus", "clk_pin": 18, "mosi_pin": 19})
with pytest.raises(cv.Invalid, match="must not exceed"):
with pytest.raises(cv.Invalid, match="must not exceed") as exc_info:
run_schema_validation(
{
"id": "test_display",
"model": "uc8279",
"dc_pin": 21,
"dimensions": {"width": 16, "height": 2, "offset_height": 0xFFFF},
"dimensions": dimensions,
}
)
assert exc_info.value.path[-2:] == ["dimensions", "height"]
def test_uc8279_x4pro_explicit_dimensions_keep_the_model_offset(
@@ -0,0 +1,22 @@
"""Every LibreTiny family frees closed TCP connections after about 1 s (TCP_MSL=250)."""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome.core import CORE
@pytest.mark.parametrize(
"config_file", ["bk72xx.yaml", "rtl8710b.yaml", "rtl8720c.yaml", "ln882x.yaml"]
)
def test_lwip_tcp_msl_is_250(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
config_file: str,
) -> None:
generate_main(component_config_path(config_file))
assert "TCP_MSL=250" in CORE.platformio_options["custom_options.lwip"]
@@ -0,0 +1,36 @@
"""Tests for the usb_host sdkconfig defaults."""
from collections.abc import Callable
from esphome.components.esp32 import RawSdkconfigValue
from esphome.components.esp32.const import KEY_ESP32, KEY_SDKCONFIG_OPTIONS
from esphome.core import CORE
def test_usb_host_uses_default_control_transfer_size(
generate_main: Callable[[str], str],
) -> None:
"""usb_host retains its existing 1024-byte default."""
generate_main("tests/component_tests/usb_host/usb_host.yaml")
assert (
CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS][
"CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE"
]
== 1024
)
def test_usb_host_respects_explicit_control_transfer_size(
generate_main: Callable[[str], str],
) -> None:
"""An explicit sdkconfig option takes precedence over the component default."""
generate_main(
"tests/component_tests/usb_host/usb_host_control_transfer_override.yaml"
)
value = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS][
"CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE"
]
assert isinstance(value, RawSdkconfigValue)
assert value.value == "4096"
@@ -0,0 +1,9 @@
esphome:
name: test
esp32:
variant: esp32s3
framework:
type: esp-idf
usb_host:
@@ -0,0 +1,11 @@
esphome:
name: test
esp32:
variant: esp32s3
framework:
type: esp-idf
sdkconfig_options:
CONFIG_USB_HOST_CONTROL_TRANSFER_MAX_SIZE: "4096"
usb_host:
@@ -4,6 +4,9 @@
#include <gtest/gtest.h>
#include <array>
#include <cstring>
namespace esphome::bluetooth_connection::testing {
using ble_device_base::ESPBTUUID;
@@ -46,4 +49,59 @@ TEST(GattUuidPacking, EstimateGrowsWithCharacteristicsAndMode) {
EXPECT_LT(estimate_service_size(1, false), estimate_service_size(2, false));
}
// BTstack hands every GATT UUID over as 128 bits, big-endian; uuid_from_be128 picks the short form
static constexpr uint8_t BASE_TAIL[12] = {0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
static ESPBTUUID on_base(uint32_t value) {
uint8_t be[16] = {static_cast<uint8_t>(value >> 24), static_cast<uint8_t>(value >> 16),
static_cast<uint8_t>(value >> 8), static_cast<uint8_t>(value)};
memcpy(be + 4, BASE_TAIL, sizeof(BASE_TAIL));
return uuid_from_be128(be);
}
TEST(GattUuidFromBe128, SixteenBitOnBase) {
const ESPBTUUID uuid = on_base(0x180F);
ASSERT_EQ(uuid.type(), ESPBTUUID::Type::UUID16);
EXPECT_EQ(uuid.uuid16(), 0x180F);
}
TEST(GattUuidFromBe128, ThirtyTwoBitOnBaseKeepsTopHalf) {
// 5EC10001-0000-1000-8000-00805F9B34FB: BTstack's own uuid16 is 0x0001 for this one
const ESPBTUUID uuid = on_base(0x5EC10001);
ASSERT_EQ(uuid.type(), ESPBTUUID::Type::UUID32);
EXPECT_EQ(uuid.uuid32(), 0x5EC10001u);
std::array<uint64_t, 2> uuid128{};
uint32_t short_uuid = 0;
fill_gatt_uuid(uuid128, short_uuid, uuid, true);
EXPECT_EQ(short_uuid, 0x5EC10001u);
short_uuid = 0;
fill_gatt_uuid(uuid128, short_uuid, uuid, false);
EXPECT_EQ(short_uuid, 0u);
EXPECT_EQ(uuid128[0], 0x5EC1000100001000ULL);
EXPECT_EQ(uuid128[1], 0x800000805F9B34FBULL);
}
TEST(GattUuidFromBe128, ShortFormBoundary) {
EXPECT_EQ(on_base(0xFFFF).type(), ESPBTUUID::Type::UUID16);
const ESPBTUUID uuid = on_base(0x10000);
ASSERT_EQ(uuid.type(), ESPBTUUID::Type::UUID32);
EXPECT_EQ(uuid.uuid32(), 0x10000u);
}
TEST(GattUuidFromBe128, OffBaseStaysFullWidth) {
const uint8_t big_endian[16] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
const ESPBTUUID uuid = uuid_from_be128(big_endian);
ASSERT_EQ(uuid.type(), ESPBTUUID::Type::UUID128);
EXPECT_EQ(uuid, ESPBTUUID::from_raw_reversed(big_endian));
// One byte off the base tail is a different 128-bit UUID, not a short one
uint8_t near_base[16] = {0x00, 0x00, 0x18, 0x0F};
memcpy(near_base + 4, BASE_TAIL, sizeof(BASE_TAIL));
near_base[15] ^= 1;
EXPECT_EQ(uuid_from_be128(near_base).type(), ESPBTUUID::Type::UUID128);
}
} // namespace esphome::bluetooth_connection::testing
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# dht is a sensor platform; its header needs the sensor component
manifest.dependencies = manifest.dependencies + ["sensor"]
+30
View File
@@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include "esphome/components/dht/dht.h"
namespace esphome::dht::testing {
TEST(DHTDecodeTemperature, Positive) {
EXPECT_EQ(decode_dht22_temperature(0x0000), 0);
EXPECT_EQ(decode_dht22_temperature(0x00E7), 231); // 23.1 C
EXPECT_EQ(decode_dht22_temperature(0x01F4), 500); // 50.0 C
}
TEST(DHTDecodeTemperature, SignAndMagnitude) {
EXPECT_EQ(decode_dht22_temperature(0x8034), -52); // -5.2 C, read as -5.3 C before
EXPECT_EQ(decode_dht22_temperature(0x8001), -1); // -0.1 C
EXPECT_EQ(decode_dht22_temperature(0x8000), 0); // -0.0 C
EXPECT_EQ(decode_dht22_temperature(0x8190), -400); // -40.0 C
}
TEST(DHTDecodeTemperature, SixteenBitTwosComplement) {
EXPECT_EQ(decode_dht22_temperature(0xFFCC), -52); // -5.2 C
EXPECT_EQ(decode_dht22_temperature(0xFE70), -400); // -40.0 C
}
TEST(DHTDecodeTemperature, TwelveBitTwosComplement) {
EXPECT_EQ(decode_dht22_temperature(0x0FCC), -52); // -5.2 C
EXPECT_EQ(decode_dht22_temperature(0x0E70), -400); // -40.0 C
}
} // namespace esphome::dht::testing
@@ -0,0 +1,2 @@
packages:
dht: !include common.yaml
@@ -0,0 +1,70 @@
#include <gtest/gtest.h>
#include <memory>
#include "esphome/components/epaper_spi/epaper_spi_jd79660.h"
#include "esphome/components/epaper_spi/epaper_spi_t133a01.h"
#include "esphome/components/epaper_spi/epaper_waveshare_bwr.h"
#include "esphome/components/epaper_spi/epaper_weact_3c.h"
namespace esphome::epaper_spi::testing {
namespace {
/// Reaches the protected EPaperBase members of any driver, including final ones, through member pointers.
class EPaperBaseAccess : public EPaperBase {
public:
/// Leaves the display as a completed refresh leaves it: nothing drawn and no full update pending.
static void set_refreshed(EPaperBase &display) {
EXPECT_TRUE((display.*&EPaperBaseAccess::init_buffer_)(display.*&EPaperBaseAccess::buffer_length_));
(display.*&EPaperBaseAccess::reset_bounds_)();
display.*&EPaperBaseAccess::full_update_requested_ = false;
}
/// The request count is shared by the whole test binary, so a display left mid-update must give its request back.
static void stop_fast_loop(EPaperBase &display) { (display.*&EPaperBaseAccess::high_frequency_loop_).stop(); }
};
template<typename Driver, typename... Args> std::unique_ptr<Driver> make_refreshed(Args... args) {
auto display = std::make_unique<Driver>("test", 16, 2, nullptr, 0, args...);
EPaperBaseAccess::set_refreshed(*display);
return display;
}
template<typename Driver> std::unique_ptr<Driver> make_display() { return make_refreshed<Driver>(); }
template<> std::unique_ptr<EPaperJD79660> make_display() { return make_refreshed<EPaperJD79660>(nullptr, 0); }
/// Runs the UPDATE state; true if the update goes on to send the frame.
bool update_is_sent(EPaperBase &display) {
display.update();
display.loop();
return display.is_updating();
}
/// fill() writes the whole buffer without drawing pixels, so it must mark the frame drawn itself. Otherwise the
/// UPDATE state finds nothing drawn after a refresh, and returns to idle without sending the frame.
template<typename Driver> class EPaperFillMarksDrawn : public ::testing::Test {
protected:
void TearDown() override { EPaperBaseAccess::stop_fast_loop(*this->display_); }
std::unique_ptr<Driver> display_ = make_display<Driver>();
};
using Drivers = ::testing::Types<EPaperWaveshareBWR, EPaperT133A01, EPaperJD79660, EPaperWeAct3C>;
TYPED_TEST_SUITE(EPaperFillMarksDrawn, Drivers);
TYPED_TEST(EPaperFillMarksDrawn, FillOnlyUpdateIsSent) {
this->display_->set_auto_clear(false);
this->display_->fill(Color::WHITE);
EXPECT_TRUE(update_is_sent(*this->display_)) << "fill-only update was skipped";
}
/// Auto clear fills the frame before the lambda runs, so an update whose lambda draws nothing still erases the
/// previous image.
TYPED_TEST(EPaperFillMarksDrawn, AutoClearUpdateIsSentWhenTheLambdaDrawsNothing) {
this->display_->set_writer([](display::Display &) {});
EXPECT_TRUE(update_is_sent(*this->display_)) << "auto clear did not erase the previous image";
}
} // namespace
} // namespace esphome::epaper_spi::testing
@@ -2,3 +2,7 @@ packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
<<: !include common.yaml
# IPv6 with an IP sensor: compiles the IPv6 event handler's listener notification
network:
enable_ipv6: true
+10
View File
@@ -1,8 +1,10 @@
from pathlib import Path
import esphome.codegen as cg
from esphome.components.logger import CONF_BOOT_LOG_BUFFER_SIZE, request_log_listener
from esphome.const import CONF_ESPHOME, CONF_INCLUDES
from esphome.core import CORE
from esphome.loader import FileResource
from esphome.types import ConfigType
from tests.testing_helpers import ComponentManifestOverride
@@ -13,6 +15,10 @@ def override_manifest(manifest: ComponentManifestOverride) -> None:
# logger must run its to_code during C++ test builds because it configures
# the logging subsystem used by ESP_LOG* macros throughout component code.
manifest.enable_codegen()
# Compiled into the LibreTiny task log buffer test; empty on its own without USE_LIBRETINY
manifest.add_resources(
FileResource("esphome.components.logger", "task_log_buffer_libretiny.cpp")
)
original_to_code = manifest.to_code
@@ -22,6 +28,10 @@ def override_manifest(manifest: ComponentManifestOverride) -> None:
if any(Path(p) == TESTS_DIR for p in CORE.config[CONF_ESPHOME][CONF_INCLUDES]):
config = {**config, CONF_BOOT_LOG_BUFFER_SIZE: 512}
request_log_listener()
# FreeRTOS doubles for the LibreTiny task log buffer test
cg.add_build_flag(
f"-I{CORE.relative_src_path('logger', '__libretiny_sdk')}"
)
await original_to_code(config)
manifest.to_code = to_code
@@ -0,0 +1,2 @@
#pragma once
// Host double for the LibreTiny FreeRTOS headers used by task_log_buffer_libretiny.cpp
@@ -0,0 +1,10 @@
#pragma once
// Single-threaded mutex double: every take succeeds
using SemaphoreHandle_t = int *;
inline SemaphoreHandle_t xSemaphoreCreateMutex() {
static int mutex;
return &mutex;
}
inline void vSemaphoreDelete(SemaphoreHandle_t /*mutex*/) {}
inline int xSemaphoreTake(SemaphoreHandle_t /*mutex*/, int /*ticks*/) { return 1; }
inline int xSemaphoreGive(SemaphoreHandle_t /*mutex*/) { return 1; }
@@ -0,0 +1 @@
#pragma once
@@ -0,0 +1,6 @@
// The same tests with a buffer size that needs no rounding
#define TASK_LOG_TEST_NS libretiny_task_log_768
#define TASK_LOG_TEST_SUITE LibreTinyTaskLogBuffer768
#define TASK_LOG_TEST_SIZE static_cast<size_t>(768)
// NOLINTNEXTLINE(bugprone-suspicious-include): reuse the tests with another buffer size.
#include "task_log_buffer_libretiny_test.cpp"
@@ -0,0 +1,154 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <random>
#include <string>
#include <utility>
#include <FreeRTOS.h>
#include <semphr.h>
#include <task.h>
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
// Not a multiple of 4; task_log_buffer_libretiny_768_test.cpp reuses this file with an aligned size
#ifndef TASK_LOG_TEST_NS
#define TASK_LOG_TEST_NS libretiny_task_log_766
#define TASK_LOG_TEST_SUITE LibreTinyTaskLogBuffer766
#define TASK_LOG_TEST_SIZE static_cast<size_t>(766)
#endif
// Compile the LibreTiny buffer against the FreeRTOS doubles in __libretiny_sdk, in a namespace per size
#define USE_LIBRETINY
#ifndef USE_ESPHOME_TASK_LOG_BUFFER
#define USE_ESPHOME_TASK_LOG_BUFFER
#endif
#undef ESPHOME_TASK_LOG_BUFFER_SIZE
#define ESPHOME_TASK_LOG_BUFFER_SIZE TASK_LOG_TEST_SIZE
namespace TASK_LOG_TEST_NS {
// Not global, where speaker_source's FreeRTOS double defines it in the same build
inline constexpr int pdTRUE = 1;
// NOLINTNEXTLINE(bugprone-suspicious-include): exercise production code with the host FreeRTOS double.
#include "esphome/components/logger/task_log_buffer_libretiny.cpp"
} // namespace TASK_LOG_TEST_NS
#undef USE_LIBRETINY
namespace {
using TaskLogBuffer = TASK_LOG_TEST_NS::esphome::logger::TaskLogBuffer;
const char *const TAGS[] = {"a", "bb", "ccc"};
bool send_format(TaskLogBuffer &buffer, const char *tag, const char *format, ...) {
va_list args;
va_start(args, format);
bool sent = buffer.send_message_thread_safe(3, tag, 1, nullptr, format, args);
va_end(args);
return sent;
}
class TASK_LOG_TEST_SUITE : public ::testing::Test {
protected:
// Records the trimmed text expected back when the buffer accepts it
bool send_(const char *tag, const std::string &text) {
if (!send_format(this->buffer_, tag, "%s", text.c_str()))
return false;
std::string trimmed = text;
while (!trimmed.empty() && trimmed.back() == '\n')
trimmed.pop_back();
this->expected_.emplace_back(tag, trimmed);
return true;
}
// Stops one past what was sent, so a broken ring cannot loop forever
void drain_(size_t count = SIZE_MAX) {
TaskLogBuffer::LogMessage *msg;
uint16_t length;
for (count = std::min(count, this->expected_.size() + 1);
count > 0 && this->buffer_.borrow_message_main_loop(msg, length); count--) {
if (reinterpret_cast<uintptr_t>(msg) % alignof(TaskLogBuffer::LogMessage) != 0)
this->misaligned_++;
if (this->expected_.empty() || this->expected_.front().first != msg->tag ||
this->expected_.front().second != std::string(msg->text_data(), length) || msg->text_data()[length] != '\0') {
this->mismatches_++;
} else {
this->expected_.pop_front();
}
this->buffer_.release_message_main_loop();
}
}
TaskLogBuffer buffer_;
std::deque<std::pair<const char *, std::string>> expected_;
int misaligned_{0};
int mismatches_{0};
};
TEST_F(TASK_LOG_TEST_SUITE, HeadersStayAligned) {
for (size_t length = 1; length <= 40; length++) {
ASSERT_TRUE(this->send_(TAGS[0], std::string(length, 'x')));
if (length % 4 == 0)
this->drain_();
}
this->drain_();
EXPECT_EQ(this->misaligned_, 0);
EXPECT_EQ(this->mismatches_, 0);
}
TEST_F(TASK_LOG_TEST_SUITE, TrimsTrailingNewlinesAndKeepsOrder) {
ASSERT_TRUE(this->send_(TAGS[0], "one"));
ASSERT_TRUE(this->send_(TAGS[1], "two\n"));
ASSERT_TRUE(this->send_(TAGS[2], "three\n\n"));
ASSERT_TRUE(this->send_(TAGS[0], "four\nlines"));
this->drain_();
EXPECT_EQ(this->mismatches_, 0);
EXPECT_TRUE(this->expected_.empty());
EXPECT_FALSE(this->buffer_.has_messages());
}
TEST_F(TASK_LOG_TEST_SUITE, EmptyBufferAcceptsEveryLengthAcrossWraps) {
// Every write position comes up, including a message ending less than a header before the end
for (int round = 0; round < 8; round++) {
for (size_t length = 1; length <= 250; length++) {
ASSERT_TRUE(this->send_(TAGS[length % 3], std::string(length, 'a' + length % 26) + (round % 2 ? "\n" : "")))
<< "round " << round << " length " << length;
this->drain_();
}
}
EXPECT_EQ(this->misaligned_, 0);
EXPECT_EQ(this->mismatches_, 0);
}
TEST_F(TASK_LOG_TEST_SUITE, RandomizedTrafficMatchesWhatWasSent) {
std::mt19937 rng(1);
for (int op = 0; op < 200000; op++) {
if (rng() % 3 != 0) {
std::string text(1 + rng() % 120, 'x');
for (auto &c : text)
c = 'a' + rng() % 26;
if (rng() % 2)
text += "\n";
if (rng() % 5 == 0)
text += "\n\n";
this->send_(TAGS[rng() % 3], text);
} else {
this->drain_(rng() % 4);
}
}
this->drain_();
EXPECT_EQ(this->misaligned_, 0);
EXPECT_EQ(this->mismatches_, 0);
EXPECT_TRUE(this->expected_.empty());
EXPECT_TRUE(this->send_(TAGS[0], "still accepted"));
}
} // namespace
#endif // USE_HOST
+12
View File
@@ -2,6 +2,7 @@
#include "esphome/components/logger/logger.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
/*
@@ -28,7 +29,18 @@ class ResetAppListener : public ::testing::EmptyTestEventListener {
// Entities of the same kind share preference keys, so a value an earlier test saved would be restored
if (esphome::global_preferences != nullptr)
esphome::global_preferences->reset();
this->high_frequency_at_start_ = esphome::HighFrequencyLoopRequester::is_high_frequency();
}
// Fail only the test that left the fast loop on, so a leak points at its culprit
void OnTestEnd(const ::testing::TestInfo &info) override {
if (!this->high_frequency_at_start_ && esphome::HighFrequencyLoopRequester::is_high_frequency()) {
ADD_FAILURE() << info.test_suite_name() << "." << info.name()
<< " left a HighFrequencyLoopRequester started; stop it before the test ends";
}
}
bool high_frequency_at_start_{false};
};
void setup() {
@@ -0,0 +1,23 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_NAME
from esphome.types import ConfigType
CODEOWNERS = ["@esphome/tests"]
MULTI_CONF = True
reboot_hook_recorder_ns = cg.esphome_ns.namespace("reboot_hook_recorder")
RebootHookRecorder = reboot_hook_recorder_ns.class_("RebootHookRecorder", cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(RebootHookRecorder),
cv.Required(CONF_NAME): cv.string,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_name(config[CONF_NAME]))
@@ -0,0 +1,10 @@
#include "reboot_hook_recorder.h"
#include "esphome/core/log.h"
namespace esphome::reboot_hook_recorder {
ESPHOME_LOG_TAG(TAG, "reboot_hook_recorder");
void RebootHookRecorder::record_(const char *hook) { ESP_LOGI(TAG, "HOOK %s %s", this->name_, hook); }
} // namespace esphome::reboot_hook_recorder
@@ -0,0 +1,33 @@
#pragma once
#include "esphome/core/component.h"
namespace esphome::reboot_hook_recorder {
/// Logs every shutdown lifecycle hook it receives, so a test can check their order.
class RebootHookRecorder : public Component {
public:
void set_name(const char *name) { this->name_ = name; }
void on_safe_shutdown() override { this->record_("on_safe_shutdown"); }
void on_shutdown() override { this->record_("on_shutdown"); }
// Needs a second call to finish, like a component waiting for a connection to close
bool teardown() override {
if (++this->teardown_calls_ < 2) {
this->record_("teardown_pending");
return false;
}
if (this->teardown_calls_ == 2)
this->record_("teardown_done");
return true;
}
void on_powerdown() override { this->record_("on_powerdown"); }
protected:
void record_(const char *hook);
const char *name_{""};
uint8_t teardown_calls_{0};
};
} // namespace esphome::reboot_hook_recorder
@@ -0,0 +1,19 @@
esphome:
name: reboot-hooks-forced
host:
logger:
level: DEBUG
api:
# Reboot once the test has connected, so that it sees every hook
on_client_connected:
- delay: 100ms
- lambda: App.reboot();
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
reboot_hook_recorder:
- name: first
- name: second
@@ -0,0 +1,19 @@
esphome:
name: reboot-hooks-safe
host:
logger:
level: DEBUG
api:
# Reboot once the test has connected, so that it sees every hook
on_client_connected:
- delay: 100ms
- lambda: App.safe_reboot();
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
reboot_hook_recorder:
- name: first
- name: second
@@ -0,0 +1,59 @@
esphome:
name: serial-proxy-backpressure
host:
api:
logger:
level: DEBUG
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
components: [sndbuf_pin_component, uart_mock]
# Pins the device's socket send buffers for deterministic TCP backpressure
sndbuf_pin_component:
buffer_size: SERVER_SNDBUF
# Dummy uart entry to satisfy serial_proxy's dependency on uart;
# the bus the proxy reads is the uart_mock component below
uart:
baud_rate: 115200
port: /dev/null
uart_mock:
id: mock_uart
baud_rate: 115200
serial_proxy:
- id: proxy
uart_id: mock_uart
name: Mock Port
port_type: TTL
interval:
- interval: 20ms
then:
- lambda: |-
// A serial device that keeps sending, from the moment a client subscribes. It sends filler
// until the client's connection is blocked, then the numbered bytes the test checks
static uint32_t sent = 0;
static bool blocked = false;
auto *client = id(proxy).get_api_connection();
if (client == nullptr || sent >= STREAM_BYTES)
return;
uint8_t chunk[STREAM_CHUNK];
if (!blocked && client->try_to_clear_buffer(false)) {
memset(chunk, STREAM_FILLER, sizeof(chunk));
id(mock_uart).inject_to_rx_buffer(chunk, sizeof(chunk));
return;
}
blocked = true;
for (auto &b : chunk)
b = sent++ % STREAM_MODULUS;
id(mock_uart).inject_to_rx_buffer(chunk, sizeof(chunk));
if (sent >= STREAM_BYTES)
ESP_LOGI("stream", "stream complete");
+25 -6
View File
@@ -1,7 +1,8 @@
"""Minimal plaintext native-api client over a raw socket.
Reads only when told to, so tests control when the TCP pipe backs up toward
the device; message types are counted and each frame is kept for inspection.
the device; message types are counted and each frame is kept for inspection,
and read_frame() hands them out in arrival order.
"""
from __future__ import annotations
@@ -137,6 +138,7 @@ class RawApiClient:
def __init__(self, port: int, recv_buffer_size: int | None = None) -> None:
self._port = port
self._parser = FrameParser()
self._read_pos = 0 # frames before this index were consumed by read_frame()
self.bytes_received = 0
self.frame_counts: Counter[int] = Counter()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -179,21 +181,38 @@ class RawApiClient:
loop = asyncio.get_running_loop()
await loop.sock_sendall(self._sock, encode_frame(msg_type, payload))
async def _recv_once(self) -> None:
data = await asyncio.get_running_loop().sock_recv(self._sock, _READ_CHUNK)
assert data, "server closed the connection unexpectedly"
self.bytes_received += len(data)
self.frame_counts.update(self._parser.feed(data))
async def read_until_frame(
self, msg_type: int, timeout: float = 10.0, count: int = 1
) -> None:
"""Read until at least count frames of msg_type have been received."""
loop = asyncio.get_running_loop()
async def _read_loop() -> None:
while self.frame_counts[msg_type] < count:
data = await loop.sock_recv(self._sock, _READ_CHUNK)
assert data, "server closed the connection unexpectedly"
self.bytes_received += len(data)
self.frame_counts.update(self._parser.feed(data))
await self._recv_once()
await asyncio.wait_for(_read_loop(), timeout)
async def read_frame(self, msg_type: int, timeout: float = 10.0) -> bytes:
"""Return the oldest unconsumed payload of msg_type, skipping earlier frames of other types."""
frames = self._parser.frames
async def _read_loop() -> bytes:
while True:
while self._read_pos < len(frames):
frame_type, payload = frames[self._read_pos]
self._read_pos += 1
if frame_type == msg_type:
return payload
await self._recv_once()
return await asyncio.wait_for(_read_loop(), timeout)
def frames(self, msg_type: int) -> list[bytes]:
"""Payloads of every frame of msg_type received so far."""
return [payload for kind, payload in self._parser.frames if kind == msg_type]
@@ -0,0 +1,80 @@
"""Test the order of the shutdown lifecycle hooks on a forced and on a safe reboot."""
import re
import pytest
from .log_utils import LineWaiter
from .types import APIClientConnectedFactory, RunCompiledFunction
from .wait_utils import wait_or_fail
HOOK_PATTERN = re.compile(r"HOOK (\w+) (\w+)")
# Hooks run in reverse component order, so "first" gets each hook last.
LAST_HOOK = "HOOK first on_powerdown"
async def _record_reboot_hooks(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> list[tuple[str, str]]:
"""Connect, which makes the device reboot, and return its (component, hook) calls in order."""
waiter = LineWaiter()
hooks: list[tuple[str, str]] = []
def on_line(line: str) -> None:
if match := HOOK_PATTERN.search(line):
hooks.append((match[1], match[2]))
waiter.callback(line)
async with (
run_compiled(yaml_config, line_callback=on_line),
api_client_connected(),
):
await wait_or_fail(
waiter.wait_for(LAST_HOOK),
5.0,
"Device did not run the powerdown hooks",
hooks=hooks,
)
return hooks
@pytest.mark.asyncio
async def test_reboot_hook_order_forced(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""A forced reboot runs on_shutdown then on_powerdown, with no safe shutdown or teardown."""
hooks = await _record_reboot_hooks(yaml_config, run_compiled, api_client_connected)
assert hooks == [
("second", "on_shutdown"),
("first", "on_shutdown"),
("second", "on_powerdown"),
("first", "on_powerdown"),
]
@pytest.mark.asyncio
async def test_reboot_hook_order_safe(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""A safe reboot powers down only after every component has finished its teardown."""
hooks = await _record_reboot_hooks(yaml_config, run_compiled, api_client_connected)
assert hooks == [
("second", "on_safe_shutdown"),
("first", "on_safe_shutdown"),
("second", "on_shutdown"),
("first", "on_shutdown"),
("second", "teardown_pending"),
("first", "teardown_pending"),
("second", "teardown_done"),
("first", "teardown_done"),
("second", "on_powerdown"),
("first", "on_powerdown"),
]
@@ -0,0 +1,91 @@
"""A serial proxy must not lose bytes while its client is not reading.
The proxy forwards what it reads from the UART in api messages. When the TCP
pipe to the client is full, the api refuses the message; bytes already read
from the UART would then be gone, leaving holes in the client's stream. The
proxy leaves them in the UART's buffer until the connection can send again.
The sndbuf_pin_component fixture pins the device's send buffers so the pipe
fills deterministically regardless of kernel autotuning; the test waits for
its log line before proceeding.
"""
from __future__ import annotations
import sys
from aioesphomeapi import api_pb2
import pytest
from .log_utils import LineWaiter
from .raw_api_client import (
MESSAGE_TYPE_OF,
RECV_BUFFER_SIZE,
SNDBUF_PIN_LOG,
RawApiClient,
pin_server_sndbuf,
)
from .types import RunCompiledFunction
DATA_RECEIVED = MESSAGE_TYPE_OF[api_pb2.SerialProxyDataReceived]
# Injected by the fixture every 20 ms; no more than the proxy reads in one loop pass
STREAM_CHUNK = 200
# Sent once the client's connection is blocked
STREAM_BYTES = 10_000
# A prime, so the pattern does not line up with the chunk or with the proxy's read size
STREAM_MODULUS = 251
# Sent until the connection is blocked; never part of the numbered bytes
STREAM_FILLER = 0xFF
assert STREAM_BYTES % STREAM_CHUNK == 0
assert STREAM_FILLER >= STREAM_MODULUS
@pytest.mark.skipif(
sys.platform != "linux",
reason="other kernels grow loopback buffers past the pinned size, so the pipe never fills",
)
@pytest.mark.asyncio
async def test_serial_proxy_backpressure(
yaml_config: str,
run_compiled: RunCompiledFunction,
unused_tcp_port: int,
) -> None:
"""Every byte the device sent while the client stalled arrives, in order."""
config = pin_server_sndbuf(yaml_config)
config = config.replace("STREAM_BYTES", str(STREAM_BYTES))
config = config.replace("STREAM_CHUNK", str(STREAM_CHUNK))
config = config.replace("STREAM_MODULUS", str(STREAM_MODULUS))
config = config.replace("STREAM_FILLER", str(STREAM_FILLER))
waiter = LineWaiter()
async with run_compiled(config, line_callback=waiter.callback):
# Fails loudly if the pin never applied
await waiter.wait_for(SNDBUF_PIN_LOG, timeout=10)
async with RawApiClient(
unused_tcp_port, recv_buffer_size=RECV_BUFFER_SIZE
) as client:
await client.connect(client_info="serial-proxy-stall-client")
subscribe = api_pb2.SerialProxyRequest()
subscribe.instance = 0
subscribe.type = api_pb2.SERIAL_PROXY_REQUEST_TYPE_SUBSCRIBE
await client.send_message(subscribe)
# The client now stops reading entirely. The device fills the pipe, then sends
# the numbered bytes into the blocked connection
await waiter.wait_for("stream complete", timeout=120)
received = bytearray()
try:
while len(received) < STREAM_BYTES:
frame = await client.read_frame(DATA_RECEIVED, timeout=10)
data = api_pb2.SerialProxyDataReceived.FromString(frame).data
received += data.replace(bytes([STREAM_FILLER]), b"")
except TimeoutError:
pytest.fail(
f"only {len(received)} of {STREAM_BYTES} bytes arrived; "
"the proxy lost data while the client was not reading"
)
expected = bytes(i % STREAM_MODULUS for i in range(STREAM_BYTES))
assert bytes(received) == expected