From 619728b686c65adfa50a42e2ba7fc3de69b37284 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Oct 2026 07:23:06 -1000 Subject: [PATCH] [components] Drop stale UART receive bytes with discard_available (#21030) Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- esphome/components/bl0906/bl0906.cpp | 4 +--- esphome/components/cm1106/cm1106.cpp | 4 +--- esphome/components/cse7761/cse7761.cpp | 4 +--- .../dfrobot_sen0465_uart/dfrobot_sen0465_uart.cpp | 6 +----- esphome/components/dlms_meter/dlms_meter.cpp | 12 +++--------- esphome/components/dlms_meter/dlms_meter.h | 1 - .../components/fingerprint_grow/fingerprint_grow.cpp | 3 +-- esphome/components/hc8/hc8.cpp | 7 ++----- esphome/components/hlk_fm22x/hlk_fm22x.cpp | 11 +++-------- esphome/components/hydreon_rgxx/hydreon_rgxx.cpp | 4 +--- esphome/components/mhz19/mhz19.cpp | 4 +--- esphome/components/pylontech/pylontech.cpp | 6 +----- esphome/components/pzem004t/pzem004t.cpp | 7 ++----- esphome/components/shelly_dimmer/shelly_dimmer.cpp | 5 +---- esphome/components/t6615/t6615.cpp | 7 ++----- esphome/components/tmc22xx/tmc22xx.cpp | 6 ++---- 16 files changed, 23 insertions(+), 68 deletions(-) diff --git a/esphome/components/bl0906/bl0906.cpp b/esphome/components/bl0906/bl0906.cpp index 6f59da77d34..681c7ad15eb 100644 --- a/esphome/components/bl0906/bl0906.cpp +++ b/esphome/components/bl0906/bl0906.cpp @@ -134,9 +134,7 @@ void BL0906::handle_actions_() { } } - while (this->available()) { - this->read(); - } + this->discard_available(); this->action_queue_.clear(); } diff --git a/esphome/components/cm1106/cm1106.cpp b/esphome/components/cm1106/cm1106.cpp index 55b7556cda5..8739e6c0805 100644 --- a/esphome/components/cm1106/cm1106.cpp +++ b/esphome/components/cm1106/cm1106.cpp @@ -115,9 +115,7 @@ bool CM1106Component::send_command_expect_ack_(const uint8_t *command, size_t co bool CM1106Component::cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response, size_t response_len) { - // Empty RX Buffer - while (this->available()) - this->read(); + this->discard_available(); this->write_array(command, command_len - 1); this->write_byte(cm1106_checksum(command, command_len)); this->flush(); diff --git a/esphome/components/cse7761/cse7761.cpp b/esphome/components/cse7761/cse7761.cpp index 0c56c741fe6..8ec520b7396 100644 --- a/esphome/components/cse7761/cse7761.cpp +++ b/esphome/components/cse7761/cse7761.cpp @@ -116,9 +116,7 @@ void CSE7761Component::write_(uint8_t reg, uint16_t data) { } bool CSE7761Component::read_once_(uint8_t reg, uint8_t size, uint32_t *value) { - while (this->available()) { - this->read(); - } + this->discard_available(); this->write_(reg, 0); diff --git a/esphome/components/dfrobot_sen0465_uart/dfrobot_sen0465_uart.cpp b/esphome/components/dfrobot_sen0465_uart/dfrobot_sen0465_uart.cpp index ed1c6733abb..08d6024c16b 100644 --- a/esphome/components/dfrobot_sen0465_uart/dfrobot_sen0465_uart.cpp +++ b/esphome/components/dfrobot_sen0465_uart/dfrobot_sen0465_uart.cpp @@ -40,11 +40,7 @@ void DFRobotSEN0465UARTComponent::loop() { bool DFRobotSEN0465UARTComponent::write_frame(const uint8_t *frame) { // Drop anything left over, such as the answer to the mode change or readings sent before passive mode was set - uint8_t byte; - while (this->available() > 0) { - if (!this->read_byte(&byte)) - break; - } + this->discard_available(); this->rx_count_ = 0; this->write_array(frame, FRAME_SIZE); return true; diff --git a/esphome/components/dlms_meter/dlms_meter.cpp b/esphome/components/dlms_meter/dlms_meter.cpp index 32d1817e0e7..7c0ad009205 100644 --- a/esphome/components/dlms_meter/dlms_meter.cpp +++ b/esphome/components/dlms_meter/dlms_meter.cpp @@ -73,7 +73,7 @@ DlmsMeterComponent::DlmsMeterComponent(const uint32_t receive_timeout_ms, const } } -void DlmsMeterComponent::setup() { this->flush_rx_buffer_(); } +void DlmsMeterComponent::setup() { this->discard_available(); } void DlmsMeterComponent::dump_config() { ESP_LOGCONFIG(TAG, "DLMS Meter:"); @@ -122,12 +122,6 @@ void DlmsMeterComponent::loop() { } } -void DlmsMeterComponent::flush_rx_buffer_() { - while (this->available()) { - this->read(); - } -} - void DlmsMeterComponent::read_rx_buffer_() { int available = this->available(); if (available == 0) @@ -137,7 +131,7 @@ void DlmsMeterComponent::read_rx_buffer_() { ESP_LOGW(TAG, "RX Buffer overflow. Frame too large! Dropping frame."); this->bytes_accumulated_ = 0; - this->flush_rx_buffer_(); + this->discard_available(); return; } @@ -145,7 +139,7 @@ void DlmsMeterComponent::read_rx_buffer_() { if (!success) { ESP_LOGW(TAG, "UART read failed. Dropping frame."); this->bytes_accumulated_ = 0; - this->flush_rx_buffer_(); + this->discard_available(); return; } diff --git a/esphome/components/dlms_meter/dlms_meter.h b/esphome/components/dlms_meter/dlms_meter.h index 2d7898f4d3e..9ff2cabf2b5 100644 --- a/esphome/components/dlms_meter/dlms_meter.h +++ b/esphome/components/dlms_meter/dlms_meter.h @@ -104,7 +104,6 @@ class DlmsMeterComponent final : public Component, public uart::UARTDevice { protected: void read_rx_buffer_(); - void flush_rx_buffer_(); void process_frame_(); void on_data_(const dlms_parser::AxdrCapture &capture); diff --git a/esphome/components/fingerprint_grow/fingerprint_grow.cpp b/esphome/components/fingerprint_grow/fingerprint_grow.cpp index 99e88af5b74..3abd1862f34 100644 --- a/esphome/components/fingerprint_grow/fingerprint_grow.cpp +++ b/esphome/components/fingerprint_grow/fingerprint_grow.cpp @@ -362,8 +362,7 @@ void FingerprintGrowComponent::aura_led_control(uint8_t state, uint8_t speed, ui } uint8_t FingerprintGrowComponent::transfer_(std::vector &data_buffer) { - while (this->available()) - this->read(); + this->discard_available(); this->write((uint8_t) (START_CODE >> 8)); this->write((uint8_t) (START_CODE & 0xFF)); this->write(this->address_[0]); diff --git a/esphome/components/hc8/hc8.cpp b/esphome/components/hc8/hc8.cpp index 4fbfbc84d70..2197a086c36 100644 --- a/esphome/components/hc8/hc8.cpp +++ b/esphome/components/hc8/hc8.cpp @@ -18,9 +18,7 @@ void HC8Component::setup() { this->write_array(HC8_COMMAND_GET_PPM); this->flush(); - // ensure the buffer is empty - while (this->available()) - this->read(); + this->discard_available(); } void HC8Component::update() { @@ -36,8 +34,7 @@ void HC8Component::update() { this->status_clear_warning(); } - while (this->available()) - this->read(); + this->discard_available(); this->write_array(HC8_COMMAND_GET_PPM); this->flush(); diff --git a/esphome/components/hlk_fm22x/hlk_fm22x.cpp b/esphome/components/hlk_fm22x/hlk_fm22x.cpp index 8674a1de97c..43311f3d968 100644 --- a/esphome/components/hlk_fm22x/hlk_fm22x.cpp +++ b/esphome/components/hlk_fm22x/hlk_fm22x.cpp @@ -11,9 +11,7 @@ static constexpr uint32_t PAYLOAD_TIMEOUT_MS = 20; void HlkFm22xComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up HLK-FM22X..."); this->set_enrolling_(false); - while (this->available() > 0) { - this->read(); - } + this->discard_available(); this->defer([this]() { this->send_command_(HlkFm22xCommand::GET_STATUS); }); } @@ -85,8 +83,7 @@ void HlkFm22xComponent::send_command_(HlkFm22xCommand command, const uint8_t *da } this->wait_cycles_ = 0; this->active_command_ = command; - while (this->available() > 0) - this->read(); + this->discard_available(); this->write((uint8_t) (START_CODE >> 8)); this->write((uint8_t) (START_CODE & 0xFF)); this->write((uint8_t) command); @@ -141,9 +138,7 @@ void HlkFm22xComponent::recv_command_() { if (millis() - start > PAYLOAD_TIMEOUT_MS) { ESP_LOGE(TAG, "Timeout waiting for payload (%u bytes)", length); // Drain any partial payload bytes to resync the parser - while (this->available() > 0) { - this->read(); - } + this->discard_available(); return; } delay(1); diff --git a/esphome/components/hydreon_rgxx/hydreon_rgxx.cpp b/esphome/components/hydreon_rgxx/hydreon_rgxx.cpp index a1d6553073a..4a479874f53 100644 --- a/esphome/components/hydreon_rgxx/hydreon_rgxx.cpp +++ b/esphome/components/hydreon_rgxx/hydreon_rgxx.cpp @@ -38,9 +38,7 @@ void HydreonRGxxComponent::dump_config() { } void HydreonRGxxComponent::setup() { - while (this->available() != 0) { - this->read(); - } + this->discard_available(); this->schedule_reboot_(); } diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index 0f623bfd726..eda4b4d8e78 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -120,9 +120,7 @@ void MHZ19Component::range_set(MHZ19DetectionRange detection_range) { } bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) { - // Empty RX Buffer - while (this->available()) - this->read(); + this->discard_available(); this->write_array(command, MHZ19_REQUEST_LENGTH); this->write_byte(mhz19_checksum(command)); this->flush(); diff --git a/esphome/components/pylontech/pylontech.cpp b/esphome/components/pylontech/pylontech.cpp index 90cb4a8b855..acc80b0f839 100644 --- a/esphome/components/pylontech/pylontech.cpp +++ b/esphome/components/pylontech/pylontech.cpp @@ -99,11 +99,7 @@ void PylontechComponent::dump_config() { LOG_UPDATE_INTERVAL(this); } -void PylontechComponent::setup() { - while (this->available() != 0) { - this->read(); - } -} +void PylontechComponent::setup() { this->discard_available(); } void PylontechComponent::update() { #ifdef PYLONTECH_CELL_BATTERY_COUNT diff --git a/esphome/components/pzem004t/pzem004t.cpp b/esphome/components/pzem004t/pzem004t.cpp index 2c5e349bd31..c27fb07ce08 100644 --- a/esphome/components/pzem004t/pzem004t.cpp +++ b/esphome/components/pzem004t/pzem004t.cpp @@ -9,9 +9,7 @@ namespace esphome::pzem004t { ESPHOME_LOG_TAG(TAG, "pzem004t"); void PZEM004T::setup() { - // Clear UART buffer - while (this->available()) - this->read(); + this->discard_available(); // Set module address this->write_state_(SET_ADDRESS); } @@ -19,8 +17,7 @@ void PZEM004T::setup() { void PZEM004T::loop() { const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_read_ > 500 && this->available() < 7) { - while (this->available()) - this->read(); + this->discard_available(); this->last_read_ = now; } diff --git a/esphome/components/shelly_dimmer/shelly_dimmer.cpp b/esphome/components/shelly_dimmer/shelly_dimmer.cpp index eedf7ff7ce2..8c44c8c5a08 100644 --- a/esphome/components/shelly_dimmer/shelly_dimmer.cpp +++ b/esphome/components/shelly_dimmer/shelly_dimmer.cpp @@ -483,10 +483,7 @@ void ShellyDimmer::reset_(bool boot0) { // Wait 50ms for the STM32 to reset. delay(50); // NOLINT - // Clear receive buffer. - while (this->available()) { - this->read(); - } + this->discard_available(); this->pin_nrst_->digital_write(true); // Wait 50ms for the STM32 to boot. diff --git a/esphome/components/t6615/t6615.cpp b/esphome/components/t6615/t6615.cpp index d3997528890..6fb58345358 100644 --- a/esphome/components/t6615/t6615.cpp +++ b/esphome/components/t6615/t6615.cpp @@ -32,8 +32,7 @@ void T6615Component::loop() { if (this->available() < 5) { if (this->command_ == T6615Command::GET_PPM && millis() - this->command_time_ > T6615_TIMEOUT) { /* command got eaten, clear the buffer and fire another */ - while (this->available()) - this->read(); + this->discard_available(); this->send_ppm_command_(); } return; @@ -48,9 +47,7 @@ void T6615Component::loop() { if (response_buffer[0] != T6615_MAGIC || response_buffer[1] != T6615_ADDR_HOST) { ESP_LOGW(TAG, "Got bad data from T6615! Magic was %02X and address was %02X", response_buffer[0], response_buffer[1]); - /* make sure the buffer is empty */ - while (this->available()) - this->read(); + this->discard_available(); /* try again to read the sensor */ this->send_ppm_command_(); this->status_set_warning(); diff --git a/esphome/components/tmc22xx/tmc22xx.cpp b/esphome/components/tmc22xx/tmc22xx.cpp index f68496bd0ca..6ee4c9e3ee6 100644 --- a/esphome/components/tmc22xx/tmc22xx.cpp +++ b/esphome/components/tmc22xx/tmc22xx.cpp @@ -446,8 +446,7 @@ bool TMC22XXStepper::write_register(uint8_t reg, uint32_t value) { 0}; datagram[7] = datagram_crc(datagram, 7); - while (this->available()) - this->read(); + this->discard_available(); this->write_array(datagram, DATAGRAM_SIZE); this->flush(); // On the single wire bus every byte sent is also received, drop that echo @@ -475,8 +474,7 @@ optional TMC22XXStepper::read_register(uint8_t reg) { uint8_t request[READ_REQUEST_SIZE] = {SYNC_BYTE, this->address_, reg, 0}; request[3] = datagram_crc(request, 3); - while (this->available()) - this->read(); + this->discard_available(); this->write_array(request, READ_REQUEST_SIZE); this->flush(); uint8_t reply[DATAGRAM_SIZE];