Compare commits

...
Author SHA1 Message Date
J. Nick Koston 9d6b50e947 Merge branch 'web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-17 09:19:03 -06:00
J. Nick Koston e490785ad3 Merge branch 'web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-17 09:19:02 -06:00
J. Nick Koston 7f1ba24ab1 Merge branch 'dev' into web-server-idf-stream-response 2026-11-17 09:18:57 -06:00
Mattheinrichs 84dea8078b [cst816] Ignore idle 0xFF touch frames and invalid coordinates (#22435) 2026-11-17 08:58:24 -06:00
Jacopo Labardi dbf6af2fd7 [logger] Qualify the host clock function (#22838) 2026-11-17 08:50:19 -06:00
David Woodhouse bc6fb7082f [climate] Don't fail on a platform entry that only holds nested climates (#22844) 2026-11-17 08:49:00 -06:00
tomaszduda23andClaude cfa27cea58 [npm1300] Fix the thermistor resistance error message (#22835)
Co-authored-by: Claude <noreply@anthropic.com>
2026-11-17 17:40:04 +03:00
rwrozelleandClaude Opus 5.5 5c061f9e1e [espidf] Hide the ccache binary in the forced-on, no-binary test (#22842)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 09:29:34 -05:00
Jesse Hills 34b24d0cf5 Merge branch 'beta' into dev 2026-11-17 22:25:16 +13:00
Jesse Hills e27614fda3 Merge pull request #22833 from esphome/bump-2026.11.0b6
2026.11.0b6
2026-11-17 22:24:55 +13:00
J. Nick Koston 8426d04138 [api] Make the event order backpressure test wait until the pipe is full (#22112) 2026-11-17 21:09:01 +13:00
641c8bedc9 [npm1300] Use datasheet defaults, require the thermistor setting for charging, fix charger status (#22769)
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-11-17 09:00:10 +01:00
Jesse Hills 4a39ca1b71 Fix 2026-11-17 20:54:40 +13:00
Jesse Hills 4a218fdbd8 Remove extra test 2026-11-17 20:47:40 +13:00
Jesse Hills d8fc1cd214 Bump version to 2026.11.0b6 2026-11-17 18:54:23 +13:00
J. Nick Koston 1e030b7371 [modem] Set the APN while the SIM is powered down (#22817) 2026-11-17 18:54:20 +13:00
J. Nick Koston 3b113f2c39 [wifi] Join the next network after a failed hidden network probe on ESP32 (#22764) 2026-11-17 18:54:20 +13:00
J. Nick Koston 73468592a9 [remote_base] Fix the Haier and Mirage remote_receiver binary sensors failing to compile 2026-11-17 18:54:06 +13:00
Jonathan Swoboda a1b456150d [esp32][libretiny] Make delay() wait at least the requested time (#22822) 2026-11-17 18:53:11 +13:00
J. Nick Koston 865a883abb [remote_base] Fix the canalsatld binary sensor and trigger build 2026-11-17 18:53:11 +13:00
J. Nick Koston 357a6717c8 [core] Release only the lwIP core lock a LwIPLock took (#22683) 2026-11-17 18:53:11 +13:00
J. Nick Koston bcb434e05e [esp32_ble] Stop Bluedroid re-running a failed queued MTU or execute write request (#22670) 2026-11-17 18:53:11 +13:00
J. Nick Koston fb65fcc5f6 [esp32_power_management] Clamp the tick step after light sleep so it cannot trip vTaskStepTick (#22637) 2026-11-17 18:53:11 +13:00
J. Nick Koston b5e379a9d4 [uart] Stop ESP8266 software serial TX from tripping the Wi-Fi watchdog at 57600 baud (#22641) 2026-11-17 18:53:11 +13:00
J. Nick Kostonandpre-commit-ci-lite[bot] 05286dfd5d [api] Send queued entity states before DisconnectRequest on shutdown (#22625)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
2026-11-17 18:53:11 +13:00
J. Nick Koston 36184f09aa [zephyr] Count micros() in microseconds and busy-wait in delayMicroseconds() (#22612) 2026-11-17 18:53:11 +13:00
Adam Matthews 449933891e [wifi] Clear the error flag when the adapter restart is skipped (#22797) 2026-11-17 18:53:11 +13:00
Jonathan Swoboda a5bfa6dac3 [esp32_ble] Apply the Bluedroid workarounds to the ESP-IDF 6.1 releases that need them (#22806) 2026-11-17 18:53:11 +13:00
Jonathan Swoboda 5aaa5b0552 [esp32_ble] Replace Bluedroid's sscanf callers on ESP-IDF 6 too (#22805) 2026-11-17 18:53:11 +13:00
J. Nick Koston 9ab090a818 [i2c] Refuse RP2040 writes larger than the Wire buffer instead of leaving the bus stuck (#22791) 2026-11-17 18:53:10 +13:00
J. Nick Koston 02d0bb56a6 [cc1101] Refuse begin_tx in packet mode (#22636) 2026-11-16 22:21:59 -06:00
Mark Mayo e12784543d [logger] Copy log lines that have no arguments instead of formatting them (#22644) 2026-11-16 22:16:47 -06:00
J. Nick Koston bc3e9c88f4 [select] Set a fixed option by index and look up a template lambda's option once (#22655) 2026-11-16 22:48:05 -05:00
luar123andCopilot Autofix powered by AI 7e3c2ebc10 [zigbee] refactor ZigbeeAttribute (#22765)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-11-16 22:31:05 -05:00
J. Nick Koston 3f6c0f35fe [modem] Set the APN while the SIM is powered down (#22817) 2026-11-17 16:23:11 +13:00
J. Nick Koston 200bf69035 [wifi] Join the next network after a failed hidden network probe on ESP32 (#22764) 2026-11-17 16:23:06 +13:00
huangkongshengandhuangkongsheng 0054041d6a [core] Reject sign-padded bind key pairs (#22660)
Signed-off-by: huangkongsheng <huangkongsheng@users.noreply.github.com>
Co-authored-by: huangkongsheng <huangkongsheng@users.noreply.github.com>
2026-11-16 22:15:45 -05:00
J. Nick Koston 205318ee1a Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-15 19:04:55 -06:00
J. Nick Koston 48d8df449a Merge remote-tracking branch 'origin/web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-15 19:04:52 -06:00
J. Nick Koston 383ac1b41a Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response 2026-11-15 19:04:42 -06:00
J. Nick Koston 0ac2299482 Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into HEAD 2026-11-14 08:40:18 -06:00
J. Nick Koston b58038af63 Merge remote-tracking branch 'origin/web-server-idf-stream-response' into HEAD 2026-11-14 08:39:50 -06:00
J. Nick Koston c741de7a47 Merge remote-tracking branch 'origin/dev' into HEAD 2026-11-14 08:38:08 -06:00
J. Nick Koston 8a5e07e29f [web_server] Keep the server out of host builds, which only need sse_chunk.cpp for unit tests
tests/components/web_server now pulls web_server into the --all host
build, where another test defines USE_WEBSERVER.
2026-11-14 00:19:08 -06:00
J. Nick Koston f7b52be670 Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-13 23:15:51 -06:00
J. Nick Koston d5317669c9 Merge remote-tracking branch 'origin/web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-13 23:15:42 -06:00
J. Nick Koston 5d0bd88bc6 Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response 2026-11-13 23:13:08 -06:00
pre-commit-ci-lite[bot] b714d8f69b apply automatic formatting fixes 2026-11-14 04:33:03 +00:00
J. Nick Koston ecabdbb24b [web_server] Keep the shared /events layer free of IDF-only types
The gather type comes from stream_response.h, the default header count is
defined on every platform, the session takes a non-const request, and the
fd in log lines goes through log_id(). Also drops a dead forward declaration
and an unused alias, and narrows the source filter.
2026-11-13 22:28:19 -06:00
J. Nick Koston ffbc4148fb [web_server] Move the ESP32 /events layer to web_server/event_source
The event source and its sessions move out of web_server_idf into
web_server/event_source.h/.cpp, sse_chunk moves to web_server and
stream_head.h to web_server_base, so the ESPAsyncWebServer platforms can
share them later. web_server_base/stream_response.h picks the stream
transport. No behaviour change; off ESP32 the build is unchanged.
2026-11-13 22:16:53 -06:00
J. Nick Koston 2169167908 [web_server] Name the iterator's session member session_ 2026-11-13 22:00:50 -06:00
J. Nick Koston 900574c03a [web_server_idf] List entities to the new /events session only
The listing iterator held the whole event source, so each new client's
detail_all listing went to every open session. Hold the session instead,
as the Arduino path already does.
2026-11-13 21:55:42 -06:00
J. Nick Koston 7b1f9c6bf1 Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response
# Conflicts:
#	.agents/skills/hardware-test/reference/recipes/web_server.md
2026-11-07 22:09:36 -06:00
J. Nick Koston 3587e29725 Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 22:21:12 -06:00
J. Nick Koston d4244c5388 [web_server_idf] Correct the session close order comment and point stall docs at the stream 2026-11-04 21:47:51 -06:00
J. Nick Koston bec53baf1d Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 21:29:11 -06:00
J. Nick Koston 98fd3786dd [web_server_idf] Create the stream send mutex at boot and test that a failed head batch stops the rest 2026-11-04 21:15:36 -06:00
J. Nick Koston 1f531ff022 Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 20:58:44 -06:00
J. Nick Koston 65e7eb6b06 [web_server_idf] Note that sess_ctx is reserved for AsyncStreamResponse 2026-11-04 20:41:33 -06:00
J. Nick Koston 7538864e1e [web_server_idf] Keep a stream from sending on a socket httpd has closed 2026-11-04 20:36:27 -06:00
J. Nick Koston c0f97475d3 [web_server_idf] Document the stream head body as a C string 2026-11-04 20:07:37 -06:00
J. Nick Koston df6fcaa6fe Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response
# Conflicts:
#	esphome/components/web_server_idf/web_server_idf.cpp
#	esphome/components/web_server_idf/web_server_idf.h
2026-11-03 18:47:25 -06:00
J. Nick Koston eb1b793af9 Merge branch 'dev' into web-server-idf-stream-response 2026-10-30 15:46:21 -10:00
J. Nick Koston b8ce904e0c [web_server_idf] Let a stream response header replace a default header of the same name 2026-10-30 14:24:31 -10:00
J. Nick Koston 6befa73b00 [web_server_idf] Shut the socket down when the stream head is not fully sent 2026-10-30 13:09:28 -10:00
J. Nick Koston 02211e15d8 [web_server_idf] Skip the stall clock for empty writes and clear it on send errors 2026-10-30 12:52:36 -10:00
J. Nick Koston dbbe02f423 [web_server_idf] Include stream_head.h without web_server, for HttpHeader 2026-10-30 12:35:17 -10:00
J. Nick Koston 97a1aa781e [web_server_idf] Simplify the stream response stall clock and head batching
Log the no-memory warning once per stall, free the closing session's buffers once,
add is_stalled() and check that the event stream head fits one write.
2026-10-30 12:22:07 -10:00
J. Nick Koston 0b67a754cd [web_server_idf] Build the stream head tests only for host 2026-10-30 12:15:18 -10:00
J. Nick Koston 320b6a8221 [web_server_idf] Factor the stream response head, iovec advance and stall clock out for host tests
Moves the pure parts of AsyncStreamResponse into stream_head.h (no httpd or
lwIP includes) and adds gtests: the event stream head is compared byte for
byte against what httpd_resp_send_chunk() sent, plus raw heads, batching,
short writes and the stall clock including a millis() rollover.
2026-10-30 12:06:59 -10:00
J. Nick Koston 4e5a5ec32d [web_server_idf] Send the stream response head in one write and tidy the stall clock
Gather the status line, headers and optional body into one iovec list
and send it with a single blocking sendmsg, instead of one httpd_send per
piece. Split the stall clock into start_stall_(), stall_expired_() and
clear_stall_(), and use is_connected() and is_closing() in the event
stream.
2026-10-30 11:40:30 -10:00
J. Nick Koston 09ce8d7d6e [web_server_idf] Add a long-lived streaming response that components can use
Move the session handling of AsyncEventSourceResponse into a new
AsyncStreamResponse base: the response head and session context set up
in the request handler, the non-blocking send override, main loop gather
writes with the send stall clock, the identity-checked close through
httpd_queue_work and the LRU refresh. The event stream now derives from
it, so other components can stream raw or chunked bytes from the main
loop without their own tasks or raw socket calls.

The response head is written directly instead of through
httpd_resp_send_chunk(), so a stream does not have to start with a data
chunk. The event stream still sends the same bytes on the wire.
2026-10-30 11:35:09 -10:00
68 changed files with 2843 additions and 1063 deletions

No files matched your search

+4 -10
View File
@@ -515,18 +515,12 @@ void CC1101Component::dump_config() {
}
void CC1101Component::begin_tx() {
this->wake_up();
// Read the mode before the PKTCTRL0 write below overwrites it in state_
const bool packet_mode = this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
// Ensure Packet Format is 3 (Async Serial)
this->write_(Register::PKTCTRL0, 0x32);
if (this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
ESP_LOGW(TAG, "begin_tx is for async serial mode; use send_packet in packet mode");
return;
}
ESP_LOGV(TAG, "Beginning TX sequence");
if (this->gdo0_pin_ != nullptr) {
// setup() only attaches the interrupt in packet mode. In async serial mode the pin may be
// shared with remote_receiver, and detaching here would remove the receiver's interrupt.
if (packet_mode) {
this->gdo0_pin_->detach_interrupt();
}
this->gdo0_pin_->pin_mode(gpio::FLAG_OUTPUT);
}
// Transition through IDLE to bypass CCA (Clear Channel Assessment) which can
@@ -63,13 +63,25 @@ void CST816Touchscreen::update_touches() {
uint8_t data[13];
if (!this->status_comm_result(this->read_bytes(REG_STATUS, data, sizeof data)))
return;
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
// The low nibble is the touch count. CST816/CST820 are single-touch;
// CST826/CST836 can report two points, but this driver only reads the first
// point. Some CST8xx controllers report an all-0xFF frame when idle or after
// disabling auto-sleep: the count lands above two and the coordinates are
// the 0xFFF sentinel, so both guards below reject it.
uint8_t num_of_touches = low_nibble(data[REG_TOUCH_NUM]);
if (num_of_touches == 0) {
return;
}
if (num_of_touches > 2) {
return;
}
uint16_t x = encode_uint16(low_nibble(data[REG_XPOS_HIGH]), data[REG_XPOS_LOW]);
uint16_t y = encode_uint16(low_nibble(data[REG_YPOS_HIGH]), data[REG_YPOS_LOW]);
if (x == 0x0FFF || y == 0x0FFF) {
return;
}
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
this->add_raw_touch_position_(0, x, y);
}
+3 -1
View File
@@ -605,7 +605,9 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_type(conf[CONF_TYPE]))
for conf in config[CONF_SELECTS]:
var = await select.new_select(conf, options=conf[CONF_OPTIONS])
var = await select.new_select(
conf, options=conf[CONF_OPTIONS], fixed_options=True
)
await cg.register_component(var, conf)
for conf in config[CONF_SENSORS]:
+1 -1
View File
@@ -41,5 +41,5 @@ CONFIG_SCHEMA = (
async def to_code(config: ConfigType) -> None:
var = await select.new_select(config, options=DUCO_MODE_OPTIONS)
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
await register_duco_device(var, config, config[CONF_DUCO_ID])
+22 -1
View File
@@ -114,6 +114,26 @@ struct LogBuffer {
this->finalize_();
}
#endif
// Writes a message that needs no formatting, truncated and stripped of trailing newlines as format_body() does
void HOT write_unformatted_body(const char *text, size_t len) {
if (!this->full_()) {
const uint16_t limit = this->remaining_() - 1; // vsnprintf() keeps the last byte for the null terminator
const uint16_t copy_len = (len < limit) ? static_cast<uint16_t>(len) : limit;
memcpy(this->current_(), text, copy_len);
this->pos += copy_len;
this->strip_trailing_newlines_();
}
this->finalize_();
}
// write_unformatted_body() for a message in flash
inline void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body_p(ESPHOME_PGM_P text) {
if (!this->full_()) {
char *const start = this->current_();
this->pos = static_cast<uint16_t>(progmem_copy_str(start, start + this->remaining_() - 1, text) - this->data);
this->strip_trailing_newlines_();
}
this->finalize_();
}
static constexpr uint16_t ANSI_COLOR_LEN = 7; // "\033[{bold};3{color}m" written by write_header for level != 0
static constexpr uint16_t ANSI_RESET_LEN = 4; // "\033[0m"
// Largest replay header: color plus "@4294967295ms "
@@ -161,7 +181,8 @@ struct LogBuffer {
this->pos += ANSI_RESET_LEN;
}
}
void strip_trailing_newlines_() {
// Forced inline: an outlined copy slows every formatted line
inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
while (this->pos > 0 && this->data[this->pos - 1] == '\n')
this->pos--;
}
+53 -5
View File
@@ -19,6 +19,15 @@ namespace esphome::logger {
ESPHOME_LOG_TAG(TAG, "logger");
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes to a socket would
// take that lock again and deadlock, so such a line is queued for loop() instead. It can print after later inline
// lines, and is lost if loop() never runs again (safe_reboot()).
static inline bool HOT ESPHOME_ALWAYS_INLINE under_lwip_core_lock() {
return sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED);
}
#endif
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
// Implementation for multi-threaded platforms (ESP32 with FreeRTOS, Host with pthreads, LibreTiny with FreeRTOS,
// Zephyr) Main thread/task always uses direct buffer access for console output and callbacks
@@ -51,11 +60,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
// Pass nullptr for thread_name since we already know this is the main task
if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes
// to a socket would take that lock again and deadlock, so queue the message for loop() instead. Such lines
// can print after later inline ones, and are lost if loop() never runs again (safe_reboot()).
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED))
[[unlikely]] {
if (under_lwip_core_lock()) [[unlikely]] {
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
return;
}
@@ -159,6 +164,49 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
}
#endif // USE_ESP8266
// Not shared with log_message_to_buffer_and_send_(): sharing it slows formatted lines
inline void HOT ESPHOME_ALWAYS_INLINE Logger::send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf) {
#ifdef USE_LOGGER_BOOT_LOG_BUFFER
if (!this->boot_log_done_)
this->boot_log_capture_(level, tag, buf);
#endif
this->notify_listeners_(level, tag, buf);
this->write_log_buffer_to_console_(buf);
}
bool HOT Logger::log_str_(uint8_t level, const char *tag, int line, const char *msg) {
if (level > this->level_for(tag))
return true;
// Check the task first: the guard belongs to the main task and must not be read from other threads
if (!this->is_main_task_())
return false;
if (this->main_task_recursion_guard_)
return true;
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// The formatting path queues it
if (under_lwip_core_lock()) [[unlikely]]
return false;
#endif
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body(msg, strlen(msg));
this->send_unformatted_line_(level, tag, buf);
return true;
}
#ifdef USE_ESP8266
void Logger::log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg) {
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
return;
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body_p(reinterpret_cast<PGM_P>(msg));
this->send_unformatted_line_(level, tag, buf);
}
#endif // USE_ESP8266
#if defined(USE_LOGGER_RUNTIME_TAG_LEVELS) || defined(USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS)
// Level for tag from a per-tag map, or fallback when the tag has none; tags may live in flash
static inline uint8_t tag_level(const std::map<const char *, uint8_t, CStrCompare> &levels, const char *tag,
+22 -1
View File
@@ -223,6 +223,12 @@ class Logger final : public Component {
// NOLINTNEXTLINE(readability-identifier-naming)
void log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
#endif
// Copies a line with no '%' without formatting; false means the caller must format it
bool log_str_(uint8_t level, const char *tag, int line, const char *msg); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg);
#endif
protected:
// RAII guard for recursion flags - sets flag on construction, clears on destruction
@@ -292,7 +298,7 @@ class Logger final : public Component {
}
// Helper to write log buffer to console if logging is enabled
inline void HOT write_log_buffer_to_console_(LogBuffer &buf) {
inline void HOT ESPHOME_ALWAYS_INLINE write_log_buffer_to_console_(LogBuffer &buf) {
if (this->baud_rate_ > 0)
this->write_to_console_(buf);
}
@@ -321,6 +327,21 @@ class Logger final : public Component {
this->write_log_buffer_to_console_(buf);
}
inline void send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf);
// True on the task that runs the main loop, and always on single-task platforms
inline bool HOT is_main_task_() const {
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
return xTaskGetCurrentTaskHandle() == this->main_task_;
#elif defined(USE_ZEPHYR)
return k_current_get() == this->main_task_;
#elif defined(USE_HOST)
return pthread_equal(pthread_self(), this->main_thread_);
#else
return true;
#endif
}
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
// Helper to format a pre-formatted message from the task log buffer and notify listeners
inline void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line,
+1 -1
View File
@@ -9,7 +9,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
char buffer[TIMESTAMP_LEN + ESPHOME_LOGGER_TX_BUFFER_SIZE + HEADROOM];
time_t rawtime;
time(&rawtime);
::time(&rawtime);
struct tm timeinfo;
localtime_r(&rawtime, &timeinfo); // Thread-safe version
size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo);
+1 -1
View File
@@ -34,6 +34,6 @@ async def to_code(config: ConfigType) -> None:
levels = list(LOG_LEVELS)
index = levels.index(CORE.data[DOMAIN][CONF_LEVEL])
levels = levels[: index + 1]
var = await select.new_select(config, options=levels)
var = await select.new_select(config, options=levels, fixed_options=True)
await register_parented(var, parent)
await register_component(var, config)
@@ -103,7 +103,9 @@ async def to_code(config: ConfigType) -> None:
)
await cg.register_component(var, config)
await select.register_select(var, config, options=list(options_map.keys()))
await select.register_select(
var, config, options=list(options_map.keys()), fixed_options=True
)
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
cg.add(parent.add_sensor_item(var))
+32 -23
View File
@@ -56,7 +56,8 @@ static constexpr uint8_t PIN_MAX_UNCONFIRMED_SENDS = 2;
// The attachment is polled every 4 s; the signal and operator queries only every 4th poll
static constexpr uint32_t ATTACH_POLL_MS = 4000;
static constexpr uint8_t ATTACH_DIAG_EVERY = 4;
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr int RADIO_STATE_MINIMUM = 0; // CFUN=0: radio off and the SIM powered down
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr uint8_t UART_EVENT_QUEUE_SIZE = 10;
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
@@ -670,22 +671,28 @@ ModemState ModemComponent::handle_state_enabling_() {
this->set_fault_(ModemFault::MODEM_FAULT_TEARDOWN);
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
// esp_modem keeps the last line before OK as the reply, which can be the echoed command
if (this->dce_->set_echo(false) != command_result::OK) {
ESP_LOGW(TAG, "Failed to disable command echo");
}
int radio_state = 0;
if (this->dce_->get_radio_state(radio_state) != command_result::OK) {
ESP_LOGW(TAG, "Failed to read the radio state; skipping the radio off step");
} else if (radio_state != RADIO_STATE_RF_OFF) {
// Drops a session left by a warm reboot; unlike CFUN=0, which powers the SIM down on the SIM7600, CFUN=4
// keeps the SIM readable for the PIN and the APN. A modem that refuses is used as it is.
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio off");
}
// The SIM7670G only uses an APN written at CFUN=0 (else the attach fails with an ESM failure); CFUN=0 also drops a
// session left by a warm reboot. A modem that refuses gets the APN after the SIM check instead, as before.
esp_modem::PdpContext pdp(this->apn_);
this->apn_pending_ = this->dce_->set_radio_state(RADIO_STATE_MINIMUM) != command_result::OK ||
this->dce_->set_pdp_context(pdp) != command_result::OK;
if (this->apn_pending_) {
ESP_LOGW(TAG, "Failed to set the APN with the SIM powered down; setting it after the SIM check");
}
// CFUN=4 powers the SIM up with the radio still off; a modem without it needs CFUN=1 to bring the SIM back
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK &&
this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to power the SIM up");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
this->modem_synced_ = true; // it answers, so a disable() still tears it down
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
if (this->baud_rate_ != 0 && this->baud_rate_ != this->current_baud_rate_) {
ESP_LOGD(TAG, "Setting baud rate: %d -> %d", this->current_baud_rate_, this->baud_rate_);
@@ -752,7 +759,7 @@ ModemState ModemComponent::handle_state_connecting_() {
return ModemState::MODEM_STATE_ENABLING;
}
}
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
switch (this->prepare_sim_()) {
case SimResult::SIM_RESULT_READY:
break;
@@ -769,13 +776,14 @@ ModemState ModemComponent::handle_state_connecting_() {
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
return ModemState::MODEM_STATE_DISCONNECTING;
}
// The APN has to be in the PDP context before the radio comes on and the modem attaches; esp_modem only
// sends it when PPP starts
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
if (this->apn_pending_) {
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
this->apn_pending_ = false;
}
if (this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio on");
@@ -862,11 +870,12 @@ ModemState ModemComponent::handle_state_disconnecting_() {
ESP_LOGW(TAG, "Failed to leave data mode");
this->set_teardown_fault_();
}
// Turn the radio off; waiting for the lost IP event could take up to 2 min. A reconnect keeps the SIM powered
// so it is not unlocked again; only disable() powers it down.
const bool reconnect = this->target_state_.load() == ModemState::MODEM_STATE_CONNECTED;
if (this->dce_->set_radio_state(reconnect ? RADIO_STATE_RF_OFF : 0) == command_result::OK) {
// Turn the radio off; waiting for the lost IP event could take up to 2 min. The SIM is powered down as well, as
// ENABLING does anyway on a reconnect.
if (this->dce_->set_radio_state(RADIO_STATE_MINIMUM) == command_result::OK) {
ESP_LOGI(TAG, "Radio off");
} else if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) == command_result::OK) {
ESP_LOGW(TAG, "Radio off, but the SIM could not be powered down");
} else {
ESP_LOGW(TAG, "Radio off failed");
this->set_teardown_fault_();
@@ -299,6 +299,7 @@ class ModemComponent : public Component {
bool connect_timeout_armed_{false};
bool modem_synced_{false}; // a modem that never answered has nothing to tear down
bool resync_{false}; // the attachment read failed, so CONNECTING syncs again first
bool apn_pending_{false}; // ENABLING could not set the APN at CFUN=0, so CONNECTING sets it after the SIM check
#ifdef USE_MODEM_SEND_AT
char at_command_[AT_COMMAND_SIZE];
#endif
+117 -39
View File
@@ -1,4 +1,5 @@
from collections.abc import Callable
import logging
from esphome import automation
import esphome.codegen as cg
@@ -8,6 +9,8 @@ import esphome.config_validation as cv
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@tomaszduda23"]
DEPENDENCIES = ["i2c"]
DOMAIN = "npm1300"
@@ -42,20 +45,18 @@ NPM1300_CHILD_SCHEMA = cv.Schema(
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
)
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
NPM1300Status = npm1300_ns.struct("NPM1300Status")
# The status registers read on each update
STATUS_PUBLISHER = cg.HubFunction(
"status",
cg.void,
[
(cg.uint8, "vbus_status"),
(cg.uint8, "charge_status"),
(cg.uint8, "error_reason"),
],
[(NPM1300Status.operator("const").operator("ref"), "status")],
define="USE_NPM1300_STATUS_PUBLISHER",
)
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
STATUS = STATUS_PUBLISHER.param("status")
VBUS_STATUS = STATUS.vbus
CHARGE_STATUS = STATUS.charge
ERROR_REASON = STATUS.error_reason
# The ADC results and the amperes per code of the battery current
DATA_PUBLISHER = cg.HubFunction(
"data",
@@ -77,7 +78,10 @@ OUTPUTS = NPM1300Output.options(
# Written for a field the YAML leaves out
NPM1300_UNSET = 0xFF
# ADCNTCRSEL codes, by kiloohms
THERMISTOR_NONE = "none"
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
# B25/50 of the thermistors the product specification lists for each resistance, by kiloohms
THERMISTOR_BETAS = {10: 3380, 47: 4050, 100: 4250}
LDO_MODES = {"load_switch": 0, "ldo": 1}
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
# BCHGVTRICKLESEL, by millivolts
@@ -101,14 +105,13 @@ def _one_of_units(
validator: Callable[[object], float],
allowed: list[int],
unit: str,
scale: float = 1000,
) -> Callable[[object], float]:
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
"""Validate a quantity with `validator` and accept only the listed values, given in thousandths of `unit`."""
def validate(value: object) -> float:
value = validator(value)
if round(value * scale) not in allowed:
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
if _millis(value) not in allowed:
options = ", ".join(f"{v / 1000:g}{unit}" for v in allowed)
raise cv.Invalid(f"Must be one of {options}")
return value
@@ -135,6 +138,23 @@ def _stepped(
return validate
def _thermistor_resistance(value: object) -> float | str:
if isinstance(value, str) and value.lower() == THERMISTOR_NONE:
return THERMISTOR_NONE
value = cv.resistance(value)
if round(value) not in (kilohms * 1000 for kilohms in THERMISTOR_RESISTANCES):
options = ", ".join(f"{kilohms}kOhm" for kilohms in THERMISTOR_RESISTANCES)
raise cv.Invalid(f"Must be one of {options} or {THERMISTOR_NONE}")
return value
def thermistor_beta(config: ConfigType) -> int:
"""The B constant of the hub's thermistor: the configured one, or the one listed for its resistance."""
if (beta := config.get(CONF_THERMISTOR_BETA)) is not None:
return beta
return THERMISTOR_BETAS[round(config[CONF_THERMISTOR_RESISTANCE] / 1000)]
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
@@ -150,15 +170,58 @@ LDO_SCHEMA = BUCK_SCHEMA.extend(
}
)
CHARGER_SCHEMA = cv.Schema(
WARM_TERMINATION_VOLTAGE_DEFAULT = 3.6
def _warm_termination_default(config: ConfigType) -> ConfigType:
"""Keep the default warm termination voltage at or below the termination voltage.
Warn when the default stays below a higher termination voltage.
"""
if (
not isinstance(config, dict)
or CONF_WARM_TERMINATION_VOLTAGE in config
or CONF_TERMINATION_VOLTAGE not in config
):
return config
try:
termination = cv.voltage(config[CONF_TERMINATION_VOLTAGE])
except cv.Invalid:
# The schema reports it
return config
if termination < WARM_TERMINATION_VOLTAGE_DEFAULT - 0.001:
# A warm battery is never charged higher than a cool one
return {
**config,
CONF_WARM_TERMINATION_VOLTAGE: config[CONF_TERMINATION_VOLTAGE],
}
if termination > WARM_TERMINATION_VOLTAGE_DEFAULT + 0.001:
_LOGGER.warning(
"npm1300 '%s' is not set, so a warm battery (45 °C to 60 °C by default) stops "
"charging at %.2fV instead of the %.2fV of '%s'. Set '%s' to the value your "
"battery allows when warm",
CONF_WARM_TERMINATION_VOLTAGE,
WARM_TERMINATION_VOLTAGE_DEFAULT,
termination,
CONF_TERMINATION_VOLTAGE,
CONF_WARM_TERMINATION_VOLTAGE,
)
return config
_CHARGER_SETTINGS_SCHEMA = cv.Schema(
{
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
# Defaults are the reset values of the product specification
cv.Optional(CONF_CHARGE_CURRENT, default="32mA"): _stepped(
cv.current, 32, 800, 2, "A"
),
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
cv.Optional(CONF_TERMINATION_VOLTAGE, default="3.6V"): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(
CONF_WARM_TERMINATION_VOLTAGE,
default=f"{WARM_TERMINATION_VOLTAGE_DEFAULT}V",
): _one_of_units(cv.voltage, TERMINATION_VOLTAGES, "V"),
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
TERMINATION_CURRENTS
),
@@ -169,22 +232,37 @@ CHARGER_SCHEMA = cv.Schema(
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
),
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
cv.Optional(CONF_ENABLED, default=False): cv.boolean,
}
)
CHARGER_SCHEMA = cv.All(_warm_termination_default, _CHARGER_SETTINGS_SCHEMA)
CONFIG_SCHEMA = (
def _check_thermistor(config: ConfigType) -> ConfigType:
"""A wrong thermistor shifts the battery temperature the charger sees, so charging needs one chosen."""
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
if CONF_CHARGER in config and resistance is None:
raise cv.Invalid(
f"'{CONF_THERMISTOR_RESISTANCE}' is required with '{CONF_CHARGER}': set the nominal "
f"resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
path=[CONF_THERMISTOR_RESISTANCE],
)
if CONF_THERMISTOR_BETA in config and resistance in (None, THERMISTOR_NONE):
raise cv.Invalid(
f"'{CONF_THERMISTOR_BETA}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
"resistance of the thermistor",
path=[CONF_THERMISTOR_BETA],
)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(NPM1300Component),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
),
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
min=1, max=65535
),
cv.Optional(CONF_THERMISTOR_RESISTANCE): _thermistor_resistance,
cv.Optional(CONF_THERMISTOR_BETA): cv.int_range(min=1, max=65535),
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
@@ -193,7 +271,8 @@ CONFIG_SCHEMA = (
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x6B))
.extend(i2c.i2c_device_schema(0x6B)),
_check_thermistor,
)
@@ -208,7 +287,7 @@ def _vbus_current_limit_code(amps: float) -> int:
def _charger_config(config: ConfigType) -> cg.StructInitializer:
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
warm = _termination_voltage_code(config[CONF_WARM_TERMINATION_VOLTAGE])
return cg.StructInitializer(
NPM1300ChargerConfig,
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
@@ -217,10 +296,7 @@ def _charger_config(config: ConfigType) -> cg.StructInitializer:
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
),
("termination_voltage", termination),
(
"warm_termination_voltage",
termination if warm is None else _termination_voltage_code(warm),
),
("warm_termination_voltage", warm),
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
("recharge", config[CONF_RECHARGE]),
@@ -247,6 +323,13 @@ def _output_config(config: ConfigType | None) -> cg.StructInitializer:
def _config(config: ConfigType) -> cg.StructInitializer:
charger = config.get(CONF_CHARGER)
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
if resistance is None:
thermistor, beta = NPM1300_UNSET, 0
elif resistance == THERMISTOR_NONE:
thermistor, beta = 0, 0
else:
thermistor = THERMISTOR_RESISTANCES[round(resistance / 1000)]
beta = thermistor_beta(config)
return cg.StructInitializer(
NPM1300Config,
(
@@ -254,13 +337,8 @@ def _config(config: ConfigType) -> cg.StructInitializer:
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
),
("charger", None if charger is None else _charger_config(charger)),
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
(
"thermistor",
NPM1300_UNSET
if resistance is None
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
),
("thermistor_beta", beta),
("thermistor", thermistor),
("charger_configured", charger is not None),
)
+51 -34
View File
@@ -29,6 +29,7 @@ static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
static constexpr uint16_t REG_BCHG_NTC_STATUS = 0x0332;
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
@@ -53,6 +54,9 @@ static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
// VBUSINSTATUS
static constexpr uint8_t VBUS_PRESENT = 0x01;
// NTCSTATUS
static constexpr uint8_t NTC_STATUS_COLD = 0x01;
static constexpr uint8_t NTC_STATUS_HOT = 0x08;
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
@@ -107,26 +111,36 @@ float ntc_temperature(uint16_t code, float inv_beta) {
#endif
#ifdef USE_TEXT_SENSOR
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot",
"Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage",
"Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging");
static constexpr uint8_t CHARGER_STATUS_FIRST_BIT = 3;
// BCHGCHARGESTATUS bits in order of priority, reported from string CHARGER_STATUS_FIRST_BIT on
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
CHARGE_STATUS_CONSTANT_VOLTAGE};
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
if (error_reason != 0)
static uint8_t charger_status_index(const NPM1300Status &status) {
if ((status.vbus & VBUS_PRESENT) == 0)
return 0;
if (status.error_reason != 0)
return 1;
if ((status.charger_enable & 1) == 0)
return 2;
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return i + 1;
if ((status.charge & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return CHARGER_STATUS_FIRST_BIT + i;
}
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
// The charger stops without an error outside the cold and hot limits of the battery temperature
if ((status.ntc & NTC_STATUS_COLD) != 0)
return 8;
if ((status.ntc & NTC_STATUS_HOT) != 0)
return 9;
return 10;
}
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason) {
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status) {
const uint8_t index = charger_status_index(status);
if (index == last)
return;
last = index;
@@ -198,7 +212,7 @@ bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
split_9bit(charger.discharge_limit, discharge);
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
// Without a thermistor the charger reports an NTC error and does not charge
if (config.thermistor == NPM1300_UNSET)
if (config.thermistor == 0 || config.thermistor == NPM1300_UNSET)
disable |= CHARGER_DISABLE_NTC;
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
@@ -292,33 +306,40 @@ void NPM1300Component::hibernate(uint32_t duration_ms) {
}
void NPM1300Component::update() {
uint8_t vbus_status;
uint8_t charge_status;
uint8_t error_reason;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
NPM1300Status status;
// NTCSTATUS, DIETEMPSTATUS and BCHGCHARGESTATUS
uint8_t charger_status[3];
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &status.vbus) &&
this->read_byte_reg16(REG_BCHG_ENABLE_SET, &status.charger_enable) &&
this->read_register16(REG_BCHG_NTC_STATUS, charger_status, sizeof(charger_status)) ==
i2c::ERROR_OK &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &status.error_reason)))
return;
status.ntc = charger_status[0];
status.charge = charger_status[2];
// Without a thermistor the NTC pin is grounded and can read as a hot battery
if (progmem_read_byte(&this->config_->thermistor) == 0)
status.ntc = 0;
// The chip goes back to its default VBUS current limit whenever VBUS is removed
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
const bool vbus_present = (status.vbus & VBUS_PRESENT) != 0;
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
!this->status_comm_check(this->apply_vbus_current_limit_()))
return;
this->vbus_present_ = vbus_present;
if (error_reason != this->last_error_reason_) {
this->last_error_reason_ = error_reason;
if (error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
if (status.error_reason != this->last_error_reason_) {
this->last_error_reason_ = status.error_reason;
if (status.error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", status.error_reason);
}
}
#ifdef USE_NPM1300_STATUS_PUBLISHER
if (this->status_publisher_ != nullptr)
this->status_publisher_(vbus_status, charge_status, error_reason);
this->status_publisher_(status);
#endif
#ifdef USE_SWITCH
if (!this->status_comm_check(this->publish_switches_()))
if (!this->status_comm_check(this->publish_switches_(status)))
return;
#endif
@@ -348,16 +369,10 @@ void NPM1300Component::update() {
}
#ifdef USE_SWITCH
bool NPM1300Component::publish_switches_() {
bool NPM1300Component::publish_switches_(const NPM1300Status &status) {
PUBLISH_STATE(this->charger_switch_, (status.charger_enable & 1) != 0);
uint8_t value;
bool ok = true;
if (this->charger_switch_ != nullptr) {
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
this->charger_switch_->publish_state((value & 1) != 0);
} else {
ok = false;
}
}
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
if (bucks[0] != nullptr || bucks[1] != nullptr) {
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
@@ -461,7 +476,9 @@ void NPM1300Component::dump_config() {
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
}
}
if (config.thermistor != NPM1300_UNSET) {
if (config.thermistor == 0) {
ESP_LOGCONFIG(TAG, " Thermistor: none");
} else if (config.thermistor != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
}
#ifdef USE_SENSOR
+13 -5
View File
@@ -54,10 +54,19 @@ struct NPM1300Config {
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
NPM1300ChargerConfig charger; // only applied when charger_configured
uint16_t thermistor_beta;
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
uint8_t thermistor; // ADCNTCRSEL code: 0 for none, 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
bool charger_configured;
};
/// The status registers read on every update
struct NPM1300Status {
uint8_t vbus; // VBUSINSTATUS
uint8_t charger_enable; // BCHGENABLESET
uint8_t ntc; // NTCSTATUS
uint8_t charge; // BCHGCHARGESTATUS
uint8_t error_reason; // BCHGERRREASON
};
#ifdef USE_NPM1300_DATA_PUBLISHER
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
@@ -68,8 +77,7 @@ float ntc_temperature(uint16_t code, float inv_beta);
#endif
#ifdef USE_TEXT_SENSOR
/// Publish the charger status text, only when it differs from `last`
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason);
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status);
#endif
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
@@ -95,7 +103,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
#ifdef USE_NPM1300_STATUS_PUBLISHER
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
HUB_PUBLISHER(status, const NPM1300Status &)
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
HUB_PUBLISHER(data, const uint8_t *, float)
@@ -128,7 +136,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
#endif
#ifdef USE_SWITCH
/// Returns false when a status read failed
bool publish_switches_();
bool publish_switches_(const NPM1300Status &status);
#endif
const NPM1300Config *config_;
+6 -5
View File
@@ -21,7 +21,6 @@ from esphome.types import ConfigType
from . import (
ADC,
CONF_NPM1300_ID,
CONF_THERMISTOR_BETA,
CONF_THERMISTOR_RESISTANCE,
CONF_VBUS_CURRENT_LIMIT,
DATA_PUBLISHER,
@@ -30,7 +29,9 @@ from . import (
DUMP_TAG,
IBAT_SCALE,
NPM1300_CHILD_SCHEMA,
THERMISTOR_NONE,
npm1300_ns,
thermistor_beta,
)
DEPENDENCIES = ["npm1300"]
@@ -109,10 +110,10 @@ def _final_validate(config: ConfigType) -> None:
if CONF_BATTERY_TEMPERATURE not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
if CONF_THERMISTOR_RESISTANCE not in hub:
if hub.get(CONF_THERMISTOR_RESISTANCE, THERMISTOR_NONE) == THERMISTOR_NONE:
raise cv.Invalid(
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
"set on the npm1300 component",
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
"nominal resistance of the battery thermistor on the npm1300 component",
path=[CONF_BATTERY_TEMPERATURE],
)
@@ -145,7 +146,7 @@ async def to_code(config: ConfigType) -> None:
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
state = npm1300_ns.ntc_temperature(
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
_adc(2, 5, 2), 1.0 / thermistor_beta(hub_config)
)
_add_adc_sensor(hub, var, state, "Battery Temperature")
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
+12 -4
View File
@@ -15,6 +15,7 @@ from . import (
CONF_THERMISTOR_RESISTANCE,
NPM1300_CHILD_SCHEMA,
OUTPUTS,
THERMISTOR_NONE,
)
DEPENDENCIES = ["npm1300"]
@@ -44,11 +45,18 @@ def _final_validate(config: ConfigType) -> None:
if CONF_CHARGER not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
# Only the charger block turns off the NTC check, without which the charger never starts
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
# A wrong thermistor shifts the battery temperature the charger sees
if CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
f"The '{CONF_CHARGER}' switch needs '{CONF_THERMISTOR_RESISTANCE}' on the npm1300 "
f"component: the nominal resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
path=[CONF_CHARGER],
)
# Only the charger block turns off the NTC check, without which the charger never starts
if CONF_CHARGER not in hub and hub[CONF_THERMISTOR_RESISTANCE] == THERMISTOR_NONE:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block when "
f"'{CONF_THERMISTOR_RESISTANCE}' is '{THERMISTOR_NONE}'",
path=[CONF_CHARGER],
)
+2 -6
View File
@@ -11,14 +11,12 @@ from esphome.cpp_generator import (
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS,
STATUS_PUBLISHER,
VBUS_STATUS,
npm1300_ns,
)
@@ -48,9 +46,7 @@ async def to_code(config: ConfigType) -> None:
)
STATUS_PUBLISHER.add(
hub,
npm1300_ns.publish_charger_status(
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
),
npm1300_ns.publish_charger_status(parent_ref(var), last, STATUS),
)
DUMP.add(
hub,
+73 -11
View File
@@ -1,3 +1,6 @@
from dataclasses import dataclass, field
import functools
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -26,7 +29,7 @@ from esphome.core.entity_helpers import (
)
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArguments
from esphome.cpp_types import global_ns
from esphome.types import ConfigType
from esphome.types import ConfigType, TemplateArgsType
CODEOWNERS = ["@esphome/core"]
DOMAIN = "select"
@@ -47,6 +50,25 @@ SELECT_OPERATION_OPTIONS = SelectOperation.options(
)
@dataclass
class SelectData:
# Options of each select whose option list never changes at run time, by id
fixed_options: dict[str, list[str]] = field(default_factory=dict)
def _get_data() -> SelectData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = SelectData()
return CORE.data[DOMAIN]
def _record_options(
config: ConfigType, options: list[str], fixed_options: bool
) -> None:
if fixed_options:
_get_data().fixed_options[config[CONF_ID].id] = options
_SELECT_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(entity_helpers.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
@@ -105,7 +127,11 @@ async def setup_select_core_(var, config, *, options: list[str]):
await entity_helpers.setup_entity_integrations(var, config)
async def register_select(var, config, *, options: list[str]):
async def register_select(
var, config, *, options: list[str], fixed_options: bool = False
):
"""``fixed_options=True`` when the options never change at run time, so ``select.set`` can use the index."""
_record_options(config, options, fixed_options)
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
queue_entity_register("select", config)
@@ -113,9 +139,9 @@ async def register_select(var, config, *, options: list[str]):
await setup_select_core_(var, config, options=options)
async def new_select(config, *args, options: list[str]):
async def new_select(config, *args, options: list[str], fixed_options: bool = False):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_select(var, config, options=options)
await register_select(var, config, options=options, fixed_options=fixed_options)
return var
@@ -131,7 +157,10 @@ def sub_selects(
config: ConfigType, *, parent: MockObj | ID | None = None
) -> SubEntities:
"""Return a SubEntities bound to new_select; ``apply`` statements can use ``entity``, ``index`` and ``option``."""
return SubEntities(new_select, config, parent, APPLY_ENTITY)
# Hub selects get their options from code generation only
return SubEntities(
functools.partial(new_select, fixed_options=True), config, parent, APPLY_ENTITY
)
@coroutine_with_priority(CoroPriority.CORE)
@@ -146,13 +175,20 @@ OPERATION_BASE_SCHEMA = cv.Schema(
)
automation.register_apply_action(
_SELECT_SET_SCHEMA = OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
)
def _option_index(config: ConfigType, option: str) -> str:
return str(_get_data().fixed_options[config[CONF_ID].id].index(option))
_set_option_builder = automation.apply_action_builder(
"select.set",
OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
),
_SELECT_SET_SCHEMA,
automation.ApplyField(
CONF_OPTION,
"set_option",
@@ -161,6 +197,32 @@ automation.register_apply_action(
),
call="make_call",
)
_set_index_builder = automation.apply_action_builder(
"select.set",
_SELECT_SET_SCHEMA,
automation.ApplyField(CONF_OPTION, "set_index", cg.size_t, const_fn=_option_index),
call="make_call",
)
@automation.register_action(
"select.set", automation.ApplyAction, _SELECT_SET_SCHEMA, synchronous=True
)
async def select_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
# A constant fixed option becomes its index
await cg.get_variable(config[CONF_ID])
option = config[CONF_OPTION]
if not cg.is_template(option) and option in _get_data().fixed_options.get(
config[CONF_ID].id, ()
):
return await _set_index_builder(config, action_id, template_arg, args)
return await _set_option_builder(config, action_id, template_arg, args)
automation.register_apply_action(
"select.set_index",
@@ -113,7 +113,7 @@ async def to_code(config):
milliseconds=SCHEDULER_DONT_RUN
)
await cg.register_component(var, component_config)
await select.register_select(var, config, options=options)
await select.register_select(var, config, options=options, fixed_options=True)
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@@ -36,13 +36,8 @@ void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref,
}
void update_lambda(BaseTemplateSelect *sel_comp, const optional<std::string> &val) {
if (val.has_value()) {
if (!sel_comp->has_option(*val)) {
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
return;
}
if (val.has_value())
sel_comp->publish_state(*val);
}
}
} // namespace esphome::template_
+1 -1
View File
@@ -115,7 +115,7 @@ async def to_code(config):
options = list(options_map.values())
mappings = list(options_map.keys())
var = await select.new_select(config, options=options)
var = await select.new_select(config, options=options, fixed_options=True)
if mappings:
cg.add(var.set_select_mappings(mappings))
hub = await cg.add_variable(var.set_tuya_parent, config[CONF_TUYA_ID])
+17 -1
View File
@@ -614,9 +614,25 @@ async def to_code(config: ConfigType) -> None:
def FILTER_SOURCE_FILES() -> list[str]:
"""Filter out web_server_v1.cpp when version is not 1."""
"""Filter out web_server_v1.cpp when version is not 1, the ESP32 event stream elsewhere, and the server on host."""
files_to_filter: list[str] = []
# The host has no web server; it builds only sse_chunk.cpp, for its unit tests
if CORE.is_host:
files_to_filter.extend(
(
"event_source.cpp",
"list_entities.cpp",
"web_server.cpp",
"web_server_v1.cpp",
)
)
return files_to_filter
# The /events implementation is ESP32 only
if not CORE.is_esp32:
files_to_filter.extend(("event_source.cpp", "sse_chunk.cpp"))
# web_server_v1.cpp is only needed when version is 1
config = CORE.config.get(DOMAIN, {})
if config.get(CONF_VERSION, 2) != 1:
@@ -0,0 +1,491 @@
#include "event_source.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include <algorithm>
#include <cstring>
#include <iterator>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "web_server.h"
namespace esphome::web_server {
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
}
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
if (ses->safe_to_delete()) {
// destroy() already logged the close with the fd; don't double-log here.
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
if (!rsp->is_attached()) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->start_session_main_loop_();
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->deferrable_send_state(source, detail_all, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server,
WebServer *ws)
: server_(server), web_server_(ws), entities_iterator_(ws, this) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
}
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
web_server_base::STREAM_HEAD_MAX_IOV,
"the event stream head must go out in one write");
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
this->close();
return;
}
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root["name"] = json::linked(group.second.name);
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
break;
}
}
#endif
this->entities_iterator_.begin(ws->include_internal_);
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
// helper for allowing only unique entries in the queue
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void AsyncEventSourceResponse::process_deferred_queue_() {
if (this->is_closing()) {
return;
}
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
if (this->send_json_(de.source_, de.message_generator_)) {
if (this->is_closing() || deferred_queue_.empty()) {
return;
}
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
} else {
break;
}
}
}
void AsyncEventSourceResponse::release_buffers_() {
std::vector<DeferredEvent>().swap(this->deferred_queue_);
this->tail_.reset();
this->tail_cap_ = 0;
this->tail_len_ = 0;
this->tail_sent_ = 0;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->is_closing() || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
const ssize_t sent = this->write(this->tail_.get() + this->tail_sent_, remaining);
if (sent <= 0) {
return; // write() owns the stall clock and the close
}
this->no_memory_logged_ = false;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->close();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
AsyncStreamResponse::loop();
if (this->is_closing()) {
if (this->tail_cap_ != 0 || this->deferred_queue_.capacity() != 0) {
this->release_buffers_();
}
return;
}
drain_tail_();
process_deferred_queue_();
if (this->is_closing())
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->stall_.clear();
this->no_memory_logged_ = false;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever. A refused write may
// already have started it, so the warning keeps its own flag.
if (!this->no_memory_logged_) {
this->no_memory_logged_ = true;
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
}
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.is_running()) {
this->stall_.start(now);
return;
}
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
this->close();
}
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || !this->is_connected() || this->is_closing()) {
return false;
}
drain_tail_();
return !this->is_closing() && this->tail_len_ == 0;
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
web_server_base::StreamIovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (sent > 0) {
this->no_memory_logged_ = false;
}
if (static_cast<size_t>(sent) == g.total) {
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->is_closing()) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->is_closing()) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -0,0 +1,176 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/stream_response.h"
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
#include "list_entities.h"
#include "sse_chunk.h"
#include <atomic>
#include <cstdint>
#include <vector>
namespace esphome::web_server {
class WebServer;
class AsyncEventSource;
class AsyncEventSourceResponse;
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class AsyncEventSourceResponse;
protected:
void *source_;
message_generator_t *message_generator_;
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
friend class AsyncEventSource;
public:
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
void loop();
protected:
AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server, WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Push what is left of the chunk in tail_ to the socket
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
bool reserve_tail_(size_t len);
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
JSON_PREPARE_RESULT_BUFFER,
JSON_PREPARE_RESULT_TAIL,
JSON_PREPARE_RESULT_NO_MEMORY,
JSON_PREPARE_RESULT_TOO_LARGE,
};
// Keep the arena out of the caller's frame so it is gone before the state send.
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
// unused for TAIL.
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
size_t *size_out);
// Warn once per stall, and close the session once the stall timeout passes with no memory for the tail
void tail_alloc_failed_(size_t cap);
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
struct SendGuard {
AsyncEventSourceResponse &owner;
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
~SendGuard() { this->owner.sending_ = false; }
};
// Frees the tail and the queue's storage once the session is closing
void release_buffers_();
AsyncEventSource *server_;
std::vector<DeferredEvent> deferred_queue_;
WebServer *web_server_;
ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
bool sending_{false};
// The no-memory warning went out during the current stall; cleared on send progress
bool no_memory_logged_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
};
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
public:
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
};
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -8,14 +8,8 @@
namespace esphome::web_server {
#ifdef USE_ESP32
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {}
#elif USE_ARDUINO
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es)
: web_server_(ws), events_(es) {}
#endif
void ListEntitiesIterator::send_all_(void *source, message_generator_t *generator) {
this->events_->deferrable_send_state(source, true, generator);
this->session_->deferrable_send_state(source, true, generator);
}
#ifdef USE_BINARY_SENSOR
+9 -18
View File
@@ -1,33 +1,28 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_WEBSERVER
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#include "esphome/core/component.h"
#include "esphome/core/component_iterator.h"
namespace esphome::json {
class JsonBuilder;
} // namespace esphome::json
namespace esphome::web_server_idf {
#ifdef USE_ESP32
class AsyncEventSource;
#endif
} // namespace esphome::web_server_idf
namespace esphome::web_server {
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
// The /events session the iterator lists entities to
#ifdef USE_ESP32
class AsyncEventSourceResponse;
using EventSession = AsyncEventSourceResponse;
#elif defined(USE_ARDUINO)
class DeferredUpdateEventSource;
using EventSession = DeferredUpdateEventSource;
#endif
class WebServer;
class ListEntitiesIterator final : public ComponentIterator {
public:
#ifdef USE_ESP32
ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es);
#elif defined(USE_ARDUINO)
ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es);
#endif
ListEntitiesIterator(const WebServer *ws, EventSession *session) : web_server_(ws), session_(session) {}
// Entity overrides (generated from entity_types.h).
// Implementations live in list_entities.cpp.
@@ -45,11 +40,7 @@ class ListEntitiesIterator final : public ComponentIterator {
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
const WebServer *web_server_;
#ifdef USE_ESP32
esphome::web_server_idf::AsyncEventSource *events_;
#elif USE_ARDUINO
DeferredUpdateEventSource *events_;
#endif
EventSession *session_;
};
} // namespace esphome::web_server
@@ -5,7 +5,7 @@
#include "esphome/core/helpers.h"
namespace esphome::web_server_idf {
namespace esphome::web_server {
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
if (message == nullptr) {
@@ -63,4 +63,4 @@ void write_chunk_header(char *buf, size_t chunk_len) {
buf[9] = '\n';
}
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
@@ -3,7 +3,9 @@
#include <cstddef>
#include <cstdint>
namespace esphome::web_server_idf {
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server {
// Wire framing of one Server-Sent-Events event inside the chunked /events response:
// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
@@ -22,6 +24,17 @@ constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
// Status, Content-Type and headers of the /events response head
constexpr char SSE_STATUS[] = "200 OK";
constexpr char SSE_CONTENT_TYPE[] = "text/event-stream";
constexpr web_server_base::HttpHeader SSE_HEADERS[] = {
{"Transfer-Encoding", "chunked"},
{"Cache-Control", "no-cache"},
{"Connection", "keep-alive"},
};
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
constexpr char SSE_FIRST_CHUNK[] = "2\r\n\r\n\r\n";
// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
@@ -37,4 +50,4 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
// Fills the header placeholder at the start of buf once the chunk length is known
void write_chunk_header(char *buf, size_t chunk_len);
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
+1 -1
View File
@@ -273,7 +273,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
deferred_queue_.erase(deferred_queue_.begin());
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
} else {
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::drain_tail_().
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
// send progress. Keep that stall policy in sync when changing either side.
+5 -1
View File
@@ -5,7 +5,11 @@
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/components/web_server_base/captive_dns.h"
#ifdef USE_WEBSERVER
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#ifdef USE_ESP32
#include "event_source.h"
#endif
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/entity_includes.h"
@@ -105,9 +105,9 @@ async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
cg.add(cg.RawExpression(f"{web_server_base_ns}::global_web_server_base = {var}"))
# Default headers added in init(): web_server_idf's StaticVector, or the library's DefaultHeaders
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
if CORE.is_esp32:
# Count for StaticVector in web_server_idf - matches headers added in init()
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
return
# ESP32 uses IDF web server (early return above), so this is for other Arduino platforms
@@ -0,0 +1,126 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <strings.h>
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
namespace esphome::web_server_base {
struct HttpHeader {
const char *name;
const char *value;
};
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
/// element it stopped in. count reaches 0 once everything went out.
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
while (count > 0 && sent >= iov->iov_len) {
sent -= iov->iov_len;
++iov;
--count;
}
if (count > 0) {
iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
iov->iov_len -= sent;
}
}
/// Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body
constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
/// Pieces per batch: a head with up to four headers goes out in one write; 192 bytes on 32-bit
static constexpr int STREAM_HEAD_MAX_IOV = 24;
/// True if headers holds a header named name, compared case insensitively
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
for (const auto &header : headers) {
if (strcasecmp(header.name, name) == 0) {
return true;
}
}
return false;
}
/// Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers, the default_headers that
/// headers does not replace, the blank line and body (may be null). send(Iovec *iov, int count) gets each full batch of
/// STREAM_HEAD_MAX_IOV pieces and then the rest; once it returns false the later pieces are skipped. Returns false if
/// any send failed.
template<typename Iovec, typename Send>
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
std::span<const HttpHeader> default_headers, const char *body) {
struct Batch {
Send &send;
int count{0};
bool ok{true};
Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
void flush() {
if (this->ok && this->count > 0) {
this->ok = this->send(this->iov, this->count);
}
this->count = 0;
}
void add(const char *buf, size_t len) {
if (this->count == STREAM_HEAD_MAX_IOV) {
this->flush();
}
this->iov[this->count++] = {const_cast<char *>(buf), len};
}
void add(const char *str) { this->add(str, strlen(str)); }
void add_header(const HttpHeader &header) {
this->add("\r\n", 2);
this->add(header.name);
this->add(": ", 2);
this->add(header.value);
}
} batch{send};
batch.add("HTTP/1.1 ", 9);
batch.add(status);
batch.add("\r\nContent-Type: ", 16);
batch.add(content_type);
for (const auto &header : headers) {
batch.add_header(header);
}
for (const auto &header : default_headers) {
if (!has_header(headers, header.name)) {
batch.add_header(header);
}
}
batch.add("\r\n\r\n", 4);
if (body != nullptr) {
batch.add(body);
}
batch.flush();
return batch.ok;
}
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
/// The comparison is wraparound safe across a millis() rollover.
class StallClock {
public:
/// Starts the clock; a running clock keeps its start time.
void start(uint32_t now) {
if (this->started_ms_ == 0) {
this->started_ms_ = now != 0 ? now : 1;
}
}
void clear() { this->started_ms_ = 0; }
bool is_running() const { return this->started_ms_ != 0; }
/// True once the clock has run for timeout_ms
bool is_expired(uint32_t now, uint32_t timeout_ms) const {
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
}
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
protected:
uint32_t started_ms_{0};
};
} // namespace esphome::web_server_base
@@ -0,0 +1,16 @@
#pragma once
#include "esphome/core/defines.h"
// The long lived stream response the /events layer writes to, for the platform's web server
#ifdef USE_ESP32
#include "esphome/components/web_server_idf/web_server_idf.h"
#include <sys/socket.h>
namespace esphome::web_server_base {
using web_server_idf::AsyncStreamResponse;
// One entry of the gather list AsyncStreamResponse::write() takes
using StreamIovec = ::iovec;
} // namespace esphome::web_server_base
#endif
@@ -17,7 +17,6 @@
#include <freertos/task.h>
#include "utils.h"
#include "sse_chunk.h"
#include "web_server_idf.h"
#ifdef USE_WEBSERVER_AUTH_DIGEST
@@ -30,17 +29,15 @@
#include "multipart.h" // For parse_multipart_boundary and other utils
#endif
#ifdef USE_WEBSERVER
#include "esphome/components/web_server/web_server.h"
#include "esphome/components/web_server/list_entities.h"
#endif // USE_WEBSERVER
// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
#include <cerrno>
#include <sys/socket.h>
namespace esphome::web_server_idf {
using web_server_base::advance_iovecs;
using web_server_base::write_stream_head;
// Status strings not provided by esp_http_server.h
#ifndef HTTPD_401
#define HTTPD_401 "401 Unauthorized"
@@ -55,13 +52,10 @@ namespace esphome::web_server_idf {
#define HTTPD_422 "422 Unprocessable Entity"
#endif
#define CRLF_STR "\r\n"
#define CRLF_LEN (sizeof(CRLF_STR) - 1)
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
// Every frame in this file is capped below one JSON arena.
// Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
@@ -140,6 +134,8 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
//
// See: https://github.com/esphome/esphome-webserver/issues/163
AsyncStreamResponse::socket_closing(hd, sockfd);
// Attempt shutdown - ignore errors as socket may already be closed
shutdown(sockfd, SHUT_RD);
@@ -736,224 +732,84 @@ void AsyncResponseStream::printf(const char *fmt, ...) {
this->print(str);
}
#ifdef USE_WEBSERVER
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
}
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
if (ses->safe_to_delete_()) {
// destroy() already logged the close with the fd; don't double-log here.
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete_()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
if (httpd_sess_get_ctx(rsp->hd_, rsp->fd_.load()) != rsp) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->start_session_main_loop_();
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
if (ses->fd_.load() != 0) { // Skip dead sessions
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
if (ses->fd_.load() != 0) { // Skip dead sessions
ses->deferrable_send_state(source, detail_all, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request,
esphome::web_server_idf::AsyncEventSource *server,
esphome::web_server::WebServer *ws)
: server_(server), web_server_(ws), entities_iterator_(ws, server) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
void AsyncStreamResponse::begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body) {
httpd_req_t *req = *request;
httpd_resp_set_status(req, HTTPD_200);
httpd_resp_set_type(req, "text/event-stream");
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
httpd_resp_set_hdr(req, "Connection", "keep-alive");
DefaultHeaders::Instance().apply_(req);
httpd_resp_send_chunk(req, CRLF_STR, CRLF_LEN);
// Same head httpd_resp_send_chunk() writes, without its first chunk. Blocking sends: the session's non-blocking
// send override is not installed yet.
const int fd = httpd_req_to_sockfd(req);
const auto &defaults = DefaultHeaders::Instance().headers_;
const bool head_sent = write_stream_head<struct iovec>(
[fd](struct iovec *iov, int count) {
while (count > 0) {
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = count;
const ssize_t sent = sendmsg(fd, &msg, 0);
if (sent <= 0) {
return false;
}
advance_iovecs(iov, count, sent);
}
return true;
},
status, content_type, headers, std::span<const HttpHeader>(defaults.data(), defaults.size()), body);
req->sess_ctx = this;
req->free_ctx = AsyncEventSourceResponse::destroy;
req->free_ctx = AsyncStreamResponse::destroy;
this->hd_ = req->handle;
this->fd_.store(httpd_req_to_sockfd(req));
this->fd_.store(fd);
// Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
httpd_sess_set_send_override(this->hd_, this->fd_.load(), nonblocking_send);
httpd_sess_set_send_override(this->hd_, fd, nonblocking_send);
if (!head_sent) {
// A partly sent head must not be continued by later writes. close() is main loop only, so shut the socket
// down here; the first write() then fails and closes the stream, and httpd frees the session as usual.
shutdown(fd, SHUT_RDWR);
}
}
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// Held around a stream's ownership check and send, and while its socket is marked closed.
// Created at boot, so no allocation can fail once requests arrive.
static Mutex stream_send_mutex; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->fd_.load());
this->request_close_();
void AsyncStreamResponse::socket_closing(httpd_handle_t hd, int fd) {
// Only streams set a session context
auto *rsp = static_cast<AsyncStreamResponse *>(httpd_sess_get_ctx(hd, fd));
if (rsp == nullptr)
return;
}
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root["name"] = json::linked(group.second.name);
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->fd_.load());
break;
}
}
#endif
this->entities_iterator_.begin(ws->include_internal_);
LockGuard guard{stream_send_mutex};
rsp->socket_closed_ = true;
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSourceResponse::destroy(void *ptr) {
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
void AsyncStreamResponse::destroy(void *ptr) {
auto *rsp = static_cast<AsyncStreamResponse *>(ptr);
int fd = rsp->fd_.exchange(0); // Atomically get and clear fd
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
// Mark as dead - will be cleaned up in the main loop
// Note: We don't delete or remove from set here to avoid race conditions
ESP_LOGD(TAG, "Stream connection closed (fd: %d)", fd);
// Mark as dead - the owner deletes it from the main loop once safe_to_delete().
// httpd will call our custom close_fn (safe_close_with_shutdown) which handles
// shutdown() before close() to prevent lwIP race conditions
}
// helper for allowing only unique entries in the queue
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void AsyncEventSourceResponse::process_deferred_queue_() {
if (this->close_requested_) {
return;
}
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
if (this->send_json_(de.source_, de.message_generator_)) {
if (this->close_requested_ || deferred_queue_.empty()) {
return;
}
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
} else {
break;
}
}
}
void AsyncEventSourceResponse::request_close_() {
void AsyncStreamResponse::close() {
if (!this->close_requested_) {
this->close_requested_ = true;
this->deferred_queue_.clear();
this->tail_.reset();
this->tail_cap_ = 0;
this->tail_len_ = 0;
this->tail_sent_ = 0;
this->next_close_attempt_ms_ = App.get_loop_component_start_time();
}
this->process_close_();
}
void AsyncEventSourceResponse::process_close_() {
void AsyncStreamResponse::loop() {
if (this->close_requested_) {
this->process_close_();
} else {
this->refresh_lru_();
}
}
void AsyncStreamResponse::process_close_() {
if (!this->close_requested_ || this->close_work_queued_.load(std::memory_order_acquire)) {
return;
}
@@ -971,7 +827,7 @@ void AsyncEventSourceResponse::process_close_() {
// httpd_sess_trigger_close() queues only a reusable fd/session slot and can
// therefore close a new client if the original peer disconnects meanwhile.
this->close_work_queued_.store(true, std::memory_order_release);
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncEventSourceResponse::close_session_work, this);
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncStreamResponse::close_session_work, this);
this->next_close_attempt_ms_ = now + (err == ESP_OK ? CLOSE_CONFIRM_INTERVAL_MS : CLOSE_RETRY_INTERVAL_MS);
if (err == ESP_OK) {
return;
@@ -979,17 +835,17 @@ void AsyncEventSourceResponse::process_close_() {
this->close_work_queued_.store(false, std::memory_order_release);
if (!this->close_retry_warning_logged_) {
ESP_LOGW(TAG, "Failed to queue EventSource close (%s); retrying", esp_err_to_name(err));
ESP_LOGW(TAG, "Failed to queue stream close (%s); retrying", esp_err_to_name(err));
this->close_retry_warning_logged_ = true;
}
}
void AsyncEventSourceResponse::refresh_lru_() {
// httpd only advances a session's LRU counter when a request arrives, and an event stream receives one
void AsyncStreamResponse::refresh_lru_() {
// httpd only advances a session's LRU counter when a request arrives, and a stream receives one
// request ever, so the purge that frees a socket for a new client would evict the live streams first.
// The HTTPD task owns the session table, so the refresh runs there.
// A stalled stream is the socket the purge should take, and a zeroed fd has no session left to refresh.
if (this->send_failure_started_ms_ != 0 || this->fd_.load() == 0)
if (this->stall_.is_running() || this->fd_.load() == 0)
return;
const uint32_t now = App.get_loop_component_start_time();
if (now - this->last_lru_refresh_ms_ < LRU_REFRESH_INTERVAL_MS ||
@@ -998,13 +854,13 @@ void AsyncEventSourceResponse::refresh_lru_() {
}
this->last_lru_refresh_ms_ = now; // a full queue also waits for the next interval
this->lru_work_queued_.store(true, std::memory_order_release);
if (httpd_queue_work(this->hd_, &AsyncEventSourceResponse::refresh_lru_work, this) != ESP_OK) {
if (httpd_queue_work(this->hd_, &AsyncStreamResponse::refresh_lru_work, this) != ESP_OK) {
this->lru_work_queued_.store(false, std::memory_order_release);
}
}
void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
void AsyncStreamResponse::refresh_lru_work(void *arg) {
auto *response = static_cast<AsyncStreamResponse *>(arg);
const int fd = response->fd_.load();
if (response->owns_session_(fd)) {
httpd_sess_update_lru_counter(response->hd_, fd);
@@ -1013,8 +869,8 @@ void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
response->lru_work_queued_.store(false, std::memory_order_release);
}
void AsyncEventSourceResponse::close_session_work(void *arg) {
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
void AsyncStreamResponse::close_session_work(void *arg) {
auto *response = static_cast<AsyncStreamResponse *>(arg);
const int fd = response->fd_.load();
if (response->owns_session_(fd)) {
// The HTTPD task remains the session owner. Shutting the socket down makes
@@ -1026,320 +882,61 @@ void AsyncEventSourceResponse::close_session_work(void *arg) {
response->close_work_queued_.store(false, std::memory_order_release);
}
ssize_t AsyncEventSourceResponse::send_(struct iovec *iov, int iovcnt) {
// httpd frees a session before closing its socket, so the fd may already be a new client's.
ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
if (this->close_requested_) {
return -1;
}
// Nothing to send is not a stall
size_t total = 0;
for (int i = 0; i < iovcnt; i++) {
total += iov[i].iov_len;
}
if (total == 0) {
return 0;
}
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
// Treated as would-block; the stall timer ends a session that never becomes ours again.
const int fd = this->fd_.load();
if (!this->owns_session_(fd)) {
return 0;
ssize_t sent = 0;
int err = 0;
{
// Held across the check and the send, so socket_closing() cannot close the socket in between
LockGuard guard{stream_send_mutex};
if (!this->socket_closed_ && this->owns_session_(fd)) {
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
sent = sendmsg(fd, &msg, MSG_DONTWAIT);
err = errno;
}
}
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
const ssize_t sent = sendmsg(fd, &msg, MSG_DONTWAIT);
if (sent >= 0) {
if (sent > 0) {
this->stall_.clear();
return sent;
}
const int err = errno;
if (err == EAGAIN || err == EWOULDBLOCK) {
return 0;
if (sent < 0 && err != EAGAIN && err != EWOULDBLOCK) {
ESP_LOGD(TAG, "send error: errno %d", err);
// A hard error is not a timeout, so is_stalled() must not report one
this->stall_.clear();
this->close();
return -1;
}
ESP_LOGD(TAG, "send error: errno %d", err);
this->request_close_();
return -1;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->close_requested_ || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
struct iovec iov = {this->tail_.get() + this->tail_sent_, remaining};
const ssize_t sent = this->send_(&iov, 1);
if (sent < 0) {
return;
}
if (sent == 0) {
// Socket buffer full, try again later
// NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_().
// The IDF path is intentionally time-based and closes through HTTPD to preserve session ownership.
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu32 " ms without send progress",
now - this->send_failure_started_ms_);
this->request_close_();
}
return;
}
this->send_failure_started_ms_ = 0;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * web_server::LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->request_close_();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
if (this->close_requested_) {
this->process_close_();
return;
}
this->refresh_lru_();
drain_tail_();
process_deferred_queue_();
if (this->close_requested_)
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->send_failure_started_ms_ = 0;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
return;
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", now - this->send_failure_started_ms_);
this->request_close_();
this->stall_.start(now);
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
this->close();
return -1;
}
return 0;
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || this->fd_.load() == 0 || this->close_requested_) {
return false;
}
drain_tail_();
return !this->close_requested_ && this->tail_len_ == 0;
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
struct iovec iov = {const_cast<void *>(data), len};
return this->write(&iov, 1);
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
struct iovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->send_(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (static_cast<size_t>(sent) == g.total) {
this->send_failure_started_ms_ = 0; // progress, whichever stall clock was running
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->close_requested_) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->close_requested_) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
#endif
#ifdef USE_WEBSERVER_OTA
esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
// Parse boundary and create reader
@@ -15,21 +15,14 @@
#include <utility>
#include <vector>
#ifdef USE_WEBSERVER
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server/list_entities.h"
#include "sse_chunk.h"
#endif
#include "esphome/components/web_server_base/stream_head.h"
struct iovec; // NOLINT(readability-identifier-naming) - forward decl of lwip's gather list entry
namespace esphome {
#ifdef USE_WEBSERVER
namespace web_server {
class WebServer;
}; // namespace web_server
#endif
namespace web_server_idf {
namespace esphome::web_server_idf {
using web_server_base::HttpHeader;
using web_server_base::StallClock;
class AsyncWebParameter {
public:
@@ -191,6 +184,7 @@ class AsyncWebServerRequest {
void onDisconnect(std::function<void()> fn) { this->on_disconnect_ = std::move(fn); }
#endif
/// sess_ctx is reserved for AsyncStreamResponse: the server's close_fn treats any session context as one.
operator httpd_req_t *() const { return this->req_; }
optional<std::string> get_header(const char *name) const;
/// Copy a header value into buf without heap allocation.
@@ -269,204 +263,85 @@ class AsyncWebHandler {
virtual bool isRequestHandlerTrivial() const { return true; }
};
#ifdef USE_WEBSERVER
class AsyncEventSource;
class AsyncEventSourceResponse;
using message_generator_t = void(esphome::web_server::WebServer *, void *, json::JsonBuilder &);
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class AsyncEventSourceResponse;
protected:
void *source_;
message_generator_t *message_generator_;
/// A response that keeps its connection open and is written from the main loop, such as an event stream or
/// an audio stream. Use it directly or derive from it. Call loop() and delete the object only through the type
/// it was created as: subclasses hide loop() instead of overriding it, and the destructor is not virtual.
///
/// Lifecycle:
/// 1. Httpd task, inside handleRequest(): create it and call begin(), then hand the pointer to the main loop.
/// 2. Main loop: once is_attached() is true, write() may go through. Call loop() on every pass.
/// 3. Main loop: delete it once safe_to_delete() is true. httpd owns the socket throughout.
class AsyncStreamResponse {
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
/// Httpd task, once. Sends the status line (for example HTTPD_200), Content-Type, headers, the default headers
/// and body (a C string or null, sent as is), then makes this object the session context. A chunked response
/// passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body = nullptr);
class AsyncEventSourceResponse {
friend class AsyncEventSource;
public:
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Main loop. Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller
/// keeps the rest and continues from there. 0 means the socket is full or not ours yet; -1 means the stream failed
/// and is closing. Refused writes for SEND_STALL_TIMEOUT_MS in a row also close it.
ssize_t write(struct iovec *iov, int iovcnt);
ssize_t write(const void *data, size_t len);
/// Main loop. Shuts the connection down through httpd; safe to call more than once.
void close();
/// Main loop, every pass. Keeps the session fresh in httpd's LRU and retries a pending close.
void loop();
/// Httpd task, from the server's close_fn: no write() reaches the socket after this returns.
static void socket_closing(httpd_handle_t hd, int fd);
protected:
AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server,
esphome::web_server::WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Non-blocking gather write. Returns bytes written, 0 on would-block or a socket that is not
// ours, -1 after requesting the close on any other error.
ssize_t send_(struct iovec *iov, int iovcnt);
// Push what is left of the chunk in tail_ to the socket; owns the stall timer.
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
bool reserve_tail_(size_t len);
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
JSON_PREPARE_RESULT_BUFFER,
JSON_PREPARE_RESULT_TAIL,
JSON_PREPARE_RESULT_NO_MEMORY,
JSON_PREPARE_RESULT_TOO_LARGE,
};
// Keep the arena out of the caller's frame so it is gone before the state send.
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
// unused for TAIL.
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
size_t *size_out);
// Warn once, and close the session once the stall timeout passes with no memory for the tail
void tail_alloc_failed_(size_t cap);
void request_close_();
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
struct SendGuard {
AsyncEventSourceResponse &owner;
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
~SendGuard() { this->owner.sending_ = false; }
};
void process_close_();
static void close_session_work(void *arg);
void refresh_lru_();
static void refresh_lru_work(void *arg);
// httpd frees a session before closing its socket, so the fd may already belong to a new client
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
// Deletable only after destroy() zeroed fd_ and no queued HTTPD work still references this object.
bool safe_to_delete_() const {
/// httpd holds this response as the session context; begin() has sent the whole head by then.
bool is_attached() const { return this->owns_session_(this->fd_.load()); }
/// The client is still connected; httpd has not freed the session.
bool is_connected() const { return this->fd_.load() != 0; }
bool is_closing() const { return this->close_requested_; }
/// The socket fd, for log lines only
int log_id() const { return this->fd_.load(); }
/// Sends are making no progress; after write() returns -1 this tells a send timeout from a send error.
bool is_stalled() const { return this->stall_.is_running(); }
/// The session is gone and no queued httpd work still references this object.
bool safe_to_delete() const {
return this->fd_.load() == 0 && !this->close_work_queued_.load(std::memory_order_acquire) &&
!this->lru_work_queued_.load(std::memory_order_acquire);
}
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
protected:
void process_close_();
static void close_session_work(void *arg);
void refresh_lru_();
static void refresh_lru_work(void *arg);
// httpd closes the socket before it clears the session, so this can still match a closed fd; write() also checks
// socket_closed_
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
static void destroy(void *p);
AsyncEventSource *server_;
httpd_handle_t hd_{};
std::atomic<int> fd_{};
std::vector<DeferredEvent> deferred_queue_;
esphome::web_server::WebServer *web_server_;
esphome::web_server::ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint32_t send_failure_started_ms_{0}; // Zero means no send stall in progress.
// Runs while sends make no progress; a running clock keeps its start time
StallClock stall_;
uint32_t next_close_attempt_ms_{0};
uint32_t last_lru_refresh_ms_{0};
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
// Set on the main loop before queueing close work, cleared by the HTTPD-task callback when done.
std::atomic<bool> close_work_queued_{false};
std::atomic<bool> lru_work_queued_{false};
// Guarded by the stream send mutex
bool socket_closed_{false};
// Main-loop only; the HTTPD task never reads or writes these flags.
bool close_requested_{false};
bool close_retry_warning_logged_{false};
bool sending_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
static constexpr uint32_t CLOSE_RETRY_INTERVAL_MS = 250;
static constexpr uint32_t CLOSE_CONFIRM_INTERVAL_MS = 1000;
static constexpr uint32_t LRU_REFRESH_INTERVAL_MS = 1000;
};
using AsyncEventSourceClient = AsyncEventSourceResponse;
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
public:
AsyncEventSource(StringRef url, esphome::web_server::WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
esphome::web_server::WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
};
#endif // USE_WEBSERVER
struct HttpHeader {
const char *name;
const char *value;
};
class DefaultHeaders {
friend class AsyncWebServerRequest;
friend class AsyncStreamResponse;
friend class AsyncWebServerResponse;
#ifdef USE_WEBSERVER
friend class AsyncEventSourceResponse;
#endif
static_assert(WEB_SERVER_DEFAULT_HEADERS_COUNT <= 8, "AsyncWebServerResponse::replaced_defaults_ holds 8 bits");
public:
@@ -484,8 +359,7 @@ class DefaultHeaders {
void apply_(httpd_req_t *req, uint8_t skip_mask = 0) const;
};
} // namespace web_server_idf
} // namespace esphome
} // namespace esphome::web_server_idf
using namespace esphome::web_server_idf; // NOLINT(google-global-names-in-headers)
+31 -21
View File
@@ -5,47 +5,57 @@
namespace esphome::wifi {
// Switching APs calls esp_wifi_disconnect(), which queues an association-leave event from the old AP. Remembers
// that AP so its leave event does not end the connection attempt started after it. Platform free for host tests.
/// Which expected leave a disconnect matched.
enum class ExpectedLeave : uint8_t {
EXPECTED_LEAVE_NONE,
EXPECTED_LEAVE_AP,
EXPECTED_LEAVE_ATTEMPT, ///< An attempt that never associated, so no BSSID
};
// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
class ExpectedDisconnect {
public:
void arm(const uint8_t *bssid) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->armed_ = true;
}
void clear() { this->armed_ = false; }
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
/// Disconnects from the current AP and, once that succeeds, expects its leave event. read_bssid(uint8_t *out)
/// returns whether there was an AP and runs first, since the record is gone after the disconnect; disconnect()
/// returns whether it succeeded, which is also the result. Any earlier expectation is dropped.
/// Disconnects and, once that succeeds, expects the leave event. read_bssid(uint8_t *out) runs first, since the
/// record is gone after the disconnect, and writes the AP's BSSID only when it returns true; without an AP the
/// attempt's leave, with no BSSID, is expected. disconnect() returns whether it succeeded, which is the result.
template<typename ReadBssid, typename Disconnect>
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
this->armed_ = false;
this->clear();
std::array<uint8_t, 6> bssid{};
const bool had_ap = read_bssid(bssid.data());
if (!disconnect())
return false;
if (had_ap)
this->arm(bssid.data());
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
return true;
}
/// A connected event still queued from the previous AP; it keeps the expectation.
bool is_previous_ap(const uint8_t *bssid) const {
return this->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
}
/// Whether a disconnect is the previous AP's leave. Every disconnect ends the expectation, so it matches at
/// most once; the caller handles roaming disconnects before this.
bool consume_disconnect(const uint8_t *bssid, bool assoc_leave) {
const bool expected = assoc_leave && this->is_previous_ap(bssid);
this->armed_ = false;
return expected;
/// Which expected leave a disconnect is. \p is_leave covers an AP's association leave (8) and the station leave
/// of an attempt that never associated (36). Every disconnect ends the expectation, so it matches at most once.
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
const ExpectedLeave matched =
is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
this->clear();
return matched;
}
protected:
void arm_(const uint8_t *bssid, ExpectedLeave expected) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->expected_ = expected;
}
std::array<uint8_t, 6> bssid_{};
bool armed_{false};
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
};
} // namespace esphome::wifi
@@ -1158,10 +1158,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
ESP_LOGI(TAG, "Disconnected ssid='%.*s' reason='Station Roaming'", it.ssid_len, (const char *) it.ssid);
return;
}
const bool expected_disconnect =
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
const ExpectedLeave expected = s_expected_disconnect.consume_disconnect(
it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE || it.reason == WIFI_REASON_STA_LEAVING);
const bool expected_disconnect = expected != ExpectedLeave::EXPECTED_LEAVE_NONE;
if (expected_disconnect) {
ESP_LOGD(TAG, "Expected disconnect from previous AP");
ESP_LOGD(TAG, "Expected leave of the previous %s",
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
} else if (it.reason == WIFI_REASON_NO_AP_FOUND) {
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
(const char *) it.ssid);
@@ -1181,7 +1183,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
// Refresh is_connected() cache; the old session is no longer connected.
this->update_connected_state_();
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
this->notify_disconnect_state_listeners_();
// An attempt that never associated ended no session listeners know of
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
@@ -8,18 +8,16 @@ namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
void ZigbeeAttribute::set_attr_() {
void ZigbeeAttribute::set_attr_internal() {
if (!this->zb_->is_started()) {
return;
}
if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
ezb_zcl_status_t state = ezb_zcl_set_attr_value(this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_,
EZB_ZCL_STD_MANUF_CODE, this->value_p_, false);
// cleared before report_() so it can disable the loop
// when the report has to wait for join
this->set_attr_requested_ = false;
if (this->force_report_) {
this->report_(true);
this->report_internal(true);
}
// Check for error
if (state != EZB_ZCL_STATUS_SUCCESS) {
@@ -30,15 +28,12 @@ void ZigbeeAttribute::set_attr_() {
}
}
void ZigbeeAttribute::report_(bool has_lock) {
void ZigbeeAttribute::report_internal(bool has_lock) {
if (!this->report_enabled) {
return;
}
if (!this->zb_->is_joined()) {
this->report_requested_ = true;
if (!this->set_attr_requested_) {
this->disable_loop();
}
return;
}
if (has_lock or esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
@@ -61,43 +56,6 @@ void ZigbeeAttribute::report_(bool has_lock) {
}
}
void ZigbeeAttribute::set_report(ZigbeeReportT report) {
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
return;
}
this->report_enabled = true;
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
this->force_report_ = true;
}
this->zb_->add_on_join_callback([this](bool) {
if (this->report_requested_) {
this->enable_loop();
}
});
}
void ZigbeeAttribute::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeAttribute::loop() {
if (this->set_attr_requested_) {
this->set_attr_();
}
if (this->report_requested_) {
this->report_(false);
}
if (!this->report_requested_ && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
@@ -4,9 +4,8 @@
#include <limits>
#include <type_traits>
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "zigbee_entity_base_esp32.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
@@ -29,34 +28,22 @@
namespace esphome::zigbee {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeAttribute final : public Component {
class ZigbeeAttribute final : public ZigbeeEntityBase {
public:
ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t attr_type, float scale, uint8_t max_size)
: zb_(parent),
endpoint_id_(endpoint_id),
: ZigbeeEntityBase(parent, endpoint_id),
cluster_id_(cluster_id),
role_(role),
attr_id_(attr_id),
attr_type_(attr_type),
max_size_(max_size),
scale_(scale) {}
void loop() override;
template<typename T> void add_attr(T value);
template<typename T> void set_attr(const T &value);
uint8_t attr_type() { return attr_type_; }
void set_report(ZigbeeReportT report);
void report();
template<typename F> void add_on_value_callback(F &&callback) { on_value_callback_.add(std::forward<F>(callback)); }
void on_value(ezb_zcl_attribute_t attribute) {
void on_value(ezb_zcl_attribute_t attribute) override {
if (attribute.data.type == this->attr_type() && attribute.data.value) {
this->on_value_callback_.call(attribute);
}
@@ -75,13 +62,10 @@ class ZigbeeAttribute final : public Component {
#ifdef USE_NUMBER
template<typename T> void connect(number::Number *device);
#endif
bool report_enabled = false;
protected:
void set_attr_();
void report_(bool has_lock);
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
void set_attr_internal() override;
void report_internal(bool has_lock) override;
uint16_t cluster_id_;
uint8_t role_;
uint16_t attr_id_;
@@ -89,9 +73,6 @@ class ZigbeeAttribute final : public Component {
uint8_t max_size_;
float scale_;
void *value_p_{nullptr};
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
LazyCallbackManager<void(ezb_zcl_attribute_t attribute)> on_value_callback_{};
template<typename T> T scale_value_(float value);
template<typename T> T invalid_value_();
@@ -0,0 +1,51 @@
#include "zigbee_entity_base_esp32.h"
#include "esphome/core/log.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.entity_base");
void ZigbeeEntityBase::set_report(ZigbeeReportT report) {
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
return;
}
this->report_enabled = true;
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
this->force_report_ = true;
}
this->zb_->add_on_join_callback([this](bool) {
if (this->report_requested_) {
this->enable_loop();
}
});
}
void ZigbeeEntityBase::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeEntityBase::loop() {
if (this->set_attr_requested_) {
this->set_attr_internal();
}
if (this->report_requested_ && this->zb_->is_joined()) {
this->report_internal(false);
}
if ((!this->report_requested_ || !this->zb_->is_joined()) && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
#endif
@@ -0,0 +1,47 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
#include "esp_zigbee.h"
#include "zigbee_esp32.h"
namespace esphome::zigbee {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeEntityBase : public Component, public ZigbeeAttributeInterface {
public:
void loop() override;
void set_report(ZigbeeReportT report);
void report() override;
bool report_enabled = false;
protected:
ZigbeeEntityBase(ZigbeeComponent *parent, uint8_t endpoint_id) : zb_(parent), endpoint_id_(endpoint_id) {}
/// Handle pending attribute writes and clear set_attr_requested_ when complete; leave it set to retry.
virtual void set_attr_internal() = 0;
/// Handle pending reports and clear report_requested_ when complete; leave it set to retry.
/// Acquire/release the Zigbee lock only when has_lock is false; otherwise the caller owns it.
virtual void report_internal(bool has_lock) = 0;
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
};
} // namespace esphome::zigbee
#endif
#endif
+12 -8
View File
@@ -31,7 +31,11 @@ static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
class ZigbeeAttribute;
class ZigbeeAttributeInterface {
public:
virtual void on_value(ezb_zcl_attribute_t attribute) = 0;
virtual void report() = 0;
};
class ZigbeeComponent final : public Component {
public:
@@ -48,8 +52,8 @@ class ZigbeeComponent final : public Component {
bool register_device();
template<typename T>
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t max_size, T value);
void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value);
template<typename T>
void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
@@ -94,13 +98,13 @@ class ZigbeeComponent final : public Component {
void end_parent_link_failures_(uint8_t seq);
void reset_parent_link_failures_();
template<typename T>
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
T *value_p);
void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p);
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
// attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
// automations
// key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttributeInterface *> attributes_;
ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
CallbackManager<void(bool)> join_cb_{};
template<typename... Args> friend void enqueue_zb_event(Args... args);
@@ -133,7 +137,7 @@ void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t
}
template<typename T>
void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value) {
// The size byte of the zcl_str must be set to the maximum value,
// even though the initial string may be shorter.
@@ -151,7 +155,7 @@ void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint1
}
template<typename T>
void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p) {
ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
if (ep_desc == NULL) {
+5 -6
View File
@@ -1497,12 +1497,11 @@ class BindKeyValidator(SensitiveValidator):
if any(len(part) != 2 for part in parts):
raise Invalid(f"{self._name} must be format XX")
for part in parts:
try:
parts_int.append(int(part, 16))
except ValueError:
raise Invalid(
f"{self._name} must be hex values from 00 to FF"
) from None
# int() also accepts surrounding whitespace and a +/- sign, which
# would silently validate a key different from the one typed
if not re.fullmatch(r"[0-9A-Fa-f]{2}", part):
raise Invalid(f"{self._name} must be hex values from 00 to FF")
parts_int.append(int(part, 16))
return "".join(f"{part:02X}" for part in parts_int)
+3 -1
View File
@@ -846,7 +846,9 @@ def convert_visual_temperatures(
that needs ``full_config`` is left for final validation, which runs this again with it."""
if (unit := entity_temperature_unit(config, full_config)) is None:
return config
visual = config[CONF_VISUAL]
# A platform entry that only holds nested entities has no visual section of its own
if (visual := config.get(CONF_VISUAL)) is None:
return config
convert_temperature_keys(visual, (CONF_MIN_TEMPERATURE, CONF_MAX_TEMPERATURE), unit)
section = visual if delta_section is None else visual.get(delta_section)
if section is not None:
+36
View File
@@ -1472,6 +1472,42 @@ __attribute__((format(printf, 4, 5))) inline size_t buf_append_printf(char *buf,
}
#endif
/// Copy a PROGMEM string to `dst`, stopping at its null terminator or at `end`, and return the new end of `dst`.
/// No terminator is written. Flash only allows aligned 32-bit loads on ESP8266, so `src` is read a whole word at a
/// time, including the bytes after its null in the last word; `src` must be readable up to the end of that word.
inline char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end, const char *src) {
const uint32_t offset = reinterpret_cast<uintptr_t>(src) & 3;
const uint32_t *word = reinterpret_cast<const uint32_t *>(src - offset);
uint32_t bytes = *word++;
// Enters the loop at the first byte of src; after that each word runs its four steps without another dispatch, and
// one step per byte keeps each a single extract
switch (offset) {
for (;;) {
bytes = *word++;
[[fallthrough]];
case 0:
if (dst >= end || static_cast<char>(bytes) == '\0')
return dst;
*dst++ = static_cast<char>(bytes);
[[fallthrough]];
case 1:
if (dst >= end || static_cast<char>(bytes >> 8) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 8);
[[fallthrough]];
case 2:
if (dst >= end || static_cast<char>(bytes >> 16) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 16);
[[fallthrough]];
default:
if (dst >= end || static_cast<char>(bytes >> 24) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 24);
}
}
}
#ifdef USE_ESP8266
/// Safely append a PROGMEM string to buffer, returning new position (capped at size).
/// ESP8266 internal implementation — prefer the `buf_append_str` macro which wraps
+28
View File
@@ -48,6 +48,34 @@ void HOT esp_log_printf_(int level, const char *tag, int line, const __FlashStri
}
#endif
void HOT esp_log_str_(int level, const char *tag, int line, const char *format) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
// Before Logger::pre_setup(), the formatting path below reports the early call
if (logger::global_logger != nullptr)
#endif
{
if (logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format))
return;
}
// Formats the line log_str_() could not copy; it has no '%', so it is its own format
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
esp_log_printf_(level, tag, line, format);
#pragma GCC diagnostic pop
#endif
}
#ifdef USE_ESP8266
void HOT esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format) {
#ifdef USE_LOGGER
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format);
#endif
}
#endif
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
+57 -14
View File
@@ -67,6 +67,14 @@ void esp_log_printf_(int level, const char *tag, int line, const char *format, .
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...);
#endif
// esp_log_printf_ for a line with no arguments and no '%': it is copied without formatting
void esp_log_str_(int level, const char *tag, int line, const char *format); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format);
#endif
// Never defined or called: the ESP_LOGx macros name it in dead code so a line without arguments has its format checked
int esp_log_format_check_(const char *format, ...) __attribute__((format(printf, 1, 2))); // NOLINT
void esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
#if defined(USE_ESP32)
int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
@@ -78,9 +86,50 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#define ESPHOME_LOG_FORMAT(format) format
#endif
#define ESPHOME_LOG_ARGS_(level, tag, format, ...) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format), __VA_ARGS__)
// The format check is a statement-level if (false), not sizeof: GCC 16 skips format checks in unevaluated or folded
// expressions. __builtin_strchr folds on a literal, so only a line with '%' (such as "100%%") is formatted.
#ifdef USE_ESP8266
// F() only takes a literal, so using format more than once cannot repeat a side effect
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
if (__builtin_strchr(format, '%') != nullptr) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
else \
::esphome::esp_log_str_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
})
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
// A function argument, so format is evaluated once and a temporary such as std::string().c_str() outlives the call
// NOLINTNEXTLINE(readability-identifier-naming)
inline void __attribute__((always_inline)) esp_log_no_args_(int level, const char *tag, int line, const char *format) {
if (__builtin_strchr(format, '%') != nullptr) {
esp_log_printf_(level, tag, line, format);
} else {
esp_log_str_(level, tag, line, format);
}
}
#pragma GCC diagnostic pop
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
::esphome::esp_log_no_args_(level, tag, __LINE__, format); \
})
#endif
// ESPHOME_LOG_NO_ARGS_ when there are no arguments after the format, ESPHOME_LOG_ARGS_ otherwise
#define ESPHOME_LOG_PICK_(skip, name, ...) name
#define ESPHOME_LOG_(level, tag, format, ...) \
ESPHOME_LOG_PICK_(__VA_OPT__(, ) ESPHOME_LOG_ARGS_, ESPHOME_LOG_NO_ARGS_)(level, tag, \
format __VA_OPT__(, ) __VA_ARGS__)
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
#define esph_log_vv(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_vv(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_VERY_VERBOSE
#else
@@ -88,8 +137,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
#define esph_log_v(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_v(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERBOSE, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_VERBOSE
#else
@@ -97,10 +145,8 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
#define esph_log_d(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_config(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_d(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_DEBUG, tag, format, __VA_ARGS__)
#define esph_log_config(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_CONFIG, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_DEBUG
#define ESPHOME_LOG_HAS_CONFIG
@@ -110,8 +156,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
#define esph_log_i(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_i(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_INFO, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_INFO
#else
@@ -119,8 +164,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
#define esph_log_w(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_WARN, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_w(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_WARN, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_WARN
#else
@@ -128,8 +172,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
#define esph_log_e(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_ERROR, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_e(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_ERROR, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_ERROR
#else
@@ -222,6 +222,20 @@ def test_a_resolved_unit_waits_for_the_full_config() -> None:
assert config[CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(59.0)
def test_final_validate_skips_a_platform_that_only_holds_nested_climates() -> None:
"""A hub platform may put each of its climates under a key of one platform entry."""
nested = climate_schema(climate_ns.class_("NestedClimate", Climate))
platform_schema = cv.Schema({cv.Optional("zone_1"): nested})
config = platform_schema(
{"zone_1": {CONF_NAME: "Zone 1", CONF_VISUAL: {CONF_MIN_TEMPERATURE: "50°F"}}}
)
# The platform entry itself is not a climate and has no visual section
assert CONF_VISUAL not in config
_final_validate([config])
# The nested climate was converted by its own schema
assert config["zone_1"][CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(10.0)
@pytest.mark.parametrize(
("use_fahrenheit", "cn105_lines"),
[
+95 -2
View File
@@ -8,11 +8,16 @@ from esphome.components.npm1300 import (
CHARGER_SCHEMA,
NPM1300_UNSET,
OUTPUT_VOLTAGE,
THERMISTOR_NONE,
VBUS_CURRENT_LIMIT,
_charger_config,
_check_thermistor,
_config,
_output_config,
_termination_voltage_code,
_thermistor_resistance,
_vbus_current_limit_code,
thermistor_beta,
)
from esphome.const import CONF_MODE, CONF_VOLTAGE
from esphome.cpp_generator import StructInitializer
@@ -54,10 +59,35 @@ def test_termination_voltage_gap_rejected(voltage: str) -> None:
CHARGER_SCHEMA({"charge_current": "100mA", "termination_voltage": voltage})
def test_warm_termination_defaults_to_termination() -> None:
def test_warm_termination_defaults_to_reset_value() -> None:
fields = _charger({"charge_current": "100mA", "termination_voltage": "4.1V"})
assert fields["termination_voltage"] == "6"
assert fields["warm_termination_voltage"] == "6"
assert fields["warm_termination_voltage"] == "2"
@pytest.mark.parametrize(
("config", "warns"),
[
({"termination_voltage": "4.2V"}, True),
({"termination_voltage": "3.5V"}, False),
({"termination_voltage": "3.6V"}, False),
({}, False),
({"termination_voltage": "4.2V", "warm_termination_voltage": "4.0V"}, False),
],
)
def test_warm_termination_default_warning(
config: ConfigType, warns: bool, caplog: pytest.LogCaptureFixture
) -> None:
CHARGER_SCHEMA(config)
assert ("warm_termination_voltage" in caplog.text) is warns
@pytest.mark.parametrize(("termination", "code"), [("3.5V", "0"), ("3.55V", "1")])
def test_warm_termination_default_not_above_termination(
termination: str, code: str
) -> None:
fields = _charger({"termination_voltage": termination})
assert fields["warm_termination_voltage"] == code
def test_warm_termination_code() -> None:
@@ -112,3 +142,66 @@ def test_vbus_current_limit_code(limit: str, code: int) -> None:
def test_vbus_current_limit_rejected(limit: str) -> None:
with pytest.raises(cv.Invalid):
VBUS_CURRENT_LIMIT(limit)
def test_charger_needs_thermistor() -> None:
with pytest.raises(cv.Invalid, match="thermistor_resistance"):
_check_thermistor({"charger": {}})
config = {"charger": {}, "thermistor_resistance": THERMISTOR_NONE}
assert _check_thermistor(config) is config
@pytest.mark.parametrize("resistance", [None, THERMISTOR_NONE])
def test_thermistor_beta_needs_resistance(resistance: str | None) -> None:
config = {"thermistor_beta": 3380}
if resistance is not None:
config["thermistor_resistance"] = resistance
with pytest.raises(cv.Invalid, match="thermistor_beta"):
_check_thermistor(config)
@pytest.mark.parametrize(
("resistance", "beta"), [(10000.0, 3380), (47000.0, 4050), (100000.0, 4250)]
)
def test_thermistor_beta_default(resistance: float, beta: int) -> None:
assert thermistor_beta({"thermistor_resistance": resistance}) == beta
def test_thermistor_beta_configured() -> None:
config = {"thermistor_resistance": 47000.0, "thermistor_beta": 3950}
assert thermistor_beta(config) == 3950
@pytest.mark.parametrize(
("resistance", "code", "beta"),
[
(None, str(NPM1300_UNSET), "0"),
(THERMISTOR_NONE, "0", "0"),
(100000.0, "3", "4250"),
],
)
def test_config_thermistor(
resistance: str | float | None, code: str, beta: str
) -> None:
config = {} if resistance is None else {"thermistor_resistance": resistance}
fields = _fields(_config(config))
assert fields["thermistor"] == code
assert fields["thermistor_beta"] == beta
@pytest.mark.parametrize(
("value", "result"),
[
("10kOhm", 10000.0),
("0.047MOhm", 47000.0),
("100000", 100000.0),
("None", "none"),
],
)
def test_thermistor_resistance(value: str, result: float | str) -> None:
assert _thermistor_resistance(value) == result
def test_thermistor_resistance_rejected() -> None:
with pytest.raises(cv.Invalid, match="10kOhm, 47kOhm, 100kOhm or none"):
_thermistor_resistance("22kOhm")
@@ -0,0 +1,39 @@
esphome:
name: test
esp8266:
board: esp01_1m
button:
# Declared before the selects, so the action is generated before them
- platform: template
id: early
name: Early
on_press:
- select.set:
id: fan
option: high
- select.set:
id: fan
option: turbo
- select.set:
id: fan
option: !lambda return "low";
- select.set:
id: fan_copy
option: medium
select:
- platform: template
id: fan
name: Fan
optimistic: true
options: [low, medium, high]
on_value:
- select.set:
id: fan
option: medium
- platform: copy
id: fan_copy
name: Fan Copy
source_id: fan
@@ -0,0 +1,35 @@
---
esphome:
name: toshiba-test
esp32:
board: esp32dev
remote_transmitter:
pin: GPIO4
carrier_duty_percent: 50%
non_blocking: true
climate:
- platform: toshiba
id: ras
name: RAS
model: RAS-25J2KVSG-ND
- platform: toshiba
id: generic
name: Generic
select:
- platform: toshiba
toshiba_id: ras
power_selection:
id: power
name: Power selection
button:
- platform: template
name: Set
on_press:
- select.set:
id: power
option: Silent 1
@@ -0,0 +1,47 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
spi:
- id: spi_bus
clk_pin: GPIO18
mosi_pin: GPIO23
display:
- platform: mipi_spi
spi_id: spi_bus
model: st7789v
id: tft_display
dimensions:
width: 240
height: 320
cs_pin: GPIO22
dc_pin: GPIO21
auto_clear_enabled: false
invert_colors: false
update_interval: never
lvgl:
displays: tft_display
widgets:
- dropdown:
id: dropdown_widget
options: [low, high]
select:
- platform: lvgl
id: lv_fan
name: LVGL Fan
widget: dropdown_widget
button:
- platform: template
name: Set
on_press:
- select.set:
id: lv_fan
option: high
@@ -0,0 +1,43 @@
"""A constant select.set option of a select with a fixed option list becomes its index."""
from collections.abc import Callable
from pathlib import Path
def _call(select_id: str, statement: str) -> str:
return f"auto apply_call = ::{select_id}->make_call();\n apply_call.{statement};"
def test_select_set_constant_uses_index(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("select_set.yaml"))
# Generated before the select, and from the select's own on_value
assert _call("fan", "set_index(2)") in main_cpp
assert _call("fan", "set_index(1)") in main_cpp
assert 'set_option("high"' not in main_cpp
# Not an option: the lookup by text still logs it at run time
assert _call("fan", 'set_option("turbo", 5)') in main_cpp
# A lambda's value is only known at run time
assert _call("fan", 'set_option("low")') in main_cpp
# A copy select gets its options at run time
assert _call("fan_copy", 'set_option("medium", 6)') in main_cpp
def test_select_set_lvgl_keeps_lookup_by_text(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Lvgl dropdown options can be replaced at run time, so the index is not fixed."""
main_cpp = generate_main(component_config_path("select_set_lvgl.yaml"))
assert _call("lv_fan", 'set_option("high", 4)') in main_cpp
def test_select_set_hub_select_uses_index(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("select_set_hub.yaml"))
assert _call("power", "set_index(3)") in main_cpp
+23
View File
@@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstring>
@@ -271,6 +272,28 @@ TEST(BufAppendStrP, AppendsAndTruncates) {
EXPECT_EQ(pos, 7u);
}
// --- progmem_copy_str() ---
TEST(ProgmemCopyStr, CopiesUpToTheNullOrTheEndAtEveryAlignment) {
static constexpr char TEXT[] = "0123456789abcdefghij";
constexpr size_t len = sizeof(TEXT) - 1;
for (size_t offset = 0; offset < 4; offset++) {
// Reads whole aligned words, so the source is a zero padded copy
alignas(4) char src[32] = {};
memcpy(src + offset, TEXT, sizeof(TEXT));
for (size_t room = 0; room <= len + 2; room++) {
SCOPED_TRACE(::testing::Message() << "offset " << offset << " room " << room);
char dst[32];
memset(dst, '#', sizeof(dst));
const char *const end = progmem_copy_str(dst, dst + room, src + offset);
const size_t copied = std::min(room, len);
EXPECT_EQ(end, dst + copied);
EXPECT_EQ(memcmp(dst, TEXT, copied), 0);
EXPECT_EQ(dst[copied], '#'); // No terminator written
}
}
}
// --- base64 encode/decode ---
static const char BASE64_ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# touchscreen.h includes display.h
manifest.dependencies = manifest.dependencies + ["display"]
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <array>
#include <gtest/gtest.h>
#include "esphome/components/cst816/touchscreen/cst816_touchscreen.h"
namespace esphome::cst816::testing {
static constexpr uint8_t ADDRESS = 0x15;
// The driver reads REG_STATUS onwards in one transfer: status, gesture, touch count, then two points.
static constexpr size_t FRAME_SIZE = 13;
/// Serves one fixed status frame.
class FrameBus final : public i2c::I2CBus {
public:
explicit FrameBus(std::array<uint8_t, FRAME_SIZE> frame) : frame_(frame) {}
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
size_t read_count) override {
EXPECT_EQ(address, ADDRESS);
for (size_t i = 0; i < read_count; i++)
read_buffer[i] = i < FRAME_SIZE ? this->frame_[i] : 0;
return i2c::ERROR_OK;
}
protected:
std::array<uint8_t, FRAME_SIZE> frame_;
};
inline std::array<uint8_t, FRAME_SIZE> make_frame(uint8_t touch_num, uint16_t x = 0, uint16_t y = 0) {
std::array<uint8_t, FRAME_SIZE> frame{};
frame[REG_TOUCH_NUM] = touch_num;
frame[REG_XPOS_HIGH] = (x >> 8) & 0x0F;
frame[REG_XPOS_LOW] = x & 0xFF;
frame[REG_YPOS_HIGH] = (y >> 8) & 0x0F;
frame[REG_YPOS_LOW] = y & 0xFF;
return frame;
}
} // namespace esphome::cst816::testing
@@ -0,0 +1,88 @@
#include <gtest/gtest.h>
#include <memory>
#include "../common.h"
namespace esphome::cst816::testing {
class CST816TouchscreenTest : public ::testing::Test {
protected:
void feed_(const std::array<uint8_t, FRAME_SIZE> &frame) {
this->bus_ = std::make_unique<FrameBus>(frame);
this->touchscreen_.set_i2c_bus(this->bus_.get());
this->touchscreen_.set_i2c_address(ADDRESS);
this->touchscreen_.update_touches();
}
std::unique_ptr<FrameBus> bus_;
CST816Touchscreen touchscreen_;
};
// An idle frame reports 0xFF in the touch count byte and 0xFFF coordinates. Masking 0xFF to the
// low two bits turns that into three touches at the maximum position, a phantom tap.
TEST_F(CST816TouchscreenTest, IdleAllOnesFrameIsIgnored) {
this->feed_(make_frame(0xFF, 0x0FFF, 0x0FFF));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
TEST_F(CST816TouchscreenTest, ZeroTouchCountIsIgnored) {
this->feed_(make_frame(0));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
// Three is the boundary the guard rejects: the old mask turned a count of three into three
// touches, so this is the value whose behaviour changes.
TEST_F(CST816TouchscreenTest, TouchCountThreeIsIgnored) {
this->feed_(make_frame(3, 100, 200));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
// The count is the whole low nibble, so 0x05 must not be masked down to one touch by a 0x03 mask.
TEST_F(CST816TouchscreenTest, TouchCountFiveIsIgnored) {
this->feed_(make_frame(0x05, 100, 200));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
TEST_F(CST816TouchscreenTest, SingleTouchIsReported) {
this->feed_(make_frame(1, 300, 100));
auto touches = this->touchscreen_.get_touches();
ASSERT_EQ(touches.size(), 1u);
EXPECT_EQ(touches[0].x_raw, 300);
EXPECT_EQ(touches[0].y_raw, 100);
}
// CST826/CST836 can report two points; the driver only consumes the first one.
TEST_F(CST816TouchscreenTest, TwoPointFrameReportsFirstPoint) {
this->feed_(make_frame(2, 300, 100));
auto touches = this->touchscreen_.get_touches();
ASSERT_EQ(touches.size(), 1u);
EXPECT_EQ(touches[0].x_raw, 300);
EXPECT_EQ(touches[0].y_raw, 100);
}
TEST_F(CST816TouchscreenTest, SentinelPositionIsIgnored) {
this->feed_(make_frame(1, 0x0FFF, 100));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
// The sentinel is checked on both axes; a valid X with Y=0xFFF is still an idle reading.
TEST_F(CST816TouchscreenTest, SentinelYPositionIsIgnored) {
this->feed_(make_frame(1, 100, 0x0FFF));
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
}
// x_raw_max_/y_raw_max_ are calibration endpoints, not validity limits: the base class applies them
// after swapping axes and clamps readings outside them. A touch beyond the calibrated maximum is
// still a real touch and must reach the base class.
TEST_F(CST816TouchscreenTest, CalibrationBoundsDoNotDropTouches) {
this->touchscreen_.set_calibration(0, 240, 0, 320);
this->touchscreen_.set_swap_xy(true);
this->feed_(make_frame(1, 300, 100));
auto touches = this->touchscreen_.get_touches();
ASSERT_EQ(touches.size(), 1u);
EXPECT_EQ(touches[0].x_raw, 100);
EXPECT_EQ(touches[0].y_raw, 300);
}
} // namespace esphome::cst816::testing
@@ -0,0 +1,140 @@
#include <gtest/gtest.h>
#include <cstdarg>
#include <cstring>
#include <string>
#include <thread>
#include <vector>
#include "esphome/components/logger/log_buffer.h"
#include "esphome/components/logger/logger.h"
#include "esphome/core/log.h"
namespace esphome::logger::testing {
ESPHOME_LOG_TAG(PLAIN_TAG, "plain.test");
static void format_into(LogBuffer &buf, const char *format, ...) {
va_list args;
va_start(args, format);
buf.format_body(format, args);
va_end(args);
}
TEST(UnformattedLogTest, BodyMatchesFormattedBodyAtEverySize) {
const std::string long_text(100, 'y');
const char *texts[] = {"", "short", "ends with newlines\n\n", "\n", "a\nb", long_text.c_str()};
constexpr uint16_t max_size = 48;
for (uint16_t size = 1; size <= max_size; size++) {
for (uint16_t start = 0; start <= size; start++) {
for (const char *text : texts) {
SCOPED_TRACE(::testing::Message() << "size " << size << " start " << start << " text '" << text << "'");
char formatted[max_size], unformatted[max_size], flash[max_size];
memset(formatted, '#', sizeof(formatted));
memset(unformatted, '#', sizeof(unformatted));
memset(flash, '#', sizeof(flash));
LogBuffer a{formatted, size, start};
LogBuffer b{unformatted, size, start};
LogBuffer c{flash, size, start};
format_into(a, "%s", text);
b.write_unformatted_body(text, strlen(text));
// The flash copy reads whole aligned words, so give it a zero padded copy
alignas(4) char padded[128] = {};
char *const in_flash = padded + (start & 3);
memcpy(in_flash, text, strlen(text) + 1);
c.write_unformatted_body_p(in_flash);
ASSERT_EQ(a.pos, b.pos);
ASSERT_EQ(a.pos, c.pos);
// Up to and including the null terminator; vsnprintf() may leave bytes after it that nothing reads
const size_t used = (a.pos < size) ? a.pos + 1 : size;
ASSERT_EQ(memcmp(formatted, unformatted, used), 0);
ASSERT_EQ(memcmp(formatted, flash, used), 0);
}
}
}
}
struct PlainLine {
uint8_t level;
std::string message;
};
static void capture_plain(void *instance, uint8_t level, const char *tag, const char *message, size_t len) {
static_cast<std::vector<PlainLine> *>(instance)->push_back({level, std::string(message, len)});
}
class UnformattedLoggerTest : public ::testing::Test {
protected:
void SetUp() override {
this->previous_ = global_logger;
this->logger_.pre_setup();
this->logger_.set_log_level(ESPHOME_LOG_LEVEL_DEBUG);
this->logger_.add_log_callback(&this->lines_, capture_plain);
}
void TearDown() override { global_logger = this->previous_; }
Logger *previous_{nullptr};
Logger logger_{0};
std::vector<PlainLine> lines_;
};
TEST_F(UnformattedLoggerTest, LineWithoutArgumentsMatchesFormattedLine) {
esp_log_str_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "no arguments here\n");
esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "%s", "no arguments here\n");
ASSERT_EQ(this->lines_.size(), 2u);
EXPECT_EQ(this->lines_[0].level, ESPHOME_LOG_LEVEL_INFO);
EXPECT_EQ(this->lines_[0].message, this->lines_[1].message);
EXPECT_NE(this->lines_[0].message.find("[I][plain.test:007]: no arguments here\033[0m"), std::string::npos)
<< this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, PercentSignIsStillFormatted) {
ESP_LOGW(PLAIN_TAG, "100%% done");
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: 100% done"), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, FormatExpressionIsEvaluatedOnce) {
const char *const messages[] = {"first", "second"};
int index = 0;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
ESP_LOGW(PLAIN_TAG, messages[index++]);
#pragma GCC diagnostic pop
EXPECT_EQ(index, 1);
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: first"), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, TemporaryFormatOutlivesTheCall) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
ESP_LOGW(PLAIN_TAG, std::string(80, 'x').c_str());
#pragma GCC diagnostic pop
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: " + std::string(80, 'x')), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, LevelIsStillFiltered) {
this->logger_.set_log_level(ESPHOME_LOG_LEVEL_WARN);
ESP_LOGI(PLAIN_TAG, "hidden");
ESP_LOGW(PLAIN_TAG, "shown");
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_EQ(this->lines_[0].level, ESPHOME_LOG_LEVEL_WARN);
}
#ifdef USE_HOST
TEST_F(UnformattedLoggerTest, WorkerThreadLineGoesThroughTaskBuffer) {
this->logger_.create_pthread_key();
std::thread worker([]() { esp_log_str_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "from a worker"); });
worker.join();
EXPECT_TRUE(this->lines_.empty());
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
this->logger_.loop();
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("from a worker"), std::string::npos) << this->lines_[0].message;
#endif
}
#endif // USE_HOST
} // namespace esphome::logger::testing
+38 -22
View File
@@ -1,29 +1,38 @@
npm1300:
id: pmic
i2c_id: i2c_bus
vbus_current_limit: 500mA
thermistor_resistance: 10kOhm
thermistor_beta: 3380
charger:
charge_current: 150mA
termination_voltage: 4.2V
warm_termination_voltage: 4.0V
termination_current: 20%
trickle_voltage: 2.5V
discharge_current_limit: 1000mA
recharge: true
buck2:
voltage: 3.3V
ldo1:
voltage: 3.3V
mode: ldo
enabled: true
ldo2:
mode: load_switch
enabled: false
- id: pmic
i2c_id: i2c_bus
vbus_current_limit: 500mA
thermistor_resistance: 10kOhm
thermistor_beta: 3380
charger:
charge_current: 150mA
termination_voltage: 4.2V
warm_termination_voltage: 4.0V
termination_current: 20%
trickle_voltage: 2.5V
discharge_current_limit: 1000mA
recharge: true
buck2:
voltage: 3.3V
ldo1:
voltage: 3.3V
mode: ldo
enabled: true
ldo2:
mode: load_switch
enabled: false
# A battery without a thermistor
- id: pmic_no_ntc
i2c_id: i2c_bus
address: 0x6C
thermistor_resistance: none
charger:
termination_voltage: 3.5V
enabled: true
sensor:
- platform: npm1300
npm1300_id: pmic
battery_voltage:
name: Battery Voltage
battery_current:
@@ -41,6 +50,7 @@ sensor:
binary_sensor:
- platform: npm1300
npm1300_id: pmic
vbus_present:
name: VBUS Present
vbus_current_limit_active:
@@ -54,11 +64,13 @@ binary_sensor:
text_sensor:
- platform: npm1300
npm1300_id: pmic
charger_status:
name: Charger Status
switch:
- platform: npm1300
npm1300_id: pmic
charger:
name: Charger
buck1:
@@ -67,6 +79,10 @@ switch:
name: LDO1
ldo2:
name: LDO2
- platform: npm1300
npm1300_id: pmic_no_ntc
charger:
name: Charger Without Thermistor
button:
- platform: template
@@ -6,9 +6,9 @@
#include <random>
#include <string>
#include "esphome/components/web_server_idf/sse_chunk.h"
#include "esphome/components/web_server/sse_chunk.h"
namespace esphome::web_server_idf::testing {
namespace esphome::web_server::testing {
// The chunk as try_send_nodefer lays it out: header, fields, first "data: ", then the pieces
static std::string build_chunk(const char *message, size_t message_len, const char *event, uint32_t id,
@@ -128,4 +128,4 @@ TEST(SseChunk, MatchesReferenceOnRandomMessages) {
}
}
} // namespace esphome::web_server_idf::testing
} // namespace esphome::web_server::testing
@@ -0,0 +1,300 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <sys/uio.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "esphome/components/web_server/sse_chunk.h"
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server::testing {
using namespace web_server_base; // NOLINT(google-build-using-namespace)
// Collects every batch write_stream_head() sends
struct Capture {
std::string bytes;
std::vector<int> batch_sizes;
int attempts{0};
int fail_after_batches{-1};
bool operator()(struct iovec *iov, int count) {
this->attempts++;
if (static_cast<int>(this->batch_sizes.size()) == this->fail_after_batches) {
return false;
}
this->batch_sizes.push_back(count);
for (int i = 0; i < count; i++) {
this->bytes.append(static_cast<const char *>(iov[i].iov_base), iov[i].iov_len);
}
return true;
}
};
static std::string build_head(Capture &cap, const char *status, const char *content_type,
std::span<const HttpHeader> headers, std::span<const HttpHeader> defaults,
const char *body) {
EXPECT_TRUE(write_stream_head<struct iovec>(cap, status, content_type, headers, defaults, body));
return cap.bytes;
}
// What httpd_resp_send_chunk() put on the wire for its first chunk (esp_http_server/src/httpd_txrx.c in
// ESP-IDF 5.5): the chunked head with Transfer-Encoding, each httpd_resp_set_hdr() header, the blank line,
// then "%lx\r\n", the chunk data and "\r\n".
static std::string reference_send_chunk(const char *status, const char *content_type,
const std::vector<HttpHeader> &resp_hdrs, const char *buf, size_t buf_len) {
char head[256];
std::string out(
head, snprintf(head, sizeof(head), "HTTP/1.1 %s\r\nContent-Type: %s\r\nTransfer-Encoding: chunked\r\n", status,
content_type));
for (const auto &hdr : resp_hdrs) {
out.append(hdr.name).append(": ").append(hdr.value).append("\r\n");
}
out.append("\r\n");
char len_str[10];
out.append(len_str, snprintf(len_str, sizeof(len_str), "%lx\r\n", static_cast<long>(buf_len)));
out.append(buf, buf_len);
out.append("\r\n");
return out;
}
static constexpr HttpHeader DEFAULTS[] = {
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Private-Network", "true"},
};
TEST(StreamHead, SseHeadMatchesHttpdRespSendChunk) {
// The old event source set Cache-Control and Connection, then the default headers, and sent "\r\n" as a chunk
const std::string expected = reference_send_chunk(
"200 OK", "text/event-stream",
{{"Cache-Control", "no-cache"}, {"Connection", "keep-alive"}, DEFAULTS[0], DEFAULTS[1]}, "\r\n", 2);
Capture cap;
EXPECT_EQ(build_head(cap, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, DEFAULTS, SSE_FIRST_CHUNK), expected);
}
TEST(StreamHead, HeaderReplacesDefaultOfSameName) {
// A default the caller also sets, in any case, goes out once with the caller's value
static constexpr HttpHeader HEADERS[] = {
{"access-control-allow-origin", "http://example.com"},
};
Capture cap;
EXPECT_EQ(build_head(cap, "200 OK", "text/plain", HEADERS, DEFAULTS, nullptr),
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\naccess-control-allow-origin: http://example.com"
"\r\nAccess-Control-Allow-Private-Network: true\r\n\r\n");
}
TEST(StreamHead, RawHeadWithCustomHeadersAndNoBody) {
static constexpr HttpHeader HEADERS[] = {
{"Cache-Control", "no-store"},
{"X-Stream", "audio"},
};
Capture cap;
EXPECT_EQ(build_head(cap, "200 OK", "audio/wav", HEADERS, {}, nullptr), "HTTP/1.1 200 OK\r\n"
"Content-Type: audio/wav\r\n"
"Cache-Control: no-store\r\n"
"X-Stream: audio\r\n"
"\r\n");
}
TEST(StreamHead, BareHead) {
Capture cap;
EXPECT_EQ(build_head(cap, "204 No Content", "text/plain", {}, {}, nullptr),
"HTTP/1.1 204 No Content\r\nContent-Type: text/plain\r\n\r\n");
EXPECT_EQ(cap.batch_sizes, std::vector<int>{5});
}
TEST(StreamHead, ManyHeadersSplitIntoBatchesWithSameBytes) {
// 4 + 12 * 4 + 2 = 54 pieces, more than two batches hold
static constexpr HttpHeader HEADERS[] = {
{"X-Header-0", "value-0"}, {"X-Header-1", "value-1"}, {"X-Header-2", "value-2"}, {"X-Header-3", "value-3"},
{"X-Header-4", "value-4"}, {"X-Header-5", "value-5"}, {"X-Header-6", "value-6"}, {"X-Header-7", "value-7"},
{"X-Header-8", "value-8"}, {"X-Header-9", "value-9"},
};
const std::span<const HttpHeader> headers{HEADERS};
Capture batched;
const std::string bytes = build_head(batched, "200 OK", "text/plain", headers, DEFAULTS, "body");
EXPECT_EQ(batched.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, STREAM_HEAD_MAX_IOV, 6}));
std::string expected = "HTTP/1.1 200 OK\r\nContent-Type: text/plain";
const auto append_headers = [&expected](std::span<const HttpHeader> list) {
for (const auto &h : list) {
expected.append("\r\n").append(h.name).append(": ").append(h.value);
}
};
append_headers(headers);
append_headers(DEFAULTS);
expected.append("\r\n\r\nbody");
EXPECT_EQ(bytes, expected);
}
TEST(StreamHead, BatchBoundary) {
// 4 + 4 * 4 + 1 + 1 = 22 pieces fit one batch; a fifth header makes 4 + 5 * 4 = 24 before the blank line
static constexpr HttpHeader HEADERS[] = {{"A", "1"}, {"B", "2"}, {"C", "3"}, {"D", "4"}, {"E", "5"}};
Capture four;
build_head(four, "200 OK", "text/plain", std::span(HEADERS, 4), {}, "x");
EXPECT_EQ(four.batch_sizes, std::vector<int>{22});
EXPECT_EQ(stream_head_iov_count(4), 22u);
Capture five;
const std::string bytes = build_head(five, "200 OK", "text/plain", HEADERS, {}, "x");
EXPECT_EQ(five.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, 2}));
EXPECT_EQ(bytes, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nA: 1\r\nB: 2\r\nC: 3\r\nD: 4\r\nE: 5\r\n\r\nx");
}
TEST(StreamHead, FailedSendSkipsLaterBatches) {
// Three batches; the second fails, so the third is never sent
std::vector<HttpHeader> headers(12, HttpHeader{"X", "y"});
Capture full;
build_head(full, "200 OK", "text/plain", headers, {}, nullptr);
ASSERT_EQ(full.batch_sizes.size(), 3u);
Capture cap;
cap.fail_after_batches = 1;
EXPECT_FALSE(write_stream_head<struct iovec>(cap, "200 OK", "text/plain", headers, {}, nullptr));
EXPECT_EQ(cap.attempts, 2);
EXPECT_EQ(cap.batch_sizes.size(), 1u);
}
// Three elements of 3, 4 and 5 bytes
struct IovFixture {
char a[4] = "abc";
char b[5] = "defg";
char c[6] = "hijkl";
struct iovec iov[3] = {{a, 3}, {b, 4}, {c, 5}};
struct iovec *cur = iov;
int count = 3;
std::string rest() const {
std::string out;
for (int i = 0; i < this->count; i++) {
out.append(static_cast<const char *>(this->cur[i].iov_base), this->cur[i].iov_len);
}
return out;
}
};
TEST(AdvanceIovecs, ZeroLengthWriteChangesNothing) {
IovFixture f;
advance_iovecs(f.cur, f.count, 0);
EXPECT_EQ(f.cur, f.iov);
EXPECT_EQ(f.count, 3);
EXPECT_EQ(f.rest(), "abcdefghijkl");
}
TEST(AdvanceIovecs, PartialInsideFirstElement) {
IovFixture f;
advance_iovecs(f.cur, f.count, 2);
EXPECT_EQ(f.cur, f.iov);
EXPECT_EQ(f.count, 3);
EXPECT_EQ(f.cur->iov_len, 1u);
EXPECT_EQ(f.rest(), "cdefghijkl");
}
TEST(AdvanceIovecs, ExactlyOnElementBoundary) {
IovFixture f;
advance_iovecs(f.cur, f.count, 3);
EXPECT_EQ(f.cur, &f.iov[1]);
EXPECT_EQ(f.count, 2);
EXPECT_EQ(f.rest(), "defghijkl");
}
TEST(AdvanceIovecs, AcrossElementBoundaries) {
IovFixture f;
advance_iovecs(f.cur, f.count, 9);
EXPECT_EQ(f.cur, &f.iov[2]);
EXPECT_EQ(f.count, 1);
EXPECT_EQ(f.rest(), "jkl");
}
TEST(AdvanceIovecs, RepeatedShortWrites) {
IovFixture f;
std::string sent;
while (f.count > 0) {
const std::string before = f.rest();
advance_iovecs(f.cur, f.count, 2);
sent += before.substr(0, before.size() - f.rest().size());
}
EXPECT_EQ(sent, "abcdefghijkl");
}
TEST(AdvanceIovecs, EverythingWritten) {
IovFixture f;
advance_iovecs(f.cur, f.count, 12);
EXPECT_EQ(f.count, 0);
EXPECT_EQ(f.rest(), "");
}
TEST(AdvanceIovecs, SkipsZeroLengthElements) {
char a[] = "ab";
char c[] = "cd";
struct iovec iov[3] = {{a, 2}, {nullptr, 0}, {c, 2}};
struct iovec *cur = iov;
int count = 3;
advance_iovecs(cur, count, 2);
EXPECT_EQ(cur, &iov[2]);
EXPECT_EQ(count, 1);
}
static constexpr uint32_t TIMEOUT = 20000;
TEST(StallClock, NotStarted) {
StallClock clock;
EXPECT_FALSE(clock.is_running());
EXPECT_FALSE(clock.is_expired(0, TIMEOUT));
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
}
TEST(StallClock, ExpiresAtExactlyTheTimeout) {
StallClock clock;
clock.start(1000);
EXPECT_TRUE(clock.is_running());
EXPECT_FALSE(clock.is_expired(1000, TIMEOUT));
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT - 1, TIMEOUT));
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT + 1, TIMEOUT));
EXPECT_EQ(clock.elapsed(1000 + TIMEOUT), TIMEOUT);
}
TEST(StallClock, RunningClockKeepsItsStart) {
StallClock clock;
clock.start(1000);
clock.start(5000);
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
}
TEST(StallClock, Cleared) {
StallClock clock;
clock.start(1000);
clock.clear();
EXPECT_FALSE(clock.is_running());
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
clock.start(50000);
EXPECT_FALSE(clock.is_expired(50000 + TIMEOUT - 1, TIMEOUT));
EXPECT_TRUE(clock.is_expired(50000 + TIMEOUT, TIMEOUT));
}
TEST(StallClock, StartAtZeroStillRuns) {
StallClock clock;
clock.start(0);
EXPECT_TRUE(clock.is_running());
// Recorded as 1, so it expires one millisecond late
EXPECT_FALSE(clock.is_expired(TIMEOUT, TIMEOUT));
EXPECT_TRUE(clock.is_expired(TIMEOUT + 1, TIMEOUT));
}
TEST(StallClock, AcrossMillisWraparound) {
StallClock clock;
const uint32_t start = UINT32_MAX - 5000;
clock.start(start);
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
EXPECT_FALSE(clock.is_expired(start + TIMEOUT - 1, TIMEOUT)); // wrapped to 14998
EXPECT_TRUE(clock.is_expired(start + TIMEOUT, TIMEOUT));
EXPECT_EQ(clock.elapsed(start + TIMEOUT), TIMEOUT);
}
} // namespace esphome::web_server::testing
#endif // USE_HOST
@@ -10,6 +10,7 @@ namespace {
constexpr uint8_t OLD_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};
constexpr uint8_t NEW_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x02};
constexpr uint8_t NO_AP[6] = {};
ExpectedDisconnect armed() {
ExpectedDisconnect expected;
@@ -20,20 +21,20 @@ ExpectedDisconnect armed() {
TEST(ExpectedDisconnectTest, PreviousApLeaveIsExpectedOnce) {
auto expected = armed();
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
// A second leave from the same AP is a real disconnect
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, OtherDisconnectsAreRealAndEndTheExpectation) {
auto other_ap = armed();
EXPECT_FALSE(other_ap.consume_disconnect(NEW_AP, true));
EXPECT_FALSE(other_ap.consume_disconnect(OLD_AP, true));
EXPECT_EQ(other_ap.consume_disconnect(NEW_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_ap.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
auto other_reason = armed();
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, false));
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, true));
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation) {
@@ -41,17 +42,17 @@ TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation)
EXPECT_TRUE(expected.is_previous_ap(OLD_AP));
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
}
TEST(ExpectedDisconnectTest, NothingIsExpectedUnlessArmed) {
ExpectedDisconnect expected;
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
auto cleared = armed();
cleared.clear();
EXPECT_FALSE(cleared.consume_disconnect(OLD_AP, true));
EXPECT_EQ(cleared.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
// Stands in for esp_wifi_sta_get_ap_info() and esp_wifi_disconnect(): the AP record is gone after disconnecting
@@ -82,16 +83,7 @@ TEST(ExpectedDisconnectTest, DisconnectExpectsTheLeaveOfTheApItLeft) {
Driver driver;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, NoApMeansNoExpectation) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
}
TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
@@ -106,5 +98,29 @@ TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
}
TEST(ExpectedDisconnectTest, DisconnectWithoutApExpectsTheAttemptsLeave) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A real AP never matches the attempt's zero BSSID
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
// The driver reports the attempt as left, with no BSSID, after the next one starts
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, AttemptLeaveExpectationEndsOnARealFailure) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A probe failure of the new attempt is real, and nothing stays expected after it
EXPECT_EQ(expected.consume_disconnect(NO_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
} // namespace
} // namespace esphome::wifi::testing
@@ -0,0 +1,62 @@
esphome:
name: select-set-constant
host:
api:
logger:
globals:
- id: next_option
type: std::string
initial_value: '"low"'
select:
- platform: template
name: "Fan"
id: fan
optimistic: true
options: [low, medium, high]
initial_option: low
- platform: template
name: "Picked"
id: picked
options: [low, medium, high]
update_interval: never
lambda: return id(next_option);
set_action:
- lambda: id(next_option) = std::string(x.c_str(), x.size());
button:
- platform: template
name: "Set High"
on_press:
- select.set:
id: fan
option: high
- platform: template
name: "Set Medium"
on_press:
- select.set:
id: fan
option: medium
- platform: template
name: "Set Invalid"
on_press:
- select.set:
id: fan
option: turbo
- platform: template
name: "Pick High"
on_press:
- lambda: id(next_option) = "high";
- component.update: picked
- platform: template
name: "Pick Invalid"
on_press:
- lambda: id(next_option) = "turbo";
- component.update: picked
- platform: template
name: "Pick Medium"
on_press:
- lambda: id(next_option) = "medium";
- component.update: picked
@@ -0,0 +1,50 @@
"""select.set with a constant option and a template select lambda, both set by index."""
from __future__ import annotations
from aioesphomeapi import SelectInfo, SelectState
import pytest
from .log_utils import LineWaiter
from .state_utils import press_and_wait, require_entity, subscribe_after_initial_states
from .types import APIClientConnectedFactory, RunCompiledFunction
from .wait_utils import wait_or_fail
@pytest.mark.asyncio
async def test_select_set_constant(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
waiter_log = LineWaiter()
invalid_lambda = waiter_log.expect("'Picked': Invalid option turbo")
# An unknown constant keeps the lookup by text and its warning, never a compiled index
invalid_constant = waiter_log.expect("'Fan' - No option set")
out_of_bounds = waiter_log.expect("out of bounds")
async with (
run_compiled(yaml_config, line_callback=waiter_log.callback),
api_client_connected() as client,
):
entities, _ = await client.list_entities_services()
fan = require_entity(entities, "fan", SelectInfo, exact=True)
picked = require_entity(entities, "picked", SelectInfo, exact=True)
sub = await subscribe_after_initial_states(client, entities)
async def press(button: str, key: int) -> str:
state = await press_and_wait(
client, entities, sub.waiter, button, SelectState, key=key
)
return state.state
assert await press("set_high", fan.key) == "high"
# An option the select does not have changes nothing; the next press is the next state
client.button_command(require_entity(entities, "set_invalid").key)
assert await press("set_medium", fan.key) == "medium"
await wait_or_fail(invalid_constant, 5.0, "invalid constant was not logged")
assert not out_of_bounds.done()
assert await press("pick_high", picked.key) == "high"
client.button_command(require_entity(entities, "pick_invalid").key)
await wait_or_fail(invalid_lambda, 5.0, "invalid lambda option was not logged")
assert await press("pick_medium", picked.key) == "medium"
@@ -1550,6 +1550,15 @@ def test_convert_visual_temperatures(
convert_visual_temperatures({"visual": {}}, delta_keys, delta_section)
def test_convert_visual_temperatures_without_a_visual_section() -> None:
"""A platform entry that only holds nested entities has no visual section and is left alone."""
nested = {"visual": {"min_temperature": (50.0, "°F")}}
config = {"zone_1": nested}
assert convert_visual_temperatures(config, ("step",)) is config
assert nested["visual"]["min_temperature"] == (50.0, "°F")
@pytest.mark.asyncio
async def test_setup_entity_integrations_registers_mqtt_and_web_server(
monkeypatch: pytest.MonkeyPatch, setup_test_environment: list[str]
@@ -326,6 +326,21 @@ def test_bind_key__rejects_non_hex_pair_length() -> None:
cv.bind_key("0123456789ABCDEF0123456789ABCDE")
@pytest.mark.parametrize(
"value",
(
" A A A A A A A A A A A A A A A A",
"0123456789ABCDEF0123456789ABCD-B",
"+E123456789ABCDEF0123456789ABCDE",
),
)
def test_bind_key__rejects_sign_padded_pairs(value: str) -> None:
# int(part, 16) accepts surrounding whitespace and a +/- sign, which would
# silently validate a key different from the one the user typed.
with pytest.raises(Invalid, match="hex values"):
cv.bind_key(value)
def test_bind_key__direct_call_with_name_validates_with_that_name() -> None:
# Passing both a value and a name validates immediately using the custom
# name for error wording, and still tags the result sensitive.
+7 -1
View File
@@ -1792,7 +1792,13 @@ def test_ccache_env_opt_in_without_binary(
# Explicit IDF_CCACHE_ENABLE=1 forces it on; without a usable binary
# IDF's CMake silently skips ccache, so this branch must say so out loud.
p1, p2, p3 = _ccache_patches(tmp_path, None, tmp_path / "build")
with patch.dict("os.environ", {"IDF_CCACHE_ENABLE": "1"}, clear=True), p1, p2, p3:
with (
patch.dict("os.environ", {"IDF_CCACHE_ENABLE": "1"}, clear=True),
patch("esphome.espidf.framework.shutil.which", return_value=None),
p1,
p2,
p3,
):
env = _ccache_env()
assert env["IDF_CCACHE_ENABLE"] == "1"
assert env["CCACHE_DIR"] == str(tmp_path / "tools" / "ccache")