[components] Let core report communication failures in dump_config (3/5) (#20872)

This commit is contained in:
J. Nick Koston
2026-10-16 08:40:54 -04:00
committed by GitHub
parent e1a6c615b1
commit 49caeae780
38 changed files with 107 additions and 282 deletions
+4 -6
View File
@@ -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_);
+4 -6
View File
@@ -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,
+3 -21
View File
@@ -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() {
-7
View File
@@ -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
@@ -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"
+4 -7
View File
@@ -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) {
@@ -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_);
+1 -4
View File
@@ -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) {
@@ -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) {
+6 -5
View File
@@ -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<uint8_t>(RegisterMap::WHO_AM_I), &who_am_i) || who_am_i != LIS2DW12_WHO_AM_I_VALUE) {
if (!this->read_byte(static_cast<uint8_t>(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"
+4 -8
View File
@@ -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"
-3
View File
@@ -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);
}
+2 -5
View File
@@ -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) {
+1 -5
View File
@@ -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_()) {
+1 -5
View File
@@ -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_()) {
+2 -8
View File
@@ -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_);
-3
View File
@@ -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_);
+1 -4
View File
@@ -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() {
+3 -6
View File
@@ -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();
+2 -9
View File
@@ -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) {
-1
View File
@@ -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
+1 -4
View File
@@ -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() {
+2 -16
View File
@@ -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() {
-6
View File
@@ -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
+12 -7
View File
@@ -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);
}
@@ -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");
}
+6 -20
View File
@@ -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_));
-5
View File
@@ -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
+2 -17
View File
@@ -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_);
-5
View File
@@ -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
+1 -10
View File
@@ -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;
-4
View File
@@ -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_();
+12 -19
View File
@@ -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_);
+2
View File
@@ -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};
+12 -19
View File
@@ -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_);
+2
View File
@@ -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};
+2 -5
View File
@@ -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_);
+8 -8
View File
@@ -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<uint8_t>(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<uint8_t>(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<uint8_t>(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"