From c2b2aaddd1e0f024de5c156db9c5c88793c00472 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Mar 2026 12:57:53 -1000 Subject: [PATCH] [sht4x] Move timestamp update after successful command, add heater test Only advance last_heater_millis_ after the heater command succeeds so a failed write retries on the next cycle instead of silently skipping. Add heater settings to the test YAML to ensure CI exercises the heater code path. --- esphome/components/sht4x/sht4x.cpp | 11 ++++++----- tests/components/sht4x/common.yaml | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/esphome/components/sht4x/sht4x.cpp b/esphome/components/sht4x/sht4x.cpp index ff2f3da46a8..e8e84f535d0 100644 --- a/esphome/components/sht4x/sht4x.cpp +++ b/esphome/components/sht4x/sht4x.cpp @@ -86,11 +86,6 @@ void SHT4XComponent::update() { uint32_t now = millis(); if (now - this->last_heater_millis_ >= this->heater_interval_) { use_heater = true; - // Ensure at least one normal measurement between heater cycles by - // pushing the next eligible heater time forward by one update interval. - // The configured duty cycle is a maximum, not an exact target. - uint32_t interval = std::max(this->heater_interval_, this->get_update_interval()); - this->last_heater_millis_ = now - this->heater_interval_ + interval; } } @@ -104,6 +99,12 @@ void SHT4XComponent::update() { this->status_set_warning(LOG_STR("Failed to send heater command")); return; } + // Only update timestamp after successful command. Ensure at least one + // normal measurement between heater cycles by pushing the next eligible + // heater time forward by at least one update interval. + uint32_t now = millis(); + uint32_t interval = std::max(this->heater_interval_, this->get_update_interval()); + this->last_heater_millis_ = now - this->heater_interval_ + interval; return; } diff --git a/tests/components/sht4x/common.yaml b/tests/components/sht4x/common.yaml index 50d5ad8ca46..bec192d6db5 100644 --- a/tests/components/sht4x/common.yaml +++ b/tests/components/sht4x/common.yaml @@ -6,4 +6,8 @@ sensor: humidity: name: SHT4X Humidity address: 0x44 + precision: High + heater_max_duty: 0.02 + heater_power: High + heater_time: Long update_interval: 15s