From e9a0d0688057a5bcc9915486969cafb752b6894b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 9 Feb 2026 09:31:19 -0600 Subject: [PATCH 1/2] Add comment explaining early guard --- esphome/components/tuya/tuya.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index d0e00d425d..8d15b6d638 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -31,6 +31,8 @@ void Tuya::setup() { } void Tuya::loop() { + // Early return avoids stack adjustment for the batch buffer below. + // loop() runs ~7000/min so most calls have nothing to read. int avail = this->available(); if (avail > 0) { // Read all available bytes in batches to reduce UART call overhead. From d5295a894bfa3a0217a0387cf33aafa5e13d23f6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 9 Feb 2026 09:41:03 -0600 Subject: [PATCH 2/2] Remove unnecessary early guard --- esphome/components/tuya/tuya.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index 8d15b6d638..9ee4c09b86 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -31,22 +31,18 @@ void Tuya::setup() { } void Tuya::loop() { - // Early return avoids stack adjustment for the batch buffer below. - // loop() runs ~7000/min so most calls have nothing to read. + // Read all available bytes in batches to reduce UART call overhead. int avail = this->available(); - if (avail > 0) { - // Read all available bytes in batches to reduce UART call overhead. - uint8_t buf[64]; - while (avail > 0) { - size_t to_read = std::min(static_cast(avail), sizeof(buf)); - if (!this->read_array(buf, to_read)) { - break; - } - avail -= to_read; + uint8_t buf[64]; + while (avail > 0) { + size_t to_read = std::min(static_cast(avail), sizeof(buf)); + if (!this->read_array(buf, to_read)) { + break; + } + avail -= to_read; - for (size_t i = 0; i < to_read; i++) { - this->handle_char_(buf[i]); - } + for (size_t i = 0; i < to_read; i++) { + this->handle_char_(buf[i]); } } process_command_queue_();