From 704dd4b445052a5029a264a8f751ddb3f9040edd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Oct 2026 14:17:57 -1000 Subject: [PATCH] [components] Report communication failures with status_comm_result in the remaining components (#21079) --- esphome/components/atm90e32/atm90e32.cpp | 5 +---- esphome/components/bme680/bme680.cpp | 4 +--- esphome/components/bme680_bsec/bme680_bsec.cpp | 6 +----- esphome/components/bme68x_bsec2/bme68x_bsec2.cpp | 6 +----- esphome/components/cm1106/cm1106.cpp | 6 ++---- .../cst226/touchscreen/cst226_touchscreen.cpp | 4 +--- .../cst9220/touchscreen/cst9220_touchscreen.cpp | 4 +--- .../dfrobot_sen0465_base/dfrobot_sen0465_base.cpp | 6 +----- .../components/ds3231/sensor/ds3231_temperature.cpp | 5 +---- .../components/esp32_rmt_led_strip/led_strip.cpp | 5 +---- esphome/components/hc8/hc8.cpp | 5 +---- esphome/components/hdc302x/hdc302x.cpp | 7 ++----- .../hoermann_hcp/cover/hoermann_hcp_cover.cpp | 4 +--- .../hoermann_hcp/light/hoermann_hcp_light.cpp | 7 ++----- esphome/components/husb238/husb238.cpp | 5 +---- esphome/components/hx711/hx711.cpp | 5 +---- esphome/components/ina2xx_base/ina2xx_base.cpp | 6 +----- esphome/components/lis2dw12/lis2dw12.cpp | 13 +++---------- esphome/components/lp5562/lp5562_output.cpp | 6 +----- .../binary_sensor/m5stack_8angle_binary_sensor.cpp | 5 +---- .../m5stack_8angle/sensor/m5stack_8angle_sensor.cpp | 5 +---- esphome/components/max6675/max6675.cpp | 6 ++---- esphome/components/mcp3221/mcp3221_sensor.cpp | 4 +--- esphome/components/mcp9808/mcp9808.cpp | 5 +---- esphome/components/mhz19/mhz19.cpp | 4 +--- esphome/components/mics_4514/mics_4514.cpp | 5 +---- esphome/components/mpr121/mpr121.cpp | 8 +------- esphome/components/nau7802/nau7802.cpp | 5 +---- esphome/components/opt3001/opt3001.cpp | 4 +--- esphome/components/pca6416a/pca6416a.cpp | 10 ++-------- esphome/components/pca9554/pca9554.cpp | 9 ++------- esphome/components/pcf8574/pcf8574.cpp | 13 ++----------- esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp | 12 ++++-------- esphome/components/pn532/pn532.cpp | 13 ++++++------- esphome/components/qmp6988/qmp6988.cpp | 4 +--- .../remote_transmitter/remote_transmitter_rmt.cpp | 4 +--- esphome/components/rx8025t/rx8025t.cpp | 4 +--- esphome/components/rx8111/rx8111.cpp | 6 ++---- esphome/components/selec_meter/selec_meter.cpp | 6 +----- esphome/components/sen21231/sen21231.cpp | 4 +--- esphome/components/sen6x/sen6x.cpp | 4 +--- esphome/components/senseair/senseair.cpp | 4 +--- esphome/components/sht4x/sht4x.cpp | 5 +---- esphome/components/spd2010/spd2010_touchscreen.cpp | 8 +++----- .../st7123/touchscreen/st7123_touchscreen.cpp | 4 +--- esphome/components/sts3x/sts3x.cpp | 5 +---- esphome/components/sy6970/sy6970.cpp | 5 +---- esphome/components/t6615/t6615.cpp | 5 +---- esphome/components/tca8418/tca8418.cpp | 5 +---- esphome/components/tmc22xx/tmc22xx.cpp | 9 +++------ esphome/components/tmp1075/tmp1075.cpp | 4 +--- esphome/components/tsl2561/tsl2561.cpp | 5 +---- esphome/components/ttp229_lsf/ttp229_lsf.cpp | 5 +---- esphome/components/veml3235/veml3235.cpp | 5 +---- 54 files changed, 76 insertions(+), 242 deletions(-) diff --git a/esphome/components/atm90e32/atm90e32.cpp b/esphome/components/atm90e32/atm90e32.cpp index 0cdf9918db9..9fb32e2f021 100644 --- a/esphome/components/atm90e32/atm90e32.cpp +++ b/esphome/components/atm90e32/atm90e32.cpp @@ -117,12 +117,9 @@ void ATM90E32Component::loop() { } void ATM90E32Component::update() { - if (this->read16_(ATM90E32_REGISTER_METEREN) != 1) { - this->status_set_warning(); + if (!this->status_comm_result(this->read16_(ATM90E32_REGISTER_METEREN) == 1, nullptr)) return; - } this->set_publish_interval_flag_(true); - this->status_clear_warning(); #ifdef USE_TEXT_SENSOR this->check_phase_status(); diff --git a/esphome/components/bme680/bme680.cpp b/esphome/components/bme680/bme680.cpp index e3297d38f55..ac5ce3287d8 100644 --- a/esphome/components/bme680/bme680.cpp +++ b/esphome/components/bme680/bme680.cpp @@ -284,7 +284,7 @@ uint8_t BME680Component::calc_heater_duration_(uint16_t duration) { } void BME680Component::read_data_() { uint8_t data[15]; - if (!this->read_bytes(BME680_REGISTER_FIELD0, data, 15)) { + if (!this->status_comm_result(this->read_bytes(BME680_REGISTER_FIELD0, data, 15))) { if (this->temperature_sensor_ != nullptr) this->temperature_sensor_->publish_state(NAN); if (this->pressure_sensor_ != nullptr) @@ -294,10 +294,8 @@ void BME680Component::read_data_() { if (this->gas_resistance_sensor_ != nullptr) this->gas_resistance_sensor_->publish_state(NAN); ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL); - this->status_set_warning(); return; } - this->status_clear_warning(); uint32_t raw_temperature = (uint32_t(data[5]) << 12) | (uint32_t(data[6]) << 4) | (uint32_t(data[7]) >> 4); uint32_t raw_pressure = (uint32_t(data[2]) << 12) | (uint32_t(data[3]) << 4) | (uint32_t(data[4]) >> 4); diff --git a/esphome/components/bme680_bsec/bme680_bsec.cpp b/esphome/components/bme680_bsec/bme680_bsec.cpp index 3f0082e1af7..fe24ba88754 100644 --- a/esphome/components/bme680_bsec/bme680_bsec.cpp +++ b/esphome/components/bme680_bsec/bme680_bsec.cpp @@ -191,11 +191,7 @@ void BME680BSECComponent::loop() { } else { this->status_clear_error(); } - if (this->bsec_status_ > BSEC_OK || this->bme680_status_ > BME680_OK) { - this->status_set_warning(); - } else { - this->status_clear_warning(); - } + this->status_comm_result(this->bsec_status_ <= BSEC_OK && this->bme680_status_ <= BME680_OK, nullptr); // Process a single action from the queue. These are primarily sensor state publishes // that in totality take too long to send in a single call. diff --git a/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp b/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp index c563e120842..1be5c3835e4 100644 --- a/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp +++ b/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp @@ -126,11 +126,7 @@ void BME68xBSEC2Component::check_status_() { this->status_clear_error(); } const bool has_bme68x_warning = this->bme68x_status_ > BME68X_OK && !is_no_new_data_warning(this->bme68x_status_); - if (this->bsec_status_ > BSEC_OK || has_bme68x_warning) { - this->status_set_warning(); - } else { - this->status_clear_warning(); - } + this->status_comm_result(this->bsec_status_ <= BSEC_OK && !has_bme68x_warning, nullptr); } void BME68xBSEC2Component::loop() { diff --git a/esphome/components/cm1106/cm1106.cpp b/esphome/components/cm1106/cm1106.cpp index 8739e6c0805..1b51030b162 100644 --- a/esphome/components/cm1106/cm1106.cpp +++ b/esphome/components/cm1106/cm1106.cpp @@ -101,15 +101,13 @@ bool CM1106Component::send_command_expect_ack_(const uint8_t *command, size_t co uint8_t response[4] = {0}; if (!this->cm1106_write_command_(command, command_len, response, sizeof(response))) { ESP_LOGW(TAG, "Command %02X failed", command[2]); - this->status_set_warning(); + this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return false; } - if (memcmp(response, ack, sizeof(response)) != 0) { + if (!this->status_comm_result(memcmp(response, ack, sizeof(response)) == 0)) { ESP_LOGW(TAG, "Unexpected response: %02X %02X %02X %02X", response[0], response[1], response[2], response[3]); - this->status_set_warning(); return false; } - this->status_clear_warning(); return true; } diff --git a/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp b/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp index 849cff4a49e..7f98051d2bc 100644 --- a/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp +++ b/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp @@ -20,12 +20,10 @@ void CST226Touchscreen::setup() { void CST226Touchscreen::update_touches() { uint8_t data[28]; - if (!this->read_bytes(CST226_REG_STATUS, data, sizeof data)) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_bytes(CST226_REG_STATUS, data, sizeof data))) { this->skip_update_ = true; return; } - this->status_clear_warning(); if (data[0] == 0x83 && data[1] == 0x17 && data[5] == 0x80) { this->update_button_state_(true); return; diff --git a/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp b/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp index 72803804335..6d869ec5ea7 100644 --- a/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp +++ b/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp @@ -87,12 +87,10 @@ void CST9220Touchscreen::update_touches() { // Only an actual I2C failure should skip the update; a successful read with no // touches is a real "all fingers lifted" state that must flow through so the // base class can generate the release event. - if (this->read_register16(REG_TOUCH_DATA, data, sizeof(data)) != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_register16(REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) { this->skip_update_ = true; return; } - this->status_clear_warning(); // Acknowledge the report so the controller can prepare the next one. uint8_t ack = TOUCH_ACK; diff --git a/esphome/components/dfrobot_sen0465_base/dfrobot_sen0465_base.cpp b/esphome/components/dfrobot_sen0465_base/dfrobot_sen0465_base.cpp index 78267d5ff9e..5fffd71fe91 100644 --- a/esphome/components/dfrobot_sen0465_base/dfrobot_sen0465_base.cpp +++ b/esphome/components/dfrobot_sen0465_base/dfrobot_sen0465_base.cpp @@ -255,11 +255,7 @@ void DFRobotSEN0465Component::handle_data_(const uint8_t *frame) { if (this->concentration_sensor_ != nullptr) this->concentration_sensor_->publish_state(concentration); - if (std::isnan(concentration) || std::isnan(temperature) || this->setup_incomplete_) { - this->status_set_warning(); - } else { - this->status_clear_warning(); - } + this->status_comm_result(!std::isnan(concentration) && !std::isnan(temperature) && !this->setup_incomplete_, nullptr); } #ifdef USE_DFROBOT_SEN0465_TEMPERATURE_COMPENSATION diff --git a/esphome/components/ds3231/sensor/ds3231_temperature.cpp b/esphome/components/ds3231/sensor/ds3231_temperature.cpp index 8d7e1156be4..569f24cab5e 100644 --- a/esphome/components/ds3231/sensor/ds3231_temperature.cpp +++ b/esphome/components/ds3231/sensor/ds3231_temperature.cpp @@ -7,11 +7,8 @@ ESPHOME_LOG_TAG(TAG, "ds3231.sensor"); void DS3231TemperatureSensor::update() { float temperature; - if (!this->parent_->read_temperature(temperature)) { - this->status_set_warning(); + if (!this->status_comm_result(this->parent_->read_temperature(temperature))) return; - } - this->status_clear_warning(); this->publish_state(temperature); } diff --git a/esphome/components/esp32_rmt_led_strip/led_strip.cpp b/esphome/components/esp32_rmt_led_strip/led_strip.cpp index 420ff64ddc6..24f3a77a7ce 100644 --- a/esphome/components/esp32_rmt_led_strip/led_strip.cpp +++ b/esphome/components/esp32_rmt_led_strip/led_strip.cpp @@ -212,12 +212,9 @@ void ESP32RMTLEDStripLightOutput::write_state(light::LightState *state) { #else error = rmt_transmit(this->channel_, this->encoder_, this->rmt_buf_, len * sizeof(rmt_symbol_word_t), &config); #endif - if (error != ESP_OK) { + if (!this->status_comm_result(error == ESP_OK, nullptr)) { ESP_LOGE(TAG, "RMT TX error"); - this->status_set_warning(); - return; } - this->status_clear_warning(); } light::ESPColorView ESP32RMTLEDStripLightOutput::get_view_internal(int32_t index) const { diff --git a/esphome/components/hc8/hc8.cpp b/esphome/components/hc8/hc8.cpp index 2197a086c36..734e133336f 100644 --- a/esphome/components/hc8/hc8.cpp +++ b/esphome/components/hc8/hc8.cpp @@ -55,14 +55,11 @@ void HC8Component::update() { return; } - if (crc16(response.data(), 12) != encode_uint16(response[13], response[12])) { + if (!this->status_comm_result(crc16(response.data(), 12) == encode_uint16(response[13], response[12]))) { ESP_LOGW(TAG, "HC8 Checksum mismatch"); - this->status_set_warning(); return; } - this->status_clear_warning(); - const uint16_t ppm = encode_uint16(response[5], response[4]); ESP_LOGD(TAG, "HC8 Received CO₂=%uppm", ppm); if (this->co2_sensor_ != nullptr) diff --git a/esphome/components/hdc302x/hdc302x.cpp b/esphome/components/hdc302x/hdc302x.cpp index d5428486579..ffe03190b3e 100644 --- a/esphome/components/hdc302x/hdc302x.cpp +++ b/esphome/components/hdc302x/hdc302x.cpp @@ -133,12 +133,9 @@ void HDC302XComponent::read_data_() { } // Check checksums - if (crc8(buf, 2, 0xff, 0x31, true) != buf[2] || crc8(buf + 3, 2, 0xff, 0x31, true) != buf[5]) { - this->status_set_warning(LOG_STR("Read data: invalid CRC")); + const bool crc_ok = crc8(buf, 2, 0xff, 0x31, true) == buf[2] && crc8(buf + 3, 2, 0xff, 0x31, true) == buf[5]; + if (!this->status_comm_result(crc_ok, LOG_STR("Read data: invalid CRC"))) return; - } - - this->status_clear_warning(); if (this->temp_sensor_ != nullptr) { uint16_t raw_t = encode_uint16(buf[0], buf[1]); diff --git a/esphome/components/hoermann_hcp/cover/hoermann_hcp_cover.cpp b/esphome/components/hoermann_hcp/cover/hoermann_hcp_cover.cpp index ab2afc74d93..d4fc2e9b689 100644 --- a/esphome/components/hoermann_hcp/cover/hoermann_hcp_cover.cpp +++ b/esphome/components/hoermann_hcp/cover/hoermann_hcp_cover.cpp @@ -39,8 +39,7 @@ void HoermannHcpCover::control(const cover::CoverCall &call) { } void HoermannHcpCover::update_from_state_() { - if (!this->parent_->is_valid()) { - this->status_set_warning(); + if (!this->status_comm_result(this->parent_->is_valid(), LOG_STR("bus controller not responding"))) { // The door can now move unheard, so drop the baseline a direction would be inferred from and stop // reporting motion instead of leaving the cover travelling until the controller returns. this->previous_position_ = NAN; @@ -50,7 +49,6 @@ void HoermannHcpCover::update_from_state_() { } return; } - this->status_clear_warning(); const auto previous_operation = this->current_operation; const float current_position = this->parent_->get_current_position(); diff --git a/esphome/components/hoermann_hcp/light/hoermann_hcp_light.cpp b/esphome/components/hoermann_hcp/light/hoermann_hcp_light.cpp index 6195c5d63d8..6a3028ad2fe 100644 --- a/esphome/components/hoermann_hcp/light/hoermann_hcp_light.cpp +++ b/esphome/components/hoermann_hcp/light/hoermann_hcp_light.cpp @@ -53,12 +53,9 @@ void HoermannHcpLight::update_from_state_() { this->status_set_warning(LOG_STR("bus controller not responding")); return; } - if (!this->parent_->is_light_known()) { - // Commands are refused until the door says, so say so rather than looking healthy and doing nothing. - this->status_set_warning(LOG_STR("door has not reported the lamp")); + // Commands are refused until the door says, so say so rather than looking healthy and doing nothing. + if (!this->status_comm_result(this->parent_->is_light_known(), LOG_STR("door has not reported the lamp"))) return; - } - this->status_clear_warning(); const bool heading_on = this->parent_->is_light_heading_on(); if (this->light_state_->remote_values.is_on() != heading_on) this->publish_lamp_state_(heading_on); diff --git a/esphome/components/husb238/husb238.cpp b/esphome/components/husb238/husb238.cpp index d0f9973b2a4..3d39b948b38 100644 --- a/esphome/components/husb238/husb238.cpp +++ b/esphome/components/husb238/husb238.cpp @@ -14,11 +14,8 @@ void Husb238Component::setup() { void Husb238Component::update() { uint8_t status; - if (!this->read_byte(REG_PD_STATUS1, &status)) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_byte(REG_PD_STATUS1, &status))) return; - } - this->status_clear_warning(); #ifdef USE_BINARY_SENSOR if (this->attached_binary_sensor_ != nullptr) { diff --git a/esphome/components/hx711/hx711.cpp b/esphome/components/hx711/hx711.cpp index 9a81befcc45..c6bf03b72e1 100644 --- a/esphome/components/hx711/hx711.cpp +++ b/esphome/components/hx711/hx711.cpp @@ -59,14 +59,11 @@ bool HX711Sensor::read_sensor_(uint32_t *result) { final_dout = this->dout_pin_->digital_read(); } - if (!final_dout) { + if (!this->status_comm_result(final_dout)) { ESP_LOGW(TAG, "HX711 DOUT pin not high after reading (data 0x%" PRIx32 ")!", data); - this->status_set_warning(); return false; } - this->status_clear_warning(); - data = static_cast(sign_extend<24>(data)); if (result != nullptr) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 16b37568d91..97f7f211370 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -172,11 +172,7 @@ void INA2XX::loop() { break; case State::DATA_COLLECTION_8: - if (this->full_loop_is_okay_) { - this->status_clear_warning(); - } else { - this->status_set_warning(); - } + this->status_comm_result(this->full_loop_is_okay_); this->state_ = State::IDLE; break; diff --git a/esphome/components/lis2dw12/lis2dw12.cpp b/esphome/components/lis2dw12/lis2dw12.cpp index 7af60be87a4..949ebe14509 100644 --- a/esphome/components/lis2dw12/lis2dw12.cpp +++ b/esphome/components/lis2dw12/lis2dw12.cpp @@ -291,11 +291,8 @@ void LIS2DW12Component::loop() { // SIXD_SRC and ALL_INT_SRC are adjacent, so one read captures both before any latch clears uint8_t regs[2]; - if (!this->read_bytes(static_cast(RegisterMap::SIXD_SRC), regs, sizeof(regs))) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_bytes(static_cast(RegisterMap::SIXD_SRC), regs, sizeof(regs)))) return; - } - this->status_clear_warning(); RegSixdSrc sixd; sixd.raw = regs[0]; RegAllIntSrc src; @@ -315,11 +312,9 @@ void LIS2DW12Component::loop() { } void LIS2DW12Component::update() { - float x, y, z; - if (!this->read_acceleration_data_(x, y, z)) { - this->status_set_warning(); + float x{}, y{}, z{}; + if (!this->status_comm_result(this->read_acceleration_data_(x, y, z))) return; - } ESP_LOGV(TAG, "Acceleration: {x = %+1.3f m/s², y = %+1.3f m/s², z = %+1.3f m/s²}", x, y, z); @@ -331,8 +326,6 @@ void LIS2DW12Component::update() { if (this->acceleration_z_sensor_ != nullptr) this->acceleration_z_sensor_->publish_state(z); #endif - - this->status_clear_warning(); } void LIS2DW12Component::set_offset(float offset_x, float offset_y, float offset_z) { diff --git a/esphome/components/lp5562/lp5562_output.cpp b/esphome/components/lp5562/lp5562_output.cpp index da392ea5c04..2010f93f0c3 100644 --- a/esphome/components/lp5562/lp5562_output.cpp +++ b/esphome/components/lp5562/lp5562_output.cpp @@ -73,11 +73,7 @@ void LP5562Output::dump_config() { void LP5562Output::set_channel_value_(uint8_t channel, uint8_t value) { if (this->is_failed()) return; - if (!this->write_byte(progmem_read_byte(&LP5562_REG_PWM[channel]), value)) { - this->status_set_warning(); - return; - } - this->status_clear_warning(); + this->status_comm_result(this->write_byte(progmem_read_byte(&LP5562_REG_PWM[channel]), value)); } void LP5562Channel::write_state(float state) { diff --git a/esphome/components/m5stack_8angle/binary_sensor/m5stack_8angle_binary_sensor.cpp b/esphome/components/m5stack_8angle/binary_sensor/m5stack_8angle_binary_sensor.cpp index 680b3bcd9e7..0eda7d9864d 100644 --- a/esphome/components/m5stack_8angle/binary_sensor/m5stack_8angle_binary_sensor.cpp +++ b/esphome/components/m5stack_8angle/binary_sensor/m5stack_8angle_binary_sensor.cpp @@ -4,12 +4,9 @@ namespace esphome::m5stack_8angle { void M5Stack8AngleSwitchBinarySensor::update() { int8_t out = this->parent_->read_switch(); - if (out == -1) { - this->status_set_warning(LOG_STR("Could not read binary sensor state from M5Stack 8Angle.")); + if (!this->status_comm_result(out != -1, LOG_STR("Could not read binary sensor state from M5Stack 8Angle."))) return; - } this->publish_state(out != 0); - this->status_clear_warning(); } } // namespace esphome::m5stack_8angle diff --git a/esphome/components/m5stack_8angle/sensor/m5stack_8angle_sensor.cpp b/esphome/components/m5stack_8angle/sensor/m5stack_8angle_sensor.cpp index b05e1e68163..24a40dd9d4d 100644 --- a/esphome/components/m5stack_8angle/sensor/m5stack_8angle_sensor.cpp +++ b/esphome/components/m5stack_8angle/sensor/m5stack_8angle_sensor.cpp @@ -5,17 +5,14 @@ namespace esphome::m5stack_8angle { void M5Stack8AngleKnobSensor::update() { if (this->parent_ != nullptr) { int32_t raw_pos = this->parent_->read_knob_pos_raw(this->channel_, this->bits_); - if (raw_pos == -1) { - this->status_set_warning(LOG_STR("Could not read knob position from M5Stack 8Angle.")); + if (!this->status_comm_result(raw_pos != -1, LOG_STR("Could not read knob position from M5Stack 8Angle."))) return; - } if (this->raw_) { this->publish_state(raw_pos); } else { float knob_pos = (float) raw_pos / ((1 << this->bits_) - 1); this->publish_state(knob_pos); } - this->status_clear_warning(); }; } diff --git a/esphome/components/max6675/max6675.cpp b/esphome/components/max6675/max6675.cpp index 2fc8dbb73d7..14eaca89164 100644 --- a/esphome/components/max6675/max6675.cpp +++ b/esphome/components/max6675/max6675.cpp @@ -31,17 +31,15 @@ void MAX6675Sensor::read_data_() { uint16_t val = data[1] | (uint16_t(data[0]) << 8); this->disable(); - if ((val & 0x04) != 0) { - // Thermocouple open + // Bit 2 set means the thermocouple is open + if (!this->status_comm_result((val & 0x04) == 0, nullptr)) { ESP_LOGW(TAG, "Got invalid value from MAX6675Sensor (0x%04X)", val); - this->status_set_warning(); return; } float temperature = float(val >> 3) / 4.0f; ESP_LOGD(TAG, "'%s': Got temperature=%.1f°C", this->name_.c_str(), temperature); this->publish_state(temperature); - this->status_clear_warning(); } } // namespace esphome::max6675 diff --git a/esphome/components/mcp3221/mcp3221_sensor.cpp b/esphome/components/mcp3221/mcp3221_sensor.cpp index d38883c3421..94e7174baa7 100644 --- a/esphome/components/mcp3221/mcp3221_sensor.cpp +++ b/esphome/components/mcp3221/mcp3221_sensor.cpp @@ -8,12 +8,10 @@ ESPHOME_LOG_TAG(TAG, "mcp3221"); float MCP3221Sensor::sample() { uint8_t data[2]; - if (this->read(data, 2) != i2c::ERROR_OK) { + if (!this->status_comm_result(this->read(data, 2) == i2c::ERROR_OK)) { ESP_LOGW(TAG, "Read failed"); - this->status_set_warning(); return NAN; } - this->status_clear_warning(); uint16_t value = encode_uint16(data[0], data[1]); float voltage = value * this->reference_voltage_ / 4096.0f; diff --git a/esphome/components/mcp9808/mcp9808.cpp b/esphome/components/mcp9808/mcp9808.cpp index 309651970ca..2c96b4119d3 100644 --- a/esphome/components/mcp9808/mcp9808.cpp +++ b/esphome/components/mcp9808/mcp9808.cpp @@ -68,14 +68,11 @@ void MCP9808Sensor::update() { temp = (uint16_t) (msb) *16 + lsb / 16.0f; } - if (std::isnan(temp)) { - this->status_set_warning(); + if (!this->status_comm_result(!std::isnan(temp), nullptr)) return; - } ESP_LOGD(TAG, "%s: Got temperature=%.4f°C", this->name_.c_str(), temp); this->publish_state(temp); - this->status_clear_warning(); } } // namespace esphome::mcp9808 diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index eda4b4d8e78..3eb71c1adad 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -66,13 +66,11 @@ void MHZ19Component::update() { } uint8_t checksum = mhz19_checksum(response); - if (response[8] != checksum) { + if (!this->status_comm_result(response[8] == checksum)) { ESP_LOGW(TAG, "MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum); - this->status_set_warning(); return; } - this->status_clear_warning(); const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3]; const int temp = int(response[4]) - 40; const uint8_t status = response[5]; diff --git a/esphome/components/mics_4514/mics_4514.cpp b/esphome/components/mics_4514/mics_4514.cpp index 957cdf90ef2..c42ca7ab465 100644 --- a/esphome/components/mics_4514/mics_4514.cpp +++ b/esphome/components/mics_4514/mics_4514.cpp @@ -43,11 +43,8 @@ void MICS4514Component::update() { return; } uint8_t data[6]; - if (this->read_register(SENSOR_REGISTER, data, 6) != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_register(SENSOR_REGISTER, data, 6) == i2c::ERROR_OK)) return; - } - this->status_clear_warning(); ESP_LOGV(TAG, "Got data: %02X %02X %02X %02X %02X %02X", data[0], data[1], data[2], data[3], data[4], data[5]); uint16_t ox = encode_uint16(data[0], data[1]); uint16_t red = encode_uint16(data[2], data[3]); diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index bbcee223e8c..781028e7f5c 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -112,13 +112,7 @@ bool MPR121Component::flush_gpio_() { this->write_byte(MPR121_GPIOEN, this->gpio_enable_); this->write_byte(MPR121_GPIODIR, this->gpio_direction_); - if (!this->write_byte(MPR121_GPIODATA, this->gpio_output_)) { - this->status_set_warning(); - return false; - } - - this->status_clear_warning(); - return true; + return this->status_comm_result(this->write_byte(MPR121_GPIODATA, this->gpio_output_)); } void MPR121GPIOPin::setup() { this->pin_mode(this->flags_); } diff --git a/esphome/components/nau7802/nau7802.cpp b/esphome/components/nau7802/nau7802.cpp index 8c0acbbe434..813321fac9e 100644 --- a/esphome/components/nau7802/nau7802.cpp +++ b/esphome/components/nau7802/nau7802.cpp @@ -289,14 +289,11 @@ void NAU7802Sensor::loop() { } void NAU7802Sensor::update() { - if (!this->is_data_ready_()) { + if (!this->status_comm_result(this->is_data_ready_(), nullptr)) { ESP_LOGW(TAG, "No measurements ready!"); - this->status_set_warning(); return; } - this->status_clear_warning(); - // Get the most recent sample to publish int32_t result = this->read_value_(ADCO_B2_REG, 3); diff --git a/esphome/components/opt3001/opt3001.cpp b/esphome/components/opt3001/opt3001.cpp index 374fd707561..caf7decde94 100644 --- a/esphome/components/opt3001/opt3001.cpp +++ b/esphome/components/opt3001/opt3001.cpp @@ -104,13 +104,11 @@ void OPT3001Sensor::update() { this->read_lx_([this](float val) { this->updating_ = false; - if (std::isnan(val)) { - this->status_set_warning(); + if (!this->status_comm_result(!std::isnan(val))) { this->publish_state(NAN); return; } ESP_LOGD(TAG, "'%s': Illuminance=%.1flx", this->get_name().c_str(), val); - this->status_clear_warning(); this->publish_state(val); }); } diff --git a/esphome/components/pca6416a/pca6416a.cpp b/esphome/components/pca6416a/pca6416a.cpp index 0825d6eac98..5f5b4ea4a10 100644 --- a/esphome/components/pca6416a/pca6416a.cpp +++ b/esphome/components/pca6416a/pca6416a.cpp @@ -138,13 +138,10 @@ bool PCA6416AComponent::read_register_(uint8_t reg, uint8_t *value) { } this->last_error_ = this->read_register(reg, value, 1); - if (this->last_error_ != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->last_error_ == i2c::ERROR_OK)) { ESP_LOGE(TAG, "read_register_(): I2C I/O error: %d", (int) this->last_error_); return false; } - - this->status_clear_warning(); return true; } @@ -155,13 +152,10 @@ bool PCA6416AComponent::write_register_(uint8_t reg, uint8_t value) { } this->last_error_ = this->write_register(reg, &value, 1); - if (this->last_error_ != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->last_error_ == i2c::ERROR_OK)) { ESP_LOGE(TAG, "write_register_(): I2C I/O error: %d", (int) this->last_error_); return false; } - - this->status_clear_warning(); return true; } diff --git a/esphome/components/pca9554/pca9554.cpp b/esphome/components/pca9554/pca9554.cpp index a12d291f6b2..4f44143c547 100644 --- a/esphome/components/pca9554/pca9554.cpp +++ b/esphome/components/pca9554/pca9554.cpp @@ -110,12 +110,10 @@ bool PCA9554Component::read_inputs_() { } this->last_error_ = this->read_register(INPUT_REG * this->reg_width_, inputs, this->reg_width_); - if (this->last_error_ != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->last_error_ == i2c::ERROR_OK)) { ESP_LOGE(TAG, "read_register_(): I2C I/O error: %d", (int) this->last_error_); return false; } - this->status_clear_warning(); this->input_mask_ = inputs[0]; if (this->reg_width_ == 2) { this->input_mask_ |= inputs[1] << 8; @@ -128,13 +126,10 @@ bool PCA9554Component::write_register_(uint8_t reg, uint16_t value) { outputs[0] = (uint8_t) value; outputs[1] = (uint8_t) (value >> 8); this->last_error_ = this->write_register(reg * this->reg_width_, outputs, this->reg_width_); - if (this->last_error_ != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->last_error_ == i2c::ERROR_OK)) { ESP_LOGE(TAG, "write_register_(): I2C I/O error: %d", (int) this->last_error_); return false; } - - this->status_clear_warning(); return true; } diff --git a/esphome/components/pcf8574/pcf8574.cpp b/esphome/components/pcf8574/pcf8574.cpp index 70068ea84da..5674b6a53e8 100644 --- a/esphome/components/pcf8574/pcf8574.cpp +++ b/esphome/components/pcf8574/pcf8574.cpp @@ -87,12 +87,9 @@ bool PCF8574Component::read_gpio_() { if (this->is_failed()) return false; uint8_t data[2]{}; // Keep the bytes zero for the PCF8574's one-byte read. - if (!this->read_bytes_raw(data, this->pcf8575_ ? 2 : 1)) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_bytes_raw(data, this->pcf8575_ ? 2 : 1))) return false; - } this->input_mask_ = encode_value_le(data); - this->status_clear_warning(); return true; } bool PCF8574Component::write_gpio_() { @@ -108,13 +105,7 @@ bool PCF8574Component::write_gpio_() { uint8_t data[2]; data[0] = value; data[1] = value >> 8; - if (this->write(data, this->pcf8575_ ? 2 : 1) != i2c::ERROR_OK) { - this->status_set_warning(); - return false; - } - - this->status_clear_warning(); - return true; + return this->status_comm_result(this->write(data, this->pcf8575_ ? 2 : 1) == i2c::ERROR_OK); } float PCF8574Component::get_setup_priority() const { return setup_priority::IO; } diff --git a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp index 0b5af94e696..28f98325ea4 100644 --- a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp +++ b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp @@ -105,15 +105,13 @@ bool PI4IOE5V6408Component::read_gpio_modes_() { return false; uint8_t data; - if (!this->read_byte(PI4IOE5V6408_REGISTER_IO_DIR, &data)) { - this->status_set_warning(LOG_STR("Failed to read GPIO modes")); + if (!this->status_comm_result(this->read_byte(PI4IOE5V6408_REGISTER_IO_DIR, &data), + LOG_STR("Failed to read GPIO modes"))) return false; - } #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE ESP_LOGV(TAG, "Read GPIO modes: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(data)); #endif this->mode_mask_ = data; - this->status_clear_warning(); return true; } @@ -140,14 +138,12 @@ void PI4IOE5V6408Component::digital_write_hw(uint8_t pin, bool value) { } else { this->output_mask_ &= ~(1 << pin); } - if (!this->write_byte(PI4IOE5V6408_REGISTER_OUT_SET, this->output_mask_)) { - this->status_set_warning(LOG_STR("Failed to write output register")); + if (!this->status_comm_result(this->write_byte(PI4IOE5V6408_REGISTER_OUT_SET, this->output_mask_), + LOG_STR("Failed to write output register"))) return; - } #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE ESP_LOGV(TAG, "Wrote GPIO output: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(this->output_mask_)); #endif - this->status_clear_warning(); } bool PI4IOE5V6408Component::write_gpio_modes_() { diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index fd38edf136a..c83726949d9 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -106,16 +106,15 @@ void PN532::update() { obj->on_scan_end(); #endif - if (!this->write_command_({ - PN532_COMMAND_INLISTPASSIVETARGET, - 0x01, // max 1 card - 0x00, // baud rate ISO14443A (106 kbit/s) - })) { + const bool ok = this->write_command_({ + PN532_COMMAND_INLISTPASSIVETARGET, + 0x01, // max 1 card + 0x00, // baud rate ISO14443A (106 kbit/s) + }); + if (!this->status_comm_result(ok)) { ESP_LOGW(TAG, "Requesting tag read failed!"); - this->status_set_warning(); return; } - this->status_clear_warning(); this->requested_read_ = true; } diff --git a/esphome/components/qmp6988/qmp6988.cpp b/esphome/components/qmp6988/qmp6988.cpp index 9dce402fe02..83344de479d 100644 --- a/esphome/components/qmp6988/qmp6988.cpp +++ b/esphome/components/qmp6988/qmp6988.cpp @@ -288,12 +288,10 @@ bool QMP6988Component::calculate_pressure_() { int32_t t_int, p_int; err = this->read_register(QMP6988_PRESSURE_MSB_REG, a_data_uint8_tr, 6); - if (err != i2c::ERROR_OK) { + if (!this->status_comm_result(err == i2c::ERROR_OK)) { ESP_LOGE(TAG, "Error reading raw pressure/temp values"); - this->status_set_warning(); return false; } - this->status_clear_warning(); p_read = encode_uint24(a_data_uint8_tr[0], a_data_uint8_tr[1], a_data_uint8_tr[2]); p_raw = (int32_t) (p_read - SUBTRACTOR); diff --git a/esphome/components/remote_transmitter/remote_transmitter_rmt.cpp b/esphome/components/remote_transmitter/remote_transmitter_rmt.cpp index 22a63ca4fd3..18ad08ab6d2 100644 --- a/esphome/components/remote_transmitter/remote_transmitter_rmt.cpp +++ b/esphome/components/remote_transmitter/remote_transmitter_rmt.cpp @@ -284,14 +284,12 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen this->store_.index = offset; esp_err_t error = rmt_transmit(this->channel_, this->encoder_, this->rmt_temp_.data(), this->rmt_temp_.size() * sizeof(rmt_symbol_half_t), &config); - if (error != ESP_OK) { + if (!this->status_comm_result(error == ESP_OK, nullptr)) { ESP_LOGW(TAG, "rmt_transmit failed: %s", esp_err_to_name(error)); - this->status_set_warning(); // nothing was queued, so there is no frame to wait for this->fire_complete_(false); return; } - this->status_clear_warning(); if (this->non_blocking_) { this->set_timeout(COMPLETE_TIMEOUT_ID, total_duration / 1000, [this]() { this->wait_for_rmt_(); }); diff --git a/esphome/components/rx8025t/rx8025t.cpp b/esphome/components/rx8025t/rx8025t.cpp index c94c64f8611..f33d39b8d0c 100644 --- a/esphome/components/rx8025t/rx8025t.cpp +++ b/esphome/components/rx8025t/rx8025t.cpp @@ -52,12 +52,10 @@ void RX8025TComponent::read_time() { } uint8_t date[7]; - if (!this->read_bytes(RX8025T_REG_SEC, date, sizeof(date))) { + if (!this->status_comm_result(this->read_bytes(RX8025T_REG_SEC, date, sizeof(date)))) { ESP_LOGE(TAG, "Can't read I2C data."); - this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } - this->status_clear_warning(); if (flags & RX8025T_FLAG_VLF) { ESP_LOGW(TAG, "VLF flag is set - time data may be invalid, not syncing to system clock."); diff --git a/esphome/components/rx8111/rx8111.cpp b/esphome/components/rx8111/rx8111.cpp index b3f26cb2611..531c4233e47 100644 --- a/esphome/components/rx8111/rx8111.cpp +++ b/esphome/components/rx8111/rx8111.cpp @@ -81,13 +81,11 @@ void RX8111Component::read_time() { this->status_set_warning(LOG_STR("Invalid RTC time")); return; } - if (ESPTime::from_epoch_utc(rtc_time.timestamp).day_of_week != weekday) { + if (!this->status_comm_result(ESPTime::from_epoch_utc(rtc_time.timestamp).day_of_week == weekday, + LOG_STR("Invalid RTC time"))) { ESP_LOGW(TAG, "RTC weekday does not match calendar date"); - this->status_set_warning(LOG_STR("Invalid RTC time")); return; } - - this->status_clear_warning(); ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month, rtc_time.hour, rtc_time.minute, rtc_time.second); time::RealTimeClock::synchronize_epoch_(rtc_time.timestamp); diff --git a/esphome/components/selec_meter/selec_meter.cpp b/esphome/components/selec_meter/selec_meter.cpp index 9a895b0e0e9..db9eca8288c 100644 --- a/esphome/components/selec_meter/selec_meter.cpp +++ b/esphome/components/selec_meter/selec_meter.cpp @@ -133,11 +133,7 @@ void SelecMeter::on_read_input_registers(uint16_t start_address, std::spandecode_em2m_(registers); #endif - if (ok) { - this->status_clear_warning(); - } else { - this->status_set_warning(); - } + this->status_comm_result(ok); break; } #if defined(USE_SELEC_METER_EM4M) && defined(USE_TEXT_SENSOR) diff --git a/esphome/components/sen21231/sen21231.cpp b/esphome/components/sen21231/sen21231.cpp index d45b247a437..b05e21eaacb 100644 --- a/esphome/components/sen21231/sen21231.cpp +++ b/esphome/components/sen21231/sen21231.cpp @@ -15,12 +15,10 @@ void Sen21231Sensor::dump_config() { void Sen21231Sensor::read_data_() { person_sensor_results_t results; - if (!this->read_bytes(PERSON_SENSOR_I2C_ADDRESS, (uint8_t *) &results, sizeof(results))) { + if (!this->status_comm_result(this->read_bytes(PERSON_SENSOR_I2C_ADDRESS, (uint8_t *) &results, sizeof(results)))) { ESP_LOGW(TAG, "Failed to read data from SEN21231"); - this->status_set_warning(); return; } - this->status_clear_warning(); ESP_LOGD(TAG, "SEN21231: %d faces detected", results.num_faces); this->publish_state(results.num_faces); if (results.num_faces == 1) { diff --git a/esphome/components/sen6x/sen6x.cpp b/esphome/components/sen6x/sen6x.cpp index 9f95ed192c0..4e8a79124c6 100644 --- a/esphome/components/sen6x/sen6x.cpp +++ b/esphome/components/sen6x/sen6x.cpp @@ -678,8 +678,7 @@ void SEN6XComponent::parse_and_publish_number_concentration_() { this->pmc_4_0_sensor_, this->pmc_10_0_sensor_}; uint16_t measurements[std::size(sensors)]; - if (!this->read_data(measurements, std::size(measurements))) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_data(measurements, std::size(measurements)))) { ESP_LOGD(TAG, "Read data failed (%d)", this->last_error_); return; } @@ -688,7 +687,6 @@ void SEN6XComponent::parse_and_publish_number_concentration_() { if (sensors[i] != nullptr) sensors[i]->publish_state(measurements[i] == 0xFFFF ? NAN : measurements[i] / 10.0f); } - this->status_clear_warning(); } #endif diff --git a/esphome/components/senseair/senseair.cpp b/esphome/components/senseair/senseair.cpp index d5849ddfc0a..7b40d0d8409 100644 --- a/esphome/components/senseair/senseair.cpp +++ b/esphome/components/senseair/senseair.cpp @@ -44,13 +44,11 @@ void SenseAirComponent::update() { uint16_t calc_checksum = crc16(response, 11); uint16_t resp_checksum = encode_value_le(&response[11]); - if (resp_checksum != calc_checksum) { + if (!this->status_comm_result(resp_checksum == calc_checksum)) { ESP_LOGW(TAG, "SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum); - this->status_set_warning(); return; } - this->status_clear_warning(); const uint8_t length = response[2]; const uint16_t status = encode_uint16(response[3], response[4]); const uint16_t ppm = encode_uint16(response[length + 1], response[length + 2]); diff --git a/esphome/components/sht4x/sht4x.cpp b/esphome/components/sht4x/sht4x.cpp index d46020b2d9a..5e6b8849dd1 100644 --- a/esphome/components/sht4x/sht4x.cpp +++ b/esphome/components/sht4x/sht4x.cpp @@ -87,15 +87,12 @@ void SHT4XComponent::update() { uint16_t buffer[2]; // Read measurement - if (!this->read_data(buffer, 2)) { + if (!this->status_comm_result(this->read_data(buffer, 2))) { // Using ESP_LOGW to force the warning to be printed ESP_LOGW(TAG, "Sensor read failed"); - this->status_set_warning(); return; } - this->status_clear_warning(); - // Evaluate and publish measurements if (this->temp_sensor_ != nullptr) { // Temp is contained in the first result word diff --git a/esphome/components/spd2010/spd2010_touchscreen.cpp b/esphome/components/spd2010/spd2010_touchscreen.cpp index 55fa177d18e..b70fe1acad9 100644 --- a/esphome/components/spd2010/spd2010_touchscreen.cpp +++ b/esphome/components/spd2010/spd2010_touchscreen.cpp @@ -147,11 +147,9 @@ bool SPD2010Touchscreen::read_data_() { return true; } if (!(status_low & (STATUS_POINT | STATUS_GESTURE))) { - if ((status_high & STATUS_RUNNING) && (status_low & STATUS_AUX) && !this->clear_interrupt_()) { - this->status_set_warning(LOG_STR("Failed to clear interrupt")); - return false; - } - this->status_clear_warning(); + const bool needs_clear = (status_high & STATUS_RUNNING) && (status_low & STATUS_AUX); + const bool cleared = !needs_clear || this->clear_interrupt_(); + this->status_comm_result(cleared, LOG_STR("Failed to clear interrupt")); return false; } if (length < REPORT_HEADER_LENGTH || length > MAX_REPORT_LENGTH || diff --git a/esphome/components/st7123/touchscreen/st7123_touchscreen.cpp b/esphome/components/st7123/touchscreen/st7123_touchscreen.cpp index 5cd986f6976..2f21de3f6d3 100644 --- a/esphome/components/st7123/touchscreen/st7123_touchscreen.cpp +++ b/esphome/components/st7123/touchscreen/st7123_touchscreen.cpp @@ -73,12 +73,10 @@ void ST7123Touchscreen::update_touches() { // Reading from this register also clears the INT pin so the controller can report the next frame. uint8_t data[(ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + ST7123_MAX_TOUCHES * ST7123_TOUCH_STRIDE]; const size_t len = (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + this->max_touches_ * ST7123_TOUCH_STRIDE; - if (this->read_register16(ST7123_REG_ADV_TOUCH_INFO, data, len) != i2c::ERROR_OK) { + if (!this->status_comm_result(this->read_register16(ST7123_REG_ADV_TOUCH_INFO, data, len) == i2c::ERROR_OK)) { this->skip_update_ = true; - this->status_set_warning(); return; } - this->status_clear_warning(); const uint8_t *points = data + (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO); for (uint8_t i = 0; i != this->max_touches_; i++) { diff --git a/esphome/components/sts3x/sts3x.cpp b/esphome/components/sts3x/sts3x.cpp index fd9569b951d..9e224a024c6 100644 --- a/esphome/components/sts3x/sts3x.cpp +++ b/esphome/components/sts3x/sts3x.cpp @@ -50,15 +50,12 @@ void STS3XComponent::update() { this->set_timeout(50, [this]() { uint16_t raw_data[1]; - if (!this->read_data(raw_data, 1)) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_data(raw_data, 1))) return; - } float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f; ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature); this->publish_state(temperature); - this->status_clear_warning(); }); } diff --git a/esphome/components/sy6970/sy6970.cpp b/esphome/components/sy6970/sy6970.cpp index 754cfb14ca8..3efc4d8a3f9 100644 --- a/esphome/components/sy6970/sy6970.cpp +++ b/esphome/components/sy6970/sy6970.cpp @@ -123,14 +123,11 @@ void SY6970Component::update() { } // Read all registers in one transaction - if (!this->read_all_registers_()) { + if (!this->status_comm_result(this->read_all_registers_())) { ESP_LOGW(TAG, "Failed to read registers during update"); - this->status_set_warning(); return; } - this->status_clear_warning(); - // Notify all listeners with the new data for (auto *listener : this->listeners_) { listener->on_data(this->data_); diff --git a/esphome/components/t6615/t6615.cpp b/esphome/components/t6615/t6615.cpp index 6fb58345358..e06d3277842 100644 --- a/esphome/components/t6615/t6615.cpp +++ b/esphome/components/t6615/t6615.cpp @@ -44,18 +44,15 @@ void T6615Component::loop() { this->read_array(response_buffer, 5); // Read header - if (response_buffer[0] != T6615_MAGIC || response_buffer[1] != T6615_ADDR_HOST) { + if (!this->status_comm_result(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]); this->discard_available(); /* try again to read the sensor */ this->send_ppm_command_(); - this->status_set_warning(); return; } - this->status_clear_warning(); - switch (this->command_) { case T6615Command::GET_PPM: { const uint16_t ppm = encode_uint16(response_buffer[3], response_buffer[4]); diff --git a/esphome/components/tca8418/tca8418.cpp b/esphome/components/tca8418/tca8418.cpp index 204bc18c1e5..27e3e0337e5 100644 --- a/esphome/components/tca8418/tca8418.cpp +++ b/esphome/components/tca8418/tca8418.cpp @@ -86,11 +86,8 @@ void TCA8418Component::loop() { return; this->last_poll_ = now; - if (!this->drain_events_(true)) { - this->status_set_warning(); + if (!this->status_comm_result(this->drain_events_(true))) return; - } - this->status_clear_warning(); // Events that arrive during a read keep INT asserted without a new edge, so only stop once it is clear if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read()) diff --git a/esphome/components/tmc22xx/tmc22xx.cpp b/esphome/components/tmc22xx/tmc22xx.cpp index 6ee4c9e3ee6..e050325436c 100644 --- a/esphome/components/tmc22xx/tmc22xx.cpp +++ b/esphome/components/tmc22xx/tmc22xx.cpp @@ -451,12 +451,10 @@ bool TMC22XXStepper::write_register(uint8_t reg, uint32_t value) { this->flush(); // On the single wire bus every byte sent is also received, drop that echo uint8_t echo[DATAGRAM_SIZE]; - if (!this->read_array(echo, DATAGRAM_SIZE) || memcmp(echo, datagram, DATAGRAM_SIZE) != 0) { + if (!this->status_comm_result(this->read_array(echo, DATAGRAM_SIZE) && memcmp(echo, datagram, DATAGRAM_SIZE) == 0)) { ESP_LOGV(TAG, "No echo writing register 0x%02X", reg); - this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return false; } - this->status_clear_warning(); if (uint32_t *shadow = this->shadow_register(reg)) { *shadow = value; @@ -483,12 +481,11 @@ optional TMC22XXStepper::read_register(uint8_t reg) { this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return {}; } - if (reply[0] != SYNC_BYTE || reply[1] != MASTER_ADDRESS || reply[2] != reg || reply[7] != datagram_crc(reply, 7)) { + if (!this->status_comm_result(reply[0] == SYNC_BYTE && reply[1] == MASTER_ADDRESS && reply[2] == reg && + reply[7] == datagram_crc(reply, 7))) { ESP_LOGV(TAG, "Invalid reply reading register 0x%02X", reg); - this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return {}; } - this->status_clear_warning(); return encode_uint32(reply[3], reply[4], reply[5], reply[6]); } diff --git a/esphome/components/tmp1075/tmp1075.cpp b/esphome/components/tmp1075/tmp1075.cpp index 0cb2456cbd3..58858b9b386 100644 --- a/esphome/components/tmp1075/tmp1075.cpp +++ b/esphome/components/tmp1075/tmp1075.cpp @@ -29,12 +29,10 @@ void TMP1075Sensor::setup() { void TMP1075Sensor::update() { uint16_t regvalue; - if (!read_byte_16(REG_TEMP, ®value)) { + if (!this->status_comm_result(this->read_byte_16(REG_TEMP, ®value), LOG_STR("can't read"))) { ESP_LOGW(TAG, "'%s' - unable to read temperature register", this->name_.c_str()); - this->status_set_warning(LOG_STR("can't read")); return; } - this->status_clear_warning(); const float temp = regvalue2temp(regvalue); this->publish_state(temp); diff --git a/esphome/components/tsl2561/tsl2561.cpp b/esphome/components/tsl2561/tsl2561.cpp index 0ae96c9fd1c..33f33bda104 100644 --- a/esphome/components/tsl2561/tsl2561.cpp +++ b/esphome/components/tsl2561/tsl2561.cpp @@ -119,15 +119,12 @@ void TSL2561Sensor::read_data_() { return; } // Power off - if (!this->tsl2561_write_byte(TSL2561_REGISTER_CONTROL, 0b00000000)) { - this->status_set_warning(); + if (!this->status_comm_result(this->tsl2561_write_byte(TSL2561_REGISTER_CONTROL, 0b00000000))) return; - } float lx = this->calculate_lx_(data0, data1); ESP_LOGD(TAG, "Got illuminance=%.1flx", lx); this->publish_state(lx); - this->status_clear_warning(); } float TSL2561Sensor::get_integration_time_ms_() { switch (this->integration_time_) { diff --git a/esphome/components/ttp229_lsf/ttp229_lsf.cpp b/esphome/components/ttp229_lsf/ttp229_lsf.cpp index bf5c3f67284..f50e5392915 100644 --- a/esphome/components/ttp229_lsf/ttp229_lsf.cpp +++ b/esphome/components/ttp229_lsf/ttp229_lsf.cpp @@ -18,12 +18,9 @@ void TTP229LSFComponent::dump_config() { } void TTP229LSFComponent::loop() { uint16_t touched = 0; - if (this->read(reinterpret_cast(&touched), 2) != i2c::ERROR_OK) { - this->status_set_warning(); + if (!this->status_comm_result(this->read(reinterpret_cast(&touched), 2) == i2c::ERROR_OK)) return; - } touched = i2c::i2ctohs(touched); - this->status_clear_warning(); touched = reverse_bits(touched); for (auto *channel : this->channels_) { channel->process(touched); diff --git a/esphome/components/veml3235/veml3235.cpp b/esphome/components/veml3235/veml3235.cpp index ffaaf9eb2a3..1b8984247a4 100644 --- a/esphome/components/veml3235/veml3235.cpp +++ b/esphome/components/veml3235/veml3235.cpp @@ -54,15 +54,12 @@ void VEML3235Sensor::update() { void VEML3235Sensor::read_and_publish_(uint8_t adjustments_left) { uint8_t als_regs[] = {0, 0}; - if ((this->read_register(ALS_REG, als_regs, sizeof als_regs) != i2c::ERROR_OK)) { - this->status_set_warning(); + if (!this->status_comm_result(this->read_register(ALS_REG, als_regs, sizeof als_regs) == i2c::ERROR_OK)) { this->publish_state(NAN); this->measurement_in_progress_ = false; return; } - this->status_clear_warning(); - uint16_t als_counts = encode_value_le(als_regs); if (this->auto_gain_ && adjustments_left > 0) {