From 49caeae7807aef32292ab1e509200293d8c295be Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Oct 2026 02:40:54 -1000 Subject: [PATCH] [components] Let core report communication failures in dump_config (3/5) (#20872) --- esphome/components/ina219/ina219.cpp | 10 +++--- esphome/components/ina226/ina226.cpp | 10 +++--- esphome/components/ina260/ina260.cpp | 24 ++------------ esphome/components/ina260/ina260.h | 7 ----- .../components/ina2xx_base/ina2xx_base.cpp | 9 ++---- esphome/components/ina3221/ina3221.cpp | 11 +++---- .../components/kamstrup_kmp/kamstrup_kmp.cpp | 3 -- esphome/components/lcd_menu/lcd_menu.cpp | 5 +-- .../lcd_pcf8574/pcf8574_display.cpp | 5 +-- esphome/components/lis2dw12/lis2dw12.cpp | 11 ++++--- esphome/components/lis3dh/lis3dh.cpp | 12 +++---- esphome/components/lm75b/lm75b.cpp | 3 -- esphome/components/lp5562/lp5562_output.cpp | 7 ++--- esphome/components/ltr501/ltr501.cpp | 6 +--- esphome/components/ltr_als_ps/ltr_als_ps.cpp | 6 +--- esphome/components/max17043/max17043.cpp | 10 ++---- esphome/components/max17048/max17048.cpp | 3 -- esphome/components/max44009/max44009.cpp | 5 +-- esphome/components/mcp4461/mcp4461.cpp | 9 ++---- esphome/components/mcp4725/mcp4725.cpp | 11 ++----- esphome/components/mcp4725/mcp4725.h | 1 - esphome/components/mcp4728/mcp4728.cpp | 5 +-- esphome/components/mcp9600/mcp9600.cpp | 18 ++--------- esphome/components/mcp9600/mcp9600.h | 6 ---- esphome/components/mcp9808/mcp9808.cpp | 19 +++++++----- .../components/mlx90393/sensor_mlx90393.cpp | 14 +++------ esphome/components/mmc5603/mmc5603.cpp | 26 ++++------------ esphome/components/mmc5603/mmc5603.h | 5 --- esphome/components/mpl3115a2/mpl3115a2.cpp | 19 ++---------- esphome/components/mpl3115a2/mpl3115a2.h | 5 --- esphome/components/mpr121/mpr121.cpp | 11 +------ esphome/components/mpr121/mpr121.h | 4 --- esphome/components/mpu6050/mpu6050.cpp | 31 +++++++------------ esphome/components/mpu6050/mpu6050.h | 2 ++ esphome/components/mpu6886/mpu6886.cpp | 31 +++++++------------ esphome/components/mpu6886/mpu6886.h | 2 ++ esphome/components/ms5611/ms5611.cpp | 7 ++--- esphome/components/msa3xx/msa3xx.cpp | 16 +++++----- 38 files changed, 107 insertions(+), 282 deletions(-) diff --git a/esphome/components/ina219/ina219.cpp b/esphome/components/ina219/ina219.cpp index a675099b4dc..48c9a06f504 100644 --- a/esphome/components/ina219/ina219.cpp +++ b/esphome/components/ina219/ina219.cpp @@ -36,7 +36,7 @@ void INA219Component::setup() { // Config Register // 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset) if (!this->write_byte_16(INA219_REGISTER_CONFIG, 0x8000)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -101,7 +101,7 @@ void INA219Component::setup() { config |= shunt_gain << 11; ESP_LOGCONFIG(TAG, " Using %dV-Range Shunt Gain=%dmV", bus_32v_range ? 32 : 16, 40 << shunt_gain); if (!this->write_byte_16(INA219_REGISTER_CONFIG, config)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -122,7 +122,7 @@ void INA219Component::setup() { auto calibration = uint32_t(0.04096f / (0.000001f * lsb * this->shunt_resistance_ohm_)); ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration); if (!this->write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } @@ -138,10 +138,8 @@ void INA219Component::dump_config() { ESP_LOGCONFIG(TAG, "INA219:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); + if (this->is_failed()) return; - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_); diff --git a/esphome/components/ina226/ina226.cpp b/esphome/components/ina226/ina226.cpp index 97b3caf949b..987ce25d60b 100644 --- a/esphome/components/ina226/ina226.cpp +++ b/esphome/components/ina226/ina226.cpp @@ -40,7 +40,7 @@ void INA226Component::setup() { config.reset = 1; if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } delay(1); @@ -61,7 +61,7 @@ void INA226Component::setup() { config.mode = 0b111; if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -75,7 +75,7 @@ void INA226Component::setup() { ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration); if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } @@ -84,10 +84,8 @@ void INA226Component::dump_config() { ESP_LOGCONFIG(TAG, "INA226:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); + if (this->is_failed()) return; - } LOG_UPDATE_INTERVAL(this); ESP_LOGCONFIG(TAG, diff --git a/esphome/components/ina260/ina260.cpp b/esphome/components/ina260/ina260.cpp index 82a4e4fba14..0600e088390 100644 --- a/esphome/components/ina260/ina260.cpp +++ b/esphome/components/ina260/ina260.cpp @@ -36,8 +36,7 @@ static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF; void INA260Component::setup() { // Reset device on setup if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) { - this->error_code_ = DEVICE_RESET_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR("Device reset failed - Is the device connected?")); return; } @@ -47,14 +46,12 @@ void INA260Component::setup() { this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_); if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR("Connected device does not match a known INA260 sensor")); return; } if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) { - this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION; - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to update device configuration")); return; } } @@ -70,21 +67,6 @@ void INA260Component::dump_config() { LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_); LOG_SENSOR(" ", "Current", this->current_sensor_); LOG_SENSOR(" ", "Power", this->power_sensor_); - - switch (this->error_code_) { - case COMMUNICATION_FAILED: - ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor"); - break; - case DEVICE_RESET_FAILED: - ESP_LOGE(TAG, "Device reset failed - Is the device connected?"); - break; - case FAILED_TO_UPDATE_CONFIGURATION: - ESP_LOGE(TAG, "Failed to update device configuration"); - break; - case NONE: - default: - break; - } } void INA260Component::update() { diff --git a/esphome/components/ina260/ina260.h b/esphome/components/ina260/ina260.h index bbcb7a7acb2..162a4447e0f 100644 --- a/esphome/components/ina260/ina260.h +++ b/esphome/components/ina260/ina260.h @@ -23,13 +23,6 @@ class INA260Component final : public PollingComponent, public i2c::I2CDevice { sensor::Sensor *bus_voltage_sensor_{nullptr}; sensor::Sensor *current_sensor_{nullptr}; sensor::Sensor *power_sensor_{nullptr}; - - enum ErrorCode { - NONE, - COMMUNICATION_FAILED, - DEVICE_RESET_FAILED, - FAILED_TO_UPDATE_CONFIGURATION, - } error_code_{NONE}; }; } // namespace esphome::ina260 diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 58e0598b06c..4c36870b680 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -50,15 +50,13 @@ static bool check_model_and_device_match(INAModel model, uint16_t dev_id) { void INA2XX::setup() { if (!this->reset_config_()) { - ESP_LOGE(TAG, "Reset failed, check connection"); - this->mark_failed(); + this->mark_failed(LOG_STR("Reset failed, check connection")); return; } delay(2); if (!this->check_device_model_()) { - ESP_LOGE(TAG, "Device not supported or model selected improperly in yaml file"); - this->mark_failed(); + this->mark_failed(LOG_STR("Device not supported or model selected improperly in yaml file")); return; } delay(1); @@ -197,9 +195,6 @@ void INA2XX::dump_config() { ESP_LOGE(TAG, " Device model mismatch. Found device with ID = %x. Please check your configuration.", this->dev_id_); } - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); ESP_LOGCONFIG(TAG, " Shunt resistance = %f Ohm\n" diff --git a/esphome/components/ina3221/ina3221.cpp b/esphome/components/ina3221/ina3221.cpp index e66e2456767..455b4c0c7d2 100644 --- a/esphome/components/ina3221/ina3221.cpp +++ b/esphome/components/ina3221/ina3221.cpp @@ -40,13 +40,13 @@ static constexpr uint32_t READ_TIMEOUT_ID = 0; void INA3221Component::setup() { if (!this->write_byte_16(INA3221_REGISTER_CONFIG, INA3221_CONFIG_RESET)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } delay(1); if (!this->write_byte_16(INA3221_REGISTER_CONFIG, this->config_)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -57,7 +57,7 @@ void INA3221Component::setup() { !this->write_byte_16(INA3221_REGISTER_CHANNEL1_CRITICAL_ALERT + i, channel.critical_limit_)) || (channel.warning_limit_ != 0 && !this->write_byte_16(INA3221_REGISTER_CHANNEL1_WARNING_ALERT + i, channel.warning_limit_))) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } @@ -67,7 +67,7 @@ void INA3221Component::setup() { // Sum the shunt voltages of every enabled channel if (this->sum_shunt_voltage_sensor_ != nullptr && !this->write_byte_16(INA3221_REGISTER_MASK_ENABLE, this->config_ & INA3221_CONFIG_CHANNEL_MASK)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } #endif @@ -76,9 +76,6 @@ void INA3221Component::setup() { void INA3221Component::dump_config() { ESP_LOGCONFIG(TAG, "INA3221:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); ESP_LOGCONFIG(TAG, " Config register: 0x%04X", this->config_); if (this->single_shot_wait_ms_ != 0) { diff --git a/esphome/components/kamstrup_kmp/kamstrup_kmp.cpp b/esphome/components/kamstrup_kmp/kamstrup_kmp.cpp index 9713c72b613..fcf648c31f3 100644 --- a/esphome/components/kamstrup_kmp/kamstrup_kmp.cpp +++ b/esphome/components/kamstrup_kmp/kamstrup_kmp.cpp @@ -9,9 +9,6 @@ ESPHOME_LOG_TAG(TAG, "kamstrup_kmp"); void KamstrupKMPComponent::dump_config() { ESP_LOGCONFIG(TAG, "kamstrup_kmp:"); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Heat Energy", this->heat_energy_sensor_); diff --git a/esphome/components/lcd_menu/lcd_menu.cpp b/esphome/components/lcd_menu/lcd_menu.cpp index 7ced6e67cec..b500461809b 100644 --- a/esphome/components/lcd_menu/lcd_menu.cpp +++ b/esphome/components/lcd_menu/lcd_menu.cpp @@ -8,7 +8,7 @@ ESPHOME_LOG_TAG(TAG, "lcd_menu"); void LCDCharacterMenuComponent::setup() { if (this->display_->is_failed()) { - this->mark_failed(); + this->mark_failed(LOG_STR("The connected display failed, the menu is disabled")); return; } @@ -24,9 +24,6 @@ void LCDCharacterMenuComponent::dump_config() { " Mark characters: %02x, %02x, %02x, %02x", this->columns_, this->rows_, this->mark_selected_, this->mark_editing_, this->mark_submenu_, this->mark_back_); - if (this->is_failed()) { - ESP_LOGE(TAG, "The connected display failed, the menu is disabled!"); - } } void LCDCharacterMenuComponent::draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) { diff --git a/esphome/components/lcd_pcf8574/pcf8574_display.cpp b/esphome/components/lcd_pcf8574/pcf8574_display.cpp index fc7f5ff2f50..2e2196be4ad 100644 --- a/esphome/components/lcd_pcf8574/pcf8574_display.cpp +++ b/esphome/components/lcd_pcf8574/pcf8574_display.cpp @@ -12,7 +12,7 @@ static const uint8_t LCD_DISPLAY_BACKLIGHT_OFF = 0x00; void PCF8574LCDDisplay::setup() { this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_ON; if (!this->write_bytes(this->backlight_value_, nullptr, 0)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -25,9 +25,6 @@ void PCF8574LCDDisplay::dump_config() { this->columns_, this->rows_); LOG_I2C_DEVICE(this); LOG_UPDATE_INTERVAL(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } } void PCF8574LCDDisplay::write_n_bits(uint8_t value, uint8_t n) { if (n == 4) { diff --git a/esphome/components/lis2dw12/lis2dw12.cpp b/esphome/components/lis2dw12/lis2dw12.cpp index 38065aa7859..21e1a07676f 100644 --- a/esphome/components/lis2dw12/lis2dw12.cpp +++ b/esphome/components/lis2dw12/lis2dw12.cpp @@ -83,9 +83,13 @@ static const LogString *filter_bandwidth_to_string(FilterBandwidth bw) { void LIS2DW12Component::setup() { uint8_t who_am_i{0}; - if (!this->read_byte(static_cast(RegisterMap::WHO_AM_I), &who_am_i) || who_am_i != LIS2DW12_WHO_AM_I_VALUE) { + if (!this->read_byte(static_cast(RegisterMap::WHO_AM_I), &who_am_i)) { + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); + return; + } + if (who_am_i != LIS2DW12_WHO_AM_I_VALUE) { ESP_LOGE(TAG, "WHO_AM_I check failed. Got 0x%02X, expected 0x%02X", who_am_i, LIS2DW12_WHO_AM_I_VALUE); - this->mark_failed(); + this->mark_failed(LOG_STR("Wrong chip ID")); return; } @@ -171,9 +175,6 @@ bool LIS2DW12Component::configure_registers_() { void LIS2DW12Component::dump_config() { ESP_LOGCONFIG(TAG, "LIS2DW12:"); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } ESP_LOGCONFIG(TAG, " Power Mode: %s\n" " Data Rate: %s\n" diff --git a/esphome/components/lis3dh/lis3dh.cpp b/esphome/components/lis3dh/lis3dh.cpp index 22ca7ae56b2..f12c00852cd 100644 --- a/esphome/components/lis3dh/lis3dh.cpp +++ b/esphome/components/lis3dh/lis3dh.cpp @@ -29,19 +29,17 @@ void LIS3DHComponent::setup() { uint8_t who_am_i = 0; if (!this->read_byte(LIS3DH_REG_WHO_AM_I, &who_am_i)) { - ESP_LOGE(TAG, "Failed to read chip ID - check wiring / address"); - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to read chip ID - check wiring / address")); return; } if (who_am_i != LIS3DH_WHO_AM_I_VALUE) { ESP_LOGE(TAG, "Wrong chip ID: 0x%02X (expected 0x%02X)", who_am_i, LIS3DH_WHO_AM_I_VALUE); - this->mark_failed(); + this->mark_failed(LOG_STR("Wrong chip ID")); return; } if (!this->setup_accelerometer_() || !this->setup_temperature_() || !this->setup_interrupt_()) { - ESP_LOGE(TAG, "Failed to configure sensor"); - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to configure sensor")); return; } } @@ -119,10 +117,8 @@ bool LIS3DHComponent::setup_interrupt_() { void LIS3DHComponent::dump_config() { ESP_LOGCONFIG(TAG, "LIS3DH:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); + if (this->is_failed()) return; - } ESP_LOGCONFIG(TAG, " Range: ±%ug\n" diff --git a/esphome/components/lm75b/lm75b.cpp b/esphome/components/lm75b/lm75b.cpp index 56e69faa62f..388f5dfd93a 100644 --- a/esphome/components/lm75b/lm75b.cpp +++ b/esphome/components/lm75b/lm75b.cpp @@ -9,9 +9,6 @@ ESPHOME_LOG_TAG(TAG, "lm75b"); void LM75BComponent::dump_config() { ESP_LOGCONFIG(TAG, "LM75B:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, "Setting up LM75B failed!"); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Temperature", this); } diff --git a/esphome/components/lp5562/lp5562_output.cpp b/esphome/components/lp5562/lp5562_output.cpp index 356ef2c0669..b55e8b297d2 100644 --- a/esphome/components/lp5562/lp5562_output.cpp +++ b/esphome/components/lp5562/lp5562_output.cpp @@ -33,7 +33,7 @@ void LP5562Output::setup() { // power up the chip and enable the internal 32 kHz oscillator, see // https://github.com/m5stack/M5GFX/blob/b2422cdb4735159209c2097d425c0756505b9a81/src/M5GFX.cpp#L566 if (!this->write_byte(LP5562_REG_ENABLE, LP5562_ENABLE_CHIP_EN)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } delay(1); @@ -41,16 +41,13 @@ void LP5562Output::setup() { // internal blink/fade engines if (!this->write_byte(LP5562_REG_CONFIG, LP5562_CONFIG_INT_CLK_EN) || !this->write_byte(LP5562_REG_LED_MAP, LP5562_LED_MAP_ALL_PWM_REGISTERS)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); } } void LP5562Output::dump_config() { ESP_LOGCONFIG(TAG, "LP5562:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, "Setting up LP5562 failed!"); - } } void LP5562Output::set_channel_value_(uint8_t channel, uint8_t value) { diff --git a/esphome/components/ltr501/ltr501.cpp b/esphome/components/ltr501/ltr501.cpp index 91fca7fa9ba..a3f082051cc 100644 --- a/esphome/components/ltr501/ltr501.cpp +++ b/esphome/components/ltr501/ltr501.cpp @@ -115,10 +115,6 @@ void LTRAlsPs501Component::dump_config() { LOG_SENSOR(" ", "CH1 Infrared counts", this->infrared_counts_sensor_); LOG_SENSOR(" ", "CH0 Visible+IR counts", this->full_spectrum_counts_sensor_); LOG_SENSOR(" ", "Actual gain", this->actual_gain_sensor_); - - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } } void LTRAlsPs501Component::update() { @@ -151,7 +147,7 @@ void LTRAlsPs501Component::loop() { err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { ESP_LOGW(TAG, "i2c connection failed"); - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); } this->configure_reset_(); if (this->is_als_()) { diff --git a/esphome/components/ltr_als_ps/ltr_als_ps.cpp b/esphome/components/ltr_als_ps/ltr_als_ps.cpp index 2d3b3755ad5..743bd68529d 100644 --- a/esphome/components/ltr_als_ps/ltr_als_ps.cpp +++ b/esphome/components/ltr_als_ps/ltr_als_ps.cpp @@ -109,10 +109,6 @@ void LTRAlsPsComponent::dump_config() { LOG_SENSOR(" ", "Proximity counts", this->proximity_counts_sensor_); } LOG_UPDATE_INTERVAL(this); - - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } } void LTRAlsPsComponent::update() { @@ -142,7 +138,7 @@ void LTRAlsPsComponent::loop() { err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { ESP_LOGV(TAG, "i2c connection failed"); - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); } this->configure_reset_(); if (this->is_als_()) { diff --git a/esphome/components/max17043/max17043.cpp b/esphome/components/max17043/max17043.cpp index a15658512b2..c64875e67c9 100644 --- a/esphome/components/max17043/max17043.cpp +++ b/esphome/components/max17043/max17043.cpp @@ -55,16 +55,13 @@ void MAX17043Component::setup() { ESP_LOGV(TAG, "MAX17043 CONFIG register reads 0x%X", config_reg); if (config_reg != MAX17043_CONFIG_POWER_UP_DEFAULT) { - ESP_LOGE(TAG, "Device does not appear to be a MAX17043"); - this->status_set_error(LOG_STR("unrecognised")); - this->mark_failed(); + this->mark_failed(LOG_STR("Device does not appear to be a MAX17043")); return; } // need to write back to config register to reset the sleep bit if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT)) { - this->status_set_error(LOG_STR("sleep reset failed")); - this->mark_failed(); + this->mark_failed(LOG_STR("sleep reset failed")); return; } } @@ -72,9 +69,6 @@ void MAX17043Component::setup() { void MAX17043Component::dump_config() { ESP_LOGCONFIG(TAG, "MAX17043:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Battery Voltage", this->voltage_sensor_); LOG_SENSOR(" ", "Battery Level", this->battery_remaining_sensor_); diff --git a/esphome/components/max17048/max17048.cpp b/esphome/components/max17048/max17048.cpp index 67f9dbaac3d..1caf8ab308b 100644 --- a/esphome/components/max17048/max17048.cpp +++ b/esphome/components/max17048/max17048.cpp @@ -65,9 +65,6 @@ void MAX17048Component::update() { void MAX17048Component::dump_config() { ESP_LOGCONFIG(TAG, "MAX17048:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_sensor_); LOG_SENSOR(" ", "Battery Level", this->battery_level_sensor_); diff --git a/esphome/components/max44009/max44009.cpp b/esphome/components/max44009/max44009.cpp index b616b9032ee..10df08bbeb4 100644 --- a/esphome/components/max44009/max44009.cpp +++ b/esphome/components/max44009/max44009.cpp @@ -39,15 +39,12 @@ void MAX44009Sensor::setup() { } } if (!state_ok) - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); } void MAX44009Sensor::dump_config() { ESP_LOGCONFIG(TAG, "MAX44009:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } } void MAX44009Sensor::update() { diff --git a/esphome/components/mcp4461/mcp4461.cpp b/esphome/components/mcp4461/mcp4461.cpp index b6e8c247021..800ec631e75 100644 --- a/esphome/components/mcp4461/mcp4461.cpp +++ b/esphome/components/mcp4461/mcp4461.cpp @@ -12,7 +12,7 @@ void Mcp4461Component::setup() { auto err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { this->error_code_ = MCP4461_STATUS_I2C_ERROR; - this->mark_failed(); + this->mark_failed(Mcp4461Component::get_message_string(this->error_code_)); return; } // save WP/WL status @@ -96,9 +96,6 @@ void Mcp4461Component::update_write_protection_status_() { void Mcp4461Component::dump_config() { ESP_LOGCONFIG(TAG, "mcp4461:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(this->get_message_string(this->error_code_))); - } // log wiper status for (uint8_t i = 0; i < 8; ++i) { // terminals only valid for volatile wipers 0-3 - enable/disable is terminal hw @@ -218,7 +215,7 @@ uint8_t Mcp4461Component::get_status_register_() { uint16_t buf; if (!this->read_16_(reg, &buf)) { this->error_code_ = MCP4461_STATUS_REGISTER_ERROR; - this->mark_failed(); + this->mark_failed(Mcp4461Component::get_message_string(this->error_code_)); return 0; } uint8_t msb = buf >> 8; @@ -227,7 +224,7 @@ uint8_t Mcp4461Component::get_status_register_() { // D8, D7 and R1 bits are hardlocked to 1 -> a status msb bit 0 (bit 9 of status register) of 0 or lsb bit 1/7 = 0 // indicate device/communication issues, therefore mark component failed this->error_code_ = MCP4461_STATUS_REGISTER_INVALID; - this->mark_failed(); + this->mark_failed(Mcp4461Component::get_message_string(this->error_code_)); return 0; } this->status_clear_warning(); diff --git a/esphome/components/mcp4725/mcp4725.cpp b/esphome/components/mcp4725/mcp4725.cpp index faa377b8de9..f25765f4a3a 100644 --- a/esphome/components/mcp4725/mcp4725.cpp +++ b/esphome/components/mcp4725/mcp4725.cpp @@ -8,19 +8,12 @@ ESPHOME_LOG_TAG(TAG, "mcp4725"); void MCP4725::setup() { auto err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } -void MCP4725::dump_config() { - LOG_I2C_DEVICE(this); - - if (this->error_code_ == COMMUNICATION_FAILED) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } -} +void MCP4725::dump_config() { LOG_I2C_DEVICE(this); } // https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886 void MCP4725::write_state(float state) { diff --git a/esphome/components/mcp4725/mcp4725.h b/esphome/components/mcp4725/mcp4725.h index a0838dc33e6..9c4b95db3f0 100644 --- a/esphome/components/mcp4725/mcp4725.h +++ b/esphome/components/mcp4725/mcp4725.h @@ -16,7 +16,6 @@ class MCP4725 final : public Component, public output::FloatOutput, public i2c:: void write_state(float state) override; protected: - enum ErrorCode { NONE = 0, COMMUNICATION_FAILED } error_code_{NONE}; }; } // namespace esphome::mcp4725 diff --git a/esphome/components/mcp4728/mcp4728.cpp b/esphome/components/mcp4728/mcp4728.cpp index 7259a0a89ec..48336af339d 100644 --- a/esphome/components/mcp4728/mcp4728.cpp +++ b/esphome/components/mcp4728/mcp4728.cpp @@ -10,7 +10,7 @@ ESPHOME_LOG_TAG(TAG, "mcp4728"); void MCP4728Component::setup() { auto err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } @@ -18,9 +18,6 @@ void MCP4728Component::setup() { void MCP4728Component::dump_config() { ESP_LOGCONFIG(TAG, "MCP4728:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } } void MCP4728Component::loop() { diff --git a/esphome/components/mcp9600/mcp9600.cpp b/esphome/components/mcp9600/mcp9600.cpp index 70e7cb176d4..d99cf63b471 100644 --- a/esphome/components/mcp9600/mcp9600.cpp +++ b/esphome/components/mcp9600/mcp9600.cpp @@ -33,8 +33,7 @@ void MCP9600Component::setup() { // Allows both MCP9600's and MCP9601's to be connected. if (this->device_id_ != (uint8_t) 0x40 && this->device_id_ != (uint8_t) 0x41) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR("Connected device does not match a known MCP9600 or MCP9601 sensor")); return; } @@ -55,8 +54,7 @@ void MCP9600Component::setup() { success &= this->write_byte_16(MCP9600_REGISTER_ALERT4_LIMIT, 0x0000); if (!success) { - this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION; - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to update device configuration")); return; } } @@ -70,18 +68,6 @@ void MCP9600Component::dump_config() { LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Hot Junction Temperature", this->hot_junction_sensor_); LOG_SENSOR(" ", "Cold Junction Temperature", this->cold_junction_sensor_); - - switch (this->error_code_) { - case COMMUNICATION_FAILED: - ESP_LOGE(TAG, "Connected device does not match a known MCP9600 or MCP901 sensor"); - break; - case FAILED_TO_UPDATE_CONFIGURATION: - ESP_LOGE(TAG, "Failed to update device configuration"); - break; - case NONE: - default: - break; - } } void MCP9600Component::update() { diff --git a/esphome/components/mcp9600/mcp9600.h b/esphome/components/mcp9600/mcp9600.h index 523c9ace2f7..88dfd046ceb 100644 --- a/esphome/components/mcp9600/mcp9600.h +++ b/esphome/components/mcp9600/mcp9600.h @@ -36,12 +36,6 @@ class MCP9600Component final : public PollingComponent, public i2c::I2CDevice { sensor::Sensor *cold_junction_sensor_{nullptr}; MCP9600ThermocoupleType thermocouple_type_{MCP9600_THERMOCOUPLE_TYPE_K}; - - enum ErrorCode { - NONE, - COMMUNICATION_FAILED, - FAILED_TO_UPDATE_CONFIGURATION, - } error_code_{NONE}; }; } // namespace esphome::mcp9600 diff --git a/esphome/components/mcp9808/mcp9808.cpp b/esphome/components/mcp9808/mcp9808.cpp index 47df37f4563..309651970ca 100644 --- a/esphome/components/mcp9808/mcp9808.cpp +++ b/esphome/components/mcp9808/mcp9808.cpp @@ -18,24 +18,29 @@ ESPHOME_LOG_TAG(TAG, "mcp9808"); void MCP9808Sensor::setup() { uint16_t manu = 0; - if (!this->read_byte_16(MCP9808_REG_MANUF_ID, &manu) || manu != MCP9808_MANUF_ID) { - this->mark_failed(); + if (!this->read_byte_16(MCP9808_REG_MANUF_ID, &manu)) { + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); + return; + } + if (manu != MCP9808_MANUF_ID) { ESP_LOGE(TAG, "Incorrect manufacturer ID (%X) for '%s'", manu, this->name_.c_str()); + this->mark_failed(LOG_STR("Wrong chip ID")); return; } uint16_t dev_id = 0; - if (!this->read_byte_16(MCP9808_REG_DEVICE_ID, &dev_id) || dev_id != MCP9808_DEV_ID) { - this->mark_failed(); + if (!this->read_byte_16(MCP9808_REG_DEVICE_ID, &dev_id)) { + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); + return; + } + if (dev_id != MCP9808_DEV_ID) { ESP_LOGE(TAG, "Incorrect device ID (%X) for '%s'", dev_id, this->name_.c_str()); + this->mark_failed(LOG_STR("Wrong chip ID")); return; } } void MCP9808Sensor::dump_config() { ESP_LOGCONFIG(TAG, "%s:", this->name_.c_str()); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL_FOR, this->name_.c_str()); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Temperature", this); } diff --git a/esphome/components/mlx90393/sensor_mlx90393.cpp b/esphome/components/mlx90393/sensor_mlx90393.cpp index e47b7d8c30b..7aa01a976d9 100644 --- a/esphome/components/mlx90393/sensor_mlx90393.cpp +++ b/esphome/components/mlx90393/sensor_mlx90393.cpp @@ -112,7 +112,7 @@ void MLX90393Cls::setup() { this->mlx_.begin_with_hal(this, 0, 0); if (!this->apply_all_settings_()) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); } // start verify settings process @@ -123,10 +123,8 @@ void MLX90393Cls::dump_config() { ESP_LOGCONFIG(TAG, "MLX90393:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); + if (this->is_failed()) return; - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "X Axis", this->x_sensor_); @@ -250,16 +248,12 @@ void MLX90393Cls::verify_settings_timeout_(MLX90393Setting stage) { if (!is_setting_ok) { if (this->mlx_.checkStatus(this->mlx_.reset()) != MLX90393::STATUS_OK) { - ESP_LOGE(TAG, "failed to reset device"); - this->status_set_error(); - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to reset device")); return; } if (!this->apply_all_settings_()) { - ESP_LOGE(TAG, "failed to re-apply settings"); - this->status_set_error(); - this->mark_failed(); + this->mark_failed(LOG_STR("Failed to re-apply settings")); } else { ESP_LOGI(TAG, "reset and re-apply settings completed"); } diff --git a/esphome/components/mmc5603/mmc5603.cpp b/esphome/components/mmc5603/mmc5603.cpp index b7d16b30edd..fa5ba5985d7 100644 --- a/esphome/components/mmc5603/mmc5603.cpp +++ b/esphome/components/mmc5603/mmc5603.cpp @@ -34,35 +34,27 @@ static const uint8_t MMC5603_ODR_REG = 0x1A; void MMC5603Component::setup() { uint8_t id = 0; if (!this->read_byte(MMC56X3_PRODUCT_ID, &id)) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } if (id != 0 && id != MMC56X3_CHIP_ID) { // ID is not reported correctly by all chips, 0 on some chips - ESP_LOGCONFIG(TAG, "Chip Wrong"); - this->error_code_ = ID_REGISTERS; - this->mark_failed(); + this->mark_failed(LOG_STR("The ID registers don't match - Is this really an MMC5603?")); return; } if (!this->write_byte(MMC56X3_CTRL1_REG, 0x80)) { // turn on set bit - ESP_LOGCONFIG(TAG, "Control 1 Failed for set bit"); - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR("Control 1 failed for set bit")); return; } if (!this->write_byte(MMC56X3_CTRL0_REG, 0x08)) { // turn on set bit - ESP_LOGCONFIG(TAG, "Control 0 Failed for set bit"); - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR("Control 0 failed for set bit")); return; } if (!this->write_byte(MMC56X3_CTRL0_REG, 0x10)) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } @@ -70,19 +62,13 @@ void MMC5603Component::setup() { ctrl_2 &= ~0x10; // turn off cmm_en bit if (!this->write_byte(MMC56X3_CTRL2_REG, ctrl_2)) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } void MMC5603Component::dump_config() { ESP_LOGCONFIG(TAG, "MMC5603:"); LOG_I2C_DEVICE(this); - if (this->error_code_ == COMMUNICATION_FAILED) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } else if (this->error_code_ == ID_REGISTERS) { - ESP_LOGE(TAG, "The ID registers don't match - Is this really an MMC5603?"); - } LOG_UPDATE_INTERVAL(this); ESP_LOGCONFIG(TAG, " Auto set/reset: %s", ONOFF(this->auto_set_reset_)); diff --git a/esphome/components/mmc5603/mmc5603.h b/esphome/components/mmc5603/mmc5603.h index d291e6d2728..9187e019fb3 100644 --- a/esphome/components/mmc5603/mmc5603.h +++ b/esphome/components/mmc5603/mmc5603.h @@ -32,11 +32,6 @@ class MMC5603Component final : public PollingComponent, public i2c::I2CDevice { sensor::Sensor *z_sensor_{nullptr}; sensor::Sensor *heading_sensor_{nullptr}; bool auto_set_reset_{true}; - enum ErrorCode { - NONE = 0, - COMMUNICATION_FAILED, - ID_REGISTERS, - } error_code_{NONE}; }; } // namespace esphome::mmc5603 diff --git a/esphome/components/mpl3115a2/mpl3115a2.cpp b/esphome/components/mpl3115a2/mpl3115a2.cpp index db892716258..4a206b30dfe 100644 --- a/esphome/components/mpl3115a2/mpl3115a2.cpp +++ b/esphome/components/mpl3115a2/mpl3115a2.cpp @@ -10,13 +10,11 @@ ESPHOME_LOG_TAG(TAG, "mpl3115a2"); void MPL3115A2Component::setup() { uint8_t whoami = 0xFF; if (!this->read_byte(MPL3115A2_WHOAMI, &whoami)) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } if (whoami != 0xC4) { - this->error_code_ = WRONG_ID; - this->mark_failed(); + this->mark_failed(LOG_STR("MPL3115A2 has invalid id")); return; } @@ -32,19 +30,6 @@ void MPL3115A2Component::setup() { void MPL3115A2Component::dump_config() { ESP_LOGCONFIG(TAG, "MPL3115A2:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - switch (this->error_code_) { - case COMMUNICATION_FAILED: - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - break; - case WRONG_ID: - ESP_LOGE(TAG, "MPL3115A2 has invalid id"); - break; - default: - ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!"); - break; - } - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Temperature", this->temperature_); LOG_SENSOR(" ", "Pressure", this->pressure_); diff --git a/esphome/components/mpl3115a2/mpl3115a2.h b/esphome/components/mpl3115a2/mpl3115a2.h index a6163673cbf..a8af1ff0fea 100644 --- a/esphome/components/mpl3115a2/mpl3115a2.h +++ b/esphome/components/mpl3115a2/mpl3115a2.h @@ -94,11 +94,6 @@ class MPL3115A2Component final : public PollingComponent, public i2c::I2CDevice sensor::Sensor *temperature_{nullptr}; sensor::Sensor *altitude_{nullptr}; sensor::Sensor *pressure_{nullptr}; - enum ErrorCode { - NONE = 0, - COMMUNICATION_FAILED, - WRONG_ID, - } error_code_{NONE}; }; } // namespace esphome::mpl3115a2 diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index 26cdcae6138..3d9cded59ff 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -15,8 +15,7 @@ void MPR121Component::setup() { this->write_byte(MPR121_SOFTRESET, 0x63); this->set_timeout(100, [this]() { if (!this->write_byte(MPR121_ECR, 0x0)) { - this->error_code_ = COMMUNICATION_FAILED; - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } // set touch sensitivity for all 12 channels @@ -70,14 +69,6 @@ void MPR121Component::set_release_debounce(uint8_t debounce) { void MPR121Component::dump_config() { ESP_LOGCONFIG(TAG, "MPR121:"); LOG_I2C_DEVICE(this); - switch (this->error_code_) { - case COMMUNICATION_FAILED: - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - break; - case NONE: - default: - break; - } } void MPR121Component::loop() { uint16_t val = 0; diff --git a/esphome/components/mpr121/mpr121.h b/esphome/components/mpr121/mpr121.h index 64c4b291b3d..4d945ded872 100644 --- a/esphome/components/mpr121/mpr121.h +++ b/esphome/components/mpr121/mpr121.h @@ -84,10 +84,6 @@ class MPR121Component final : public Component, public i2c::I2CDevice { uint8_t touch_threshold_{}; uint8_t release_threshold_{}; uint8_t max_touch_channel_{3}; - enum ErrorCode { - NONE = 0, - COMMUNICATION_FAILED, - } error_code_{NONE}; bool flush_gpio_(); diff --git a/esphome/components/mpu6050/mpu6050.cpp b/esphome/components/mpu6050/mpu6050.cpp index cf393fd4d10..364843ebdce 100644 --- a/esphome/components/mpu6050/mpu6050.cpp +++ b/esphome/components/mpu6050/mpu6050.cpp @@ -20,19 +20,22 @@ const uint8_t MPU6050_BIT_TEMPERATURE_DISABLED = 3; const float GRAVITY_EARTH = 9.80665f; void MPU6050Component::setup() { + if (!this->setup_registers_()) + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); +} + +bool MPU6050Component::setup_registers_() { uint8_t who_am_i; if (!this->read_byte(MPU6050_REGISTER_WHO_AM_I, &who_am_i) || (who_am_i != 0x68 && who_am_i != 0x70 && who_am_i != 0x98)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Power Management"); // Setup power management uint8_t power_management; if (!this->read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management)); // Set clock source - X-Gyro @@ -44,48 +47,38 @@ void MPU6050Component::setup() { power_management &= ~(1 << MPU6050_BIT_TEMPERATURE_DISABLED); ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management)); if (!this->write_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, power_management)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Gyro Config"); // Set scale - 2000DPS uint8_t gyro_config; if (!this->read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config)); gyro_config &= 0b11100111; gyro_config |= MPU6050_SCALE_2000_DPS << 3; ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config)); if (!this->write_byte(MPU6050_REGISTER_GYRO_CONFIG, gyro_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Accel Config"); // Set range - 2G uint8_t accel_config; if (!this->read_byte(MPU6050_REGISTER_ACCEL_CONFIG, &accel_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config)); accel_config &= 0b11100111; accel_config |= (MPU6050_RANGE_2G << 3); ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config)); - if (!this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config)) { - this->mark_failed(); - return; - } + return this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config); } void MPU6050Component::dump_config() { ESP_LOGCONFIG(TAG, "MPU6050:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_); LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_); diff --git a/esphome/components/mpu6050/mpu6050.h b/esphome/components/mpu6050/mpu6050.h index 4410bf01645..2a346c1f4d9 100644 --- a/esphome/components/mpu6050/mpu6050.h +++ b/esphome/components/mpu6050/mpu6050.h @@ -22,6 +22,8 @@ class MPU6050Component final : public PollingComponent, public i2c::I2CDevice { void set_gyro_z_sensor(sensor::Sensor *gyro_z_sensor) { gyro_z_sensor_ = gyro_z_sensor; } protected: + /// Configure the sensor; false on an I2C failure. + bool setup_registers_(); sensor::Sensor *accel_x_sensor_{nullptr}; sensor::Sensor *accel_y_sensor_{nullptr}; sensor::Sensor *accel_z_sensor_{nullptr}; diff --git a/esphome/components/mpu6886/mpu6886.cpp b/esphome/components/mpu6886/mpu6886.cpp index 514ff0dd0e7..15fbd3c9c16 100644 --- a/esphome/components/mpu6886/mpu6886.cpp +++ b/esphome/components/mpu6886/mpu6886.cpp @@ -25,18 +25,21 @@ const float TEMPERATURE_SENSITIVITY = 326.8; const float TEMPERATURE_OFFSET = 25.0; void MPU6886Component::setup() { + if (!this->setup_registers_()) + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); +} + +bool MPU6886Component::setup_registers_() { uint8_t who_am_i; if (!this->read_byte(MPU6886_REGISTER_WHO_AM_I, &who_am_i) || who_am_i != MPU6886_WHO_AM_I_IDENTIFIER) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Power Management"); // Setup power management uint8_t power_management; if (!this->read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management)); // Set clock source - X-Gyro @@ -48,49 +51,39 @@ void MPU6886Component::setup() { power_management &= ~(1 << MPU6886_BIT_TEMPERATURE_DISABLED); ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management)); if (!this->write_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, power_management)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Gyroscope Config"); // Set scale - 2000DPS uint8_t gyro_config; if (!this->read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input gyroscope_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config)); gyro_config &= 0b11100111; gyro_config |= MPU6886_SCALE_2000_DPS << 3; ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config)); if (!this->write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Setting up Accelerometer Config"); // Set range - 2G uint8_t accel_config; if (!this->read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) { - this->mark_failed(); - return; + return false; } ESP_LOGV(TAG, " Input accelerometer_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config)); accel_config &= 0b11100111; accel_config |= (MPU6886_RANGE_2G << 3); ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config)); - if (!this->write_byte(MPU6886_REGISTER_ACCEL_CONFIG, accel_config)) { - this->mark_failed(); - return; - } + return this->write_byte(MPU6886_REGISTER_ACCEL_CONFIG, accel_config); } void MPU6886Component::dump_config() { ESP_LOGCONFIG(TAG, "MPU6886:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_); LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_); diff --git a/esphome/components/mpu6886/mpu6886.h b/esphome/components/mpu6886/mpu6886.h index b795d5f690f..a7c81e17593 100644 --- a/esphome/components/mpu6886/mpu6886.h +++ b/esphome/components/mpu6886/mpu6886.h @@ -22,6 +22,8 @@ class MPU6886Component final : public PollingComponent, public i2c::I2CDevice { void set_gyro_z_sensor(sensor::Sensor *gyro_z_sensor) { gyro_z_sensor_ = gyro_z_sensor; } protected: + /// Configure the sensor; false on an I2C failure. + bool setup_registers_(); sensor::Sensor *accel_x_sensor_{nullptr}; sensor::Sensor *accel_y_sensor_{nullptr}; sensor::Sensor *accel_z_sensor_{nullptr}; diff --git a/esphome/components/ms5611/ms5611.cpp b/esphome/components/ms5611/ms5611.cpp index 8f66d704fe6..e3e5194c415 100644 --- a/esphome/components/ms5611/ms5611.cpp +++ b/esphome/components/ms5611/ms5611.cpp @@ -18,13 +18,13 @@ static const uint8_t MS5611_CMD_READ_PROM = 0xA2; void MS5611Component::setup() { if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } this->set_timeout(100, [this]() { for (uint8_t offset = 0; offset < 6; offset++) { if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) { - this->mark_failed(); + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } } @@ -33,9 +33,6 @@ void MS5611Component::setup() { void MS5611Component::dump_config() { ESP_LOGCONFIG(TAG, "MS5611:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); diff --git a/esphome/components/msa3xx/msa3xx.cpp b/esphome/components/msa3xx/msa3xx.cpp index 78f2b75c9ce..19a4b067bc7 100644 --- a/esphome/components/msa3xx/msa3xx.cpp +++ b/esphome/components/msa3xx/msa3xx.cpp @@ -118,9 +118,13 @@ const char *orientation_z_to_string(bool orientation) { return orientation ? "Do void MSA3xxComponent::setup() { uint8_t part_id{0xff}; - if (!this->read_byte(static_cast(RegisterMap::PART_ID), &part_id) || (part_id != MSA_3XX_PART_ID)) { - ESP_LOGE(TAG, "Part ID is wrong or missing. Got 0x%02X", part_id); - this->mark_failed(); + if (!this->read_byte(static_cast(RegisterMap::PART_ID), &part_id)) { + this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); + return; + } + if (part_id != MSA_3XX_PART_ID) { + ESP_LOGE(TAG, "Part ID is wrong. Got 0x%02X", part_id); + this->mark_failed(LOG_STR("Wrong chip ID")); return; } @@ -137,8 +141,7 @@ void MSA3xxComponent::setup() { this->device_params_.accel_data_width = 12; this->device_params_.scale_factor_exp = static_cast(this->range_) - 10; } else { - ESP_LOGE(TAG, "Unknown model"); - this->mark_failed(); + this->mark_failed(LOG_STR("Unknown model")); return; } @@ -155,9 +158,6 @@ void MSA3xxComponent::setup() { void MSA3xxComponent::dump_config() { ESP_LOGCONFIG(TAG, "MSA3xx:"); LOG_I2C_DEVICE(this); - if (this->is_failed()) { - ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); - } ESP_LOGCONFIG(TAG, " Model: %s\n" " Power Mode: %s\n"