From 78a69cb744a265bcc74a2a86c1492e9683bae92b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 16 Nov 2025 22:33:59 -0600 Subject: [PATCH] tidy --- esphome/components/bh1750/bh1750.cpp | 18 +++++++++--------- esphome/components/bh1750/bh1750.h | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/esphome/components/bh1750/bh1750.cpp b/esphome/components/bh1750/bh1750.cpp index eb91ee0789..a2f94e1b4b 100644 --- a/esphome/components/bh1750/bh1750.cpp +++ b/esphome/components/bh1750/bh1750.cpp @@ -124,9 +124,7 @@ void BH1750Sensor::loop() { case READING_COARSE_RESULT: { float lx; if (!this->read_measurement_(lx)) { - this->status_set_warning(); - this->publish_state(NAN); - this->state_ = IDLE; + this->fail_and_reset_(); break; } @@ -134,9 +132,7 @@ void BH1750Sensor::loop() { // Start fine measurement with optimal settings if (!this->start_measurement_(this->fine_mode_, this->fine_mtreg_, now)) { - this->status_set_warning(); - this->publish_state(NAN); - this->state_ = IDLE; + this->fail_and_reset_(); break; } @@ -153,9 +149,7 @@ void BH1750Sensor::loop() { case READING_FINE_RESULT: { float lx; if (!this->read_measurement_(lx)) { - this->status_set_warning(); - this->publish_state(NAN); - this->state_ = IDLE; + this->fail_and_reset_(); break; } @@ -264,6 +258,12 @@ void BH1750Sensor::process_coarse_result_(float lx) { ESP_LOGV(TAG, "L result: %.1f -> Calculated mode=%d, mtreg=%d", lx, (int) this->fine_mode_, this->fine_mtreg_); } +void BH1750Sensor::fail_and_reset_() { + this->status_set_warning(); + this->publish_state(NAN); + this->state_ = IDLE; +} + float BH1750Sensor::get_setup_priority() const { return setup_priority::DATA; } } // namespace esphome::bh1750 diff --git a/esphome/components/bh1750/bh1750.h b/esphome/components/bh1750/bh1750.h index b9b3fa3f47..0460427954 100644 --- a/esphome/components/bh1750/bh1750.h +++ b/esphome/components/bh1750/bh1750.h @@ -49,6 +49,7 @@ class BH1750Sensor : public sensor::Sensor, public PollingComponent, public i2c: bool start_measurement_(BH1750Mode mode, uint8_t mtreg, uint32_t now); bool read_measurement_(float &lx_out); void process_coarse_result_(float lx); + void fail_and_reset_(); }; } // namespace esphome::bh1750