From 80b4d52f379e683fe12b0952fd90774abf8453ad Mon Sep 17 00:00:00 2001 From: assaf Date: Wed, 25 Dec 2024 22:39:59 -0800 Subject: [PATCH 1/7] add bus voltage over limit for ina2xx sensors --- esphome/components/ina2xx_base/__init__.py | 12 ++++++++++++ esphome/components/ina2xx_base/ina2xx_base.cpp | 13 +++++++++++++ esphome/components/ina2xx_base/ina2xx_base.h | 3 +++ esphome/const.py | 1 + 4 files changed, 29 insertions(+) diff --git a/esphome/components/ina2xx_base/__init__.py b/esphome/components/ina2xx_base/__init__.py index 35b5baa83e..ac4966ae0a 100644 --- a/esphome/components/ina2xx_base/__init__.py +++ b/esphome/components/ina2xx_base/__init__.py @@ -12,6 +12,7 @@ from esphome.const import ( CONF_SHUNT_RESISTANCE, CONF_SHUNT_VOLTAGE, CONF_TEMPERATURE, + CONF_BOVL, DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, @@ -34,6 +35,7 @@ CONF_CHARGE = "charge" CONF_CHARGE_COULOMBS = "charge_coulombs" CONF_ENERGY_JOULES = "energy_joules" CONF_TEMPERATURE_COEFFICIENT = "temperature_coefficient" +CONF_BOVL = "bus_voltage_over_limit" UNIT_AMPERE_HOURS = "Ah" UNIT_COULOMB = "C" UNIT_JOULE = "J" @@ -90,6 +92,11 @@ def validate_model_config(config): f"Device model '{model}' does not support temperature coefficient" ) + if config.get(CONF_BOVL) and model not in ["INA228", "INA229"]: + raise cv.Invalid( + f"Device model '{model}' does not support bus voltage over limit" + ) + return config @@ -193,6 +200,8 @@ INA2XX_SCHEMA = cv.Schema( ), key=CONF_NAME, ), + cv.Optional(CONF_BOVL): cv.All(cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000)), + } ).extend(cv.polling_component_schema("60s")) @@ -253,3 +262,6 @@ async def setup_ina2xx(var, config): if conf := config.get(CONF_CHARGE_COULOMBS): sens = await sensor.new_sensor(conf) cg.add(var.set_charge_sensor_c(sens)) + + if conf := config.get(CONF_BOVL): + cg.add(var.set_bus_voltage_over_limit_v(conf)) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 924bf91e5e..4ae7bb50be 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -78,6 +78,9 @@ void INA2XX::setup() { this->configure_shunt_tempco_(); delay(1); + this->configure_bovl_(); + delay(1); + this->state_ = State::IDLE; } @@ -212,6 +215,7 @@ void INA2XX::dump_config() { ESP_LOGCONFIG(TAG, " ADCRANGE = %d (%s)", (uint8_t) this->adc_range_, this->adc_range_ ? "±40.96 mV" : "±163.84 mV"); ESP_LOGCONFIG(TAG, " CURRENT_LSB = %f", this->current_lsb_); ESP_LOGCONFIG(TAG, " SHUNT_CAL = %d", this->shunt_cal_); + ESP_LOGCONFIG(TAG, " BOVL = %f V", this->bovl_v_); ESP_LOGCONFIG(TAG, " ADC Samples = %d; ADC times: Bus = %d μs, Shunt = %d μs, Temp = %d μs", ADC_SAMPLES[0b111 & (uint8_t) this->adc_avg_samples_], @@ -374,6 +378,15 @@ bool INA2XX::configure_shunt_tempco_() { return true; } +bool INA2XX::configure_bovl_() { + // Only for 228/229 + if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && + this->bovl_v_ > 0) { + return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t)(this->bovl_v_ * 1000 / 3.125)); + } + return true; +} + bool INA2XX::read_shunt_voltage_mv_(float &volt_out) { // Two's complement value // 228, 229 - 24bit: 20(23-4) + 4(3-0) res diff --git a/esphome/components/ina2xx_base/ina2xx_base.h b/esphome/components/ina2xx_base/ina2xx_base.h index 261c5321bf..6abb5d2ad8 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.h +++ b/esphome/components/ina2xx_base/ina2xx_base.h @@ -127,6 +127,7 @@ class INA2XX : public PollingComponent { void set_adc_time_die_temperature(AdcTime time) { this->adc_time_die_temperature_ = time; } void set_adc_avg_samples(AdcAvgSamples samples) { this->adc_avg_samples_ = samples; } void set_shunt_tempco(uint16_t coeff) { this->shunt_tempco_ppm_c_ = coeff; } + void set_bus_voltage_over_limit_v(float bovl_v) { this->bovl_v_ = bovl_v; } void set_shunt_voltage_sensor(sensor::Sensor *sensor) { this->shunt_voltage_sensor_ = sensor; } void set_bus_voltage_sensor(sensor::Sensor *sensor) { this->bus_voltage_sensor_ = sensor; } @@ -150,6 +151,7 @@ class INA2XX : public PollingComponent { bool configure_shunt_(); bool configure_shunt_tempco_(); bool configure_adc_range_(); + bool configure_bovl_(); bool read_shunt_voltage_mv_(float &volt_out); bool read_bus_voltage_(float &volt_out); @@ -172,6 +174,7 @@ class INA2XX : public PollingComponent { AdcTime adc_time_die_temperature_{AdcTime::ADC_TIME_4120US}; AdcAvgSamples adc_avg_samples_{AdcAvgSamples::ADC_AVG_SAMPLES_128}; uint16_t shunt_tempco_ppm_c_{0}; + float bovl_v_; // // Calculated coefficients diff --git a/esphome/const.py b/esphome/const.py index 0f41dc1aec..3aac2b90b6 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -98,6 +98,7 @@ CONF_BLUE = "blue" CONF_BOARD = "board" CONF_BOARD_FLASH_MODE = "board_flash_mode" CONF_BORDER = "border" +CONF_BOVL = "bus_voltage_over_limit" CONF_BRANCH = "branch" CONF_BRIGHTNESS = "brightness" CONF_BRIGHTNESS_LIMITS = "brightness_limits" From f32641c00e3f5e6db377d7fc475c7f5229db8c51 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Thu, 26 Dec 2024 20:55:27 -0800 Subject: [PATCH 2/7] rename CONF_BOVL to CONF_BUS_VOLTAGE_OVER_LIMIT --- esphome/components/ina2xx_base/__init__.py | 9 ++++----- esphome/const.py | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/esphome/components/ina2xx_base/__init__.py b/esphome/components/ina2xx_base/__init__.py index ac4966ae0a..e6c8839b59 100644 --- a/esphome/components/ina2xx_base/__init__.py +++ b/esphome/components/ina2xx_base/__init__.py @@ -12,7 +12,6 @@ from esphome.const import ( CONF_SHUNT_RESISTANCE, CONF_SHUNT_VOLTAGE, CONF_TEMPERATURE, - CONF_BOVL, DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, @@ -31,11 +30,11 @@ CODEOWNERS = ["@latonita"] CONF_ADC_AVERAGING = "adc_averaging" CONF_ADC_RANGE = "adc_range" CONF_ADC_TIME = "adc_time" +CONF_BUS_VOLTAGE_OVER_LIMIT = "bus_voltage_over_limit" CONF_CHARGE = "charge" CONF_CHARGE_COULOMBS = "charge_coulombs" CONF_ENERGY_JOULES = "energy_joules" CONF_TEMPERATURE_COEFFICIENT = "temperature_coefficient" -CONF_BOVL = "bus_voltage_over_limit" UNIT_AMPERE_HOURS = "Ah" UNIT_COULOMB = "C" UNIT_JOULE = "J" @@ -92,7 +91,7 @@ def validate_model_config(config): f"Device model '{model}' does not support temperature coefficient" ) - if config.get(CONF_BOVL) and model not in ["INA228", "INA229"]: + if config.get(CONF_BUS_VOLTAGE_OVER_LIMIT) and model not in ["INA228", "INA229"]: raise cv.Invalid( f"Device model '{model}' does not support bus voltage over limit" ) @@ -200,7 +199,7 @@ INA2XX_SCHEMA = cv.Schema( ), key=CONF_NAME, ), - cv.Optional(CONF_BOVL): cv.All(cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000)), + cv.Optional(CONF_BUS_VOLTAGE_OVER_LIMIT): cv.All(cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000)), } ).extend(cv.polling_component_schema("60s")) @@ -263,5 +262,5 @@ async def setup_ina2xx(var, config): sens = await sensor.new_sensor(conf) cg.add(var.set_charge_sensor_c(sens)) - if conf := config.get(CONF_BOVL): + if conf := config.get(CONF_BUS_VOLTAGE_OVER_LIMIT): cg.add(var.set_bus_voltage_over_limit_v(conf)) diff --git a/esphome/const.py b/esphome/const.py index 3aac2b90b6..0f41dc1aec 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -98,7 +98,6 @@ CONF_BLUE = "blue" CONF_BOARD = "board" CONF_BOARD_FLASH_MODE = "board_flash_mode" CONF_BORDER = "border" -CONF_BOVL = "bus_voltage_over_limit" CONF_BRANCH = "branch" CONF_BRIGHTNESS = "brightness" CONF_BRIGHTNESS_LIMITS = "brightness_limits" From 5f3714651c2648fb5e2b12a408891f0896477d42 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Thu, 26 Dec 2024 20:56:17 -0800 Subject: [PATCH 3/7] formatting --- esphome/components/ina2xx_base/__init__.py | 5 +++-- esphome/components/ina2xx_base/ina2xx_base.cpp | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/esphome/components/ina2xx_base/__init__.py b/esphome/components/ina2xx_base/__init__.py index e6c8839b59..ab5fe8a7fb 100644 --- a/esphome/components/ina2xx_base/__init__.py +++ b/esphome/components/ina2xx_base/__init__.py @@ -199,8 +199,9 @@ INA2XX_SCHEMA = cv.Schema( ), key=CONF_NAME, ), - cv.Optional(CONF_BUS_VOLTAGE_OVER_LIMIT): cv.All(cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000)), - + cv.Optional(CONF_BUS_VOLTAGE_OVER_LIMIT): cv.All( + cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000) + ), } ).extend(cv.polling_component_schema("60s")) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 4ae7bb50be..b2311fb027 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -380,8 +380,7 @@ bool INA2XX::configure_shunt_tempco_() { bool INA2XX::configure_bovl_() { // Only for 228/229 - if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && - this->bovl_v_ > 0) { + if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bovl_v_ > 0) { return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t)(this->bovl_v_ * 1000 / 3.125)); } return true; From d136ec96b83c54946768a778b8385b790d374a08 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:00:12 -0800 Subject: [PATCH 4/7] formatting --- esphome/components/ina2xx_base/__init__.py | 2 +- esphome/components/ina2xx_base/ina2xx_base.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/ina2xx_base/__init__.py b/esphome/components/ina2xx_base/__init__.py index ab5fe8a7fb..429988c8ed 100644 --- a/esphome/components/ina2xx_base/__init__.py +++ b/esphome/components/ina2xx_base/__init__.py @@ -200,7 +200,7 @@ INA2XX_SCHEMA = cv.Schema( key=CONF_NAME, ), cv.Optional(CONF_BUS_VOLTAGE_OVER_LIMIT): cv.All( - cv.voltage, cv.Range(min=0.0, max=0x7fff * 3.125 / 1000) + cv.voltage, cv.Range(min=0.0, max=0x7FFF * 3.125 / 1000) ), } ).extend(cv.polling_component_schema("60s")) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index b2311fb027..ee2a3e4642 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -381,7 +381,7 @@ bool INA2XX::configure_shunt_tempco_() { bool INA2XX::configure_bovl_() { // Only for 228/229 if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bovl_v_ > 0) { - return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t)(this->bovl_v_ * 1000 / 3.125)); + return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t) (this->bovl_v_ * 1000 / 3.125)); } return true; } From 4babb7a285219db5f9f14ca6403d75ac60329a62 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Sat, 18 Jan 2025 11:33:42 -0800 Subject: [PATCH 5/7] rename configure_bovl_ to configure_bus_voltage_over_limit_ --- esphome/components/ina2xx_base/ina2xx_base.cpp | 4 ++-- esphome/components/ina2xx_base/ina2xx_base.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index ee2a3e4642..9aea43ccb4 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -78,7 +78,7 @@ void INA2XX::setup() { this->configure_shunt_tempco_(); delay(1); - this->configure_bovl_(); + this->configure_bus_voltage_over_limit_(); delay(1); this->state_ = State::IDLE; @@ -378,7 +378,7 @@ bool INA2XX::configure_shunt_tempco_() { return true; } -bool INA2XX::configure_bovl_() { +bool INA2XX::configure_bus_voltage_over_limit_() { // Only for 228/229 if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bovl_v_ > 0) { return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t) (this->bovl_v_ * 1000 / 3.125)); diff --git a/esphome/components/ina2xx_base/ina2xx_base.h b/esphome/components/ina2xx_base/ina2xx_base.h index 6abb5d2ad8..72eecc959d 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.h +++ b/esphome/components/ina2xx_base/ina2xx_base.h @@ -151,7 +151,7 @@ class INA2XX : public PollingComponent { bool configure_shunt_(); bool configure_shunt_tempco_(); bool configure_adc_range_(); - bool configure_bovl_(); + bool configure_bus_voltage_over_limit_(); bool read_shunt_voltage_mv_(float &volt_out); bool read_bus_voltage_(float &volt_out); From a75bc504aaacc30546244f2caeeac66499050f74 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:16:24 -0800 Subject: [PATCH 6/7] rename bovl_v_ to bus_voltage_over_limit_v_ --- esphome/components/ina2xx_base/ina2xx_base.cpp | 6 +++--- esphome/components/ina2xx_base/ina2xx_base.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 9aea43ccb4..004c3da9d8 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -215,7 +215,7 @@ void INA2XX::dump_config() { ESP_LOGCONFIG(TAG, " ADCRANGE = %d (%s)", (uint8_t) this->adc_range_, this->adc_range_ ? "±40.96 mV" : "±163.84 mV"); ESP_LOGCONFIG(TAG, " CURRENT_LSB = %f", this->current_lsb_); ESP_LOGCONFIG(TAG, " SHUNT_CAL = %d", this->shunt_cal_); - ESP_LOGCONFIG(TAG, " BOVL = %f V", this->bovl_v_); + ESP_LOGCONFIG(TAG, " BUS_VOLTAGE_OVER_LIMIT = %f V", this->bus_voltage_over_limit_v_); ESP_LOGCONFIG(TAG, " ADC Samples = %d; ADC times: Bus = %d μs, Shunt = %d μs, Temp = %d μs", ADC_SAMPLES[0b111 & (uint8_t) this->adc_avg_samples_], @@ -380,8 +380,8 @@ bool INA2XX::configure_shunt_tempco_() { bool INA2XX::configure_bus_voltage_over_limit_() { // Only for 228/229 - if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bovl_v_ > 0) { - return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t) (this->bovl_v_ * 1000 / 3.125)); + if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bus_voltage_over_limit_v_ > 0) { + return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t) (this->bus_voltage_over_limit_v_ * 1000 / 3.125)); } return true; } diff --git a/esphome/components/ina2xx_base/ina2xx_base.h b/esphome/components/ina2xx_base/ina2xx_base.h index 72eecc959d..1e0153a845 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.h +++ b/esphome/components/ina2xx_base/ina2xx_base.h @@ -127,7 +127,7 @@ class INA2XX : public PollingComponent { void set_adc_time_die_temperature(AdcTime time) { this->adc_time_die_temperature_ = time; } void set_adc_avg_samples(AdcAvgSamples samples) { this->adc_avg_samples_ = samples; } void set_shunt_tempco(uint16_t coeff) { this->shunt_tempco_ppm_c_ = coeff; } - void set_bus_voltage_over_limit_v(float bovl_v) { this->bovl_v_ = bovl_v; } + void set_bus_voltage_over_limit_v(float limit_v) { this->bus_voltage_over_limit_v_ = limit_v; } void set_shunt_voltage_sensor(sensor::Sensor *sensor) { this->shunt_voltage_sensor_ = sensor; } void set_bus_voltage_sensor(sensor::Sensor *sensor) { this->bus_voltage_sensor_ = sensor; } @@ -174,7 +174,7 @@ class INA2XX : public PollingComponent { AdcTime adc_time_die_temperature_{AdcTime::ADC_TIME_4120US}; AdcAvgSamples adc_avg_samples_{AdcAvgSamples::ADC_AVG_SAMPLES_128}; uint16_t shunt_tempco_ppm_c_{0}; - float bovl_v_; + float bus_voltage_over_limit_v_; // // Calculated coefficients From 646097f9cecb74749f83e5b0ef0e42ecc1956012 Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:35:41 -0800 Subject: [PATCH 7/7] formatting --- esphome/components/ina2xx_base/ina2xx_base.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/ina2xx_base/ina2xx_base.cpp b/esphome/components/ina2xx_base/ina2xx_base.cpp index 004c3da9d8..aa909f650b 100644 --- a/esphome/components/ina2xx_base/ina2xx_base.cpp +++ b/esphome/components/ina2xx_base/ina2xx_base.cpp @@ -380,7 +380,8 @@ bool INA2XX::configure_shunt_tempco_() { bool INA2XX::configure_bus_voltage_over_limit_() { // Only for 228/229 - if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && this->bus_voltage_over_limit_v_ > 0) { + if ((this->ina_model_ == INAModel::INA_228 || this->ina_model_ == INAModel::INA_229) && + this->bus_voltage_over_limit_v_ > 0) { return this->write_unsigned_16_(RegisterMap::REG_BOVL, (uint16_t) (this->bus_voltage_over_limit_v_ * 1000 / 3.125)); } return true;