mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Correct offset calibration (#7228)
Co-authored-by: descipher <mike.laspina@gelidus.ca> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -46,6 +46,7 @@ esphome/components/async_tcp/* @OttoWinter | |||||||
| esphome/components/at581x/* @X-Ryl669 | esphome/components/at581x/* @X-Ryl669 | ||||||
| esphome/components/atc_mithermometer/* @ahpohl | esphome/components/atc_mithermometer/* @ahpohl | ||||||
| esphome/components/atm90e26/* @danieltwagner | esphome/components/atm90e26/* @danieltwagner | ||||||
|  | esphome/components/atm90e32/* @circuitsetup @descipher | ||||||
| esphome/components/b_parasite/* @rbaron | esphome/components/b_parasite/* @rbaron | ||||||
| esphome/components/ballu/* @bazuchan | esphome/components/ballu/* @bazuchan | ||||||
| esphome/components/bang_bang/* @OttoWinter | esphome/components/bang_bang/* @OttoWinter | ||||||
|   | |||||||
| @@ -0,0 +1,7 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
|  |  | ||||||
|  | CODEOWNERS = ["@circuitsetup", "@descipher"] | ||||||
|  |  | ||||||
|  | atm90e32_ns = cg.esphome_ns.namespace("atm90e32") | ||||||
|  |  | ||||||
|  | CONF_ATM90E32_ID = "atm90e32_id" | ||||||
|   | |||||||
| @@ -132,10 +132,77 @@ void ATM90E32Component::update() { | |||||||
|   this->status_clear_warning(); |   this->status_clear_warning(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void ATM90E32Component::restore_calibrations_() { | ||||||
|  |   if (enable_offset_calibration_) { | ||||||
|  |     this->pref_.load(&this->offset_phase_); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | void ATM90E32Component::run_offset_calibrations() { | ||||||
|  |   // Run the calibrations and | ||||||
|  |   // Setup voltage and current calibration offsets for PHASE A | ||||||
|  |   this->offset_phase_[PHASEA].voltage_offset_ = calibrate_voltage_offset_phase(PHASEA); | ||||||
|  |   this->phase_[PHASEA].voltage_offset_ = this->offset_phase_[PHASEA].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETA, this->phase_[PHASEA].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEA].current_offset_ = calibrate_current_offset_phase(PHASEA); | ||||||
|  |   this->phase_[PHASEA].current_offset_ = this->offset_phase_[PHASEA].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETA, this->phase_[PHASEA].current_offset_);  // C Current offset | ||||||
|  |   // Setup voltage and current calibration offsets for PHASE B | ||||||
|  |   this->offset_phase_[PHASEB].voltage_offset_ = calibrate_voltage_offset_phase(PHASEB); | ||||||
|  |   this->phase_[PHASEB].voltage_offset_ = this->offset_phase_[PHASEB].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETB, this->phase_[PHASEB].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEB].current_offset_ = calibrate_current_offset_phase(PHASEB); | ||||||
|  |   this->phase_[PHASEB].current_offset_ = this->offset_phase_[PHASEB].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETB, this->phase_[PHASEB].current_offset_);  // C Current offset | ||||||
|  |   // Setup voltage and current calibration offsets for PHASE C | ||||||
|  |   this->offset_phase_[PHASEC].voltage_offset_ = calibrate_voltage_offset_phase(PHASEC); | ||||||
|  |   this->phase_[PHASEC].voltage_offset_ = this->offset_phase_[PHASEC].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETC, this->phase_[PHASEC].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEC].current_offset_ = calibrate_current_offset_phase(PHASEC); | ||||||
|  |   this->phase_[PHASEC].current_offset_ = this->offset_phase_[PHASEC].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETC, this->phase_[PHASEC].current_offset_);  // C Current offset | ||||||
|  |   this->pref_.save(&this->offset_phase_); | ||||||
|  |   ESP_LOGI(TAG, "PhaseA Vo=%5d PhaseB Vo=%5d PhaseC Vo=%5d", this->offset_phase_[PHASEA].voltage_offset_, | ||||||
|  |            this->offset_phase_[PHASEB].voltage_offset_, this->offset_phase_[PHASEC].voltage_offset_); | ||||||
|  |   ESP_LOGI(TAG, "PhaseA Io=%5d PhaseB Io=%5d PhaseC Io=%5d", this->offset_phase_[PHASEA].current_offset_, | ||||||
|  |            this->offset_phase_[PHASEB].current_offset_, this->offset_phase_[PHASEC].current_offset_); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void ATM90E32Component::clear_offset_calibrations() { | ||||||
|  |   // Clear the calibrations and | ||||||
|  |   this->offset_phase_[PHASEA].voltage_offset_ = 0; | ||||||
|  |   this->phase_[PHASEA].voltage_offset_ = this->offset_phase_[PHASEA].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETA, this->phase_[PHASEA].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEA].current_offset_ = 0; | ||||||
|  |   this->phase_[PHASEA].current_offset_ = this->offset_phase_[PHASEA].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETA, this->phase_[PHASEA].current_offset_);  // C Current offset | ||||||
|  |   this->offset_phase_[PHASEB].voltage_offset_ = 0; | ||||||
|  |   this->phase_[PHASEB].voltage_offset_ = this->offset_phase_[PHASEB].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETB, this->phase_[PHASEB].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEB].current_offset_ = 0; | ||||||
|  |   this->phase_[PHASEB].current_offset_ = this->offset_phase_[PHASEB].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETB, this->phase_[PHASEB].current_offset_);  // C Current offset | ||||||
|  |   this->offset_phase_[PHASEC].voltage_offset_ = 0; | ||||||
|  |   this->phase_[PHASEC].voltage_offset_ = this->offset_phase_[PHASEC].voltage_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_UOFFSETC, this->phase_[PHASEC].voltage_offset_);  // C Voltage offset | ||||||
|  |   this->offset_phase_[PHASEC].current_offset_ = 0; | ||||||
|  |   this->phase_[PHASEC].current_offset_ = this->offset_phase_[PHASEC].current_offset_; | ||||||
|  |   this->write16_(ATM90E32_REGISTER_IOFFSETC, this->phase_[PHASEC].current_offset_);  // C Current offset | ||||||
|  |   this->pref_.save(&this->offset_phase_); | ||||||
|  |   ESP_LOGI(TAG, "PhaseA Vo=%5d PhaseB Vo=%5d PhaseC Vo=%5d", this->offset_phase_[PHASEA].voltage_offset_, | ||||||
|  |            this->offset_phase_[PHASEB].voltage_offset_, this->offset_phase_[PHASEC].voltage_offset_); | ||||||
|  |   ESP_LOGI(TAG, "PhaseA Io=%5d PhaseB Io=%5d PhaseC Io=%5d", this->offset_phase_[PHASEA].current_offset_, | ||||||
|  |            this->offset_phase_[PHASEB].current_offset_, this->offset_phase_[PHASEC].current_offset_); | ||||||
|  | } | ||||||
|  |  | ||||||
| void ATM90E32Component::setup() { | void ATM90E32Component::setup() { | ||||||
|   ESP_LOGCONFIG(TAG, "Setting up ATM90E32 Component..."); |   ESP_LOGCONFIG(TAG, "Setting up ATM90E32 Component..."); | ||||||
|   this->spi_setup(); |   this->spi_setup(); | ||||||
|  |   if (this->enable_offset_calibration_) { | ||||||
|  |     uint32_t hash = fnv1_hash(App.get_friendly_name()); | ||||||
|  |     this->pref_ = global_preferences->make_preference<Calibration[3]>(hash, true); | ||||||
|  |     this->restore_calibrations_(); | ||||||
|  |   } | ||||||
|   uint16_t mmode0 = 0x87;  // 3P4W 50Hz |   uint16_t mmode0 = 0x87;  // 3P4W 50Hz | ||||||
|   if (line_freq_ == 60) { |   if (line_freq_ == 60) { | ||||||
|     mmode0 |= 1 << 12;  // sets 12th bit to 1, 60Hz |     mmode0 |= 1 << 12;  // sets 12th bit to 1, 60Hz | ||||||
| @@ -167,27 +234,12 @@ void ATM90E32Component::setup() { | |||||||
|   this->write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);       // All Reactive Startup Power Threshold - 50% |   this->write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);       // All Reactive Startup Power Threshold - 50% | ||||||
|   this->write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);       // Each Phase Active Phase Threshold - 0.002A/0.00032 = 750 |   this->write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);       // Each Phase Active Phase Threshold - 0.002A/0.00032 = 750 | ||||||
|   this->write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);       // Each phase Reactive Phase Threshold - 10% |   this->write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);       // Each phase Reactive Phase Threshold - 10% | ||||||
|   // Setup voltage and current calibration offsets for PHASE A |  | ||||||
|   this->phase_[PHASEA].voltage_offset_ = calibrate_voltage_offset_phase(PHASEA); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_UOFFSETA, this->phase_[PHASEA].voltage_offset_);  // A Voltage offset |  | ||||||
|   this->phase_[PHASEA].current_offset_ = calibrate_current_offset_phase(PHASEA); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_IOFFSETA, this->phase_[PHASEA].current_offset_);  // A Current offset |  | ||||||
|   // Setup voltage and current gain for PHASE A |   // Setup voltage and current gain for PHASE A | ||||||
|   this->write16_(ATM90E32_REGISTER_UGAINA, this->phase_[PHASEA].voltage_gain_);  // A Voltage rms gain |   this->write16_(ATM90E32_REGISTER_UGAINA, this->phase_[PHASEA].voltage_gain_);  // A Voltage rms gain | ||||||
|   this->write16_(ATM90E32_REGISTER_IGAINA, this->phase_[PHASEA].ct_gain_);       // A line current gain |   this->write16_(ATM90E32_REGISTER_IGAINA, this->phase_[PHASEA].ct_gain_);       // A line current gain | ||||||
|   // Setup voltage and current calibration offsets for PHASE B |  | ||||||
|   this->phase_[PHASEB].voltage_offset_ = calibrate_voltage_offset_phase(PHASEB); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_UOFFSETB, this->phase_[PHASEB].voltage_offset_);  // B Voltage offset |  | ||||||
|   this->phase_[PHASEB].current_offset_ = calibrate_current_offset_phase(PHASEB); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_IOFFSETB, this->phase_[PHASEB].current_offset_);  // B Current offset |  | ||||||
|   // Setup voltage and current gain for PHASE B |   // Setup voltage and current gain for PHASE B | ||||||
|   this->write16_(ATM90E32_REGISTER_UGAINB, this->phase_[PHASEB].voltage_gain_);  // B Voltage rms gain |   this->write16_(ATM90E32_REGISTER_UGAINB, this->phase_[PHASEB].voltage_gain_);  // B Voltage rms gain | ||||||
|   this->write16_(ATM90E32_REGISTER_IGAINB, this->phase_[PHASEB].ct_gain_);       // B line current gain |   this->write16_(ATM90E32_REGISTER_IGAINB, this->phase_[PHASEB].ct_gain_);       // B line current gain | ||||||
|   // Setup voltage and current calibration offsets for PHASE C |  | ||||||
|   this->phase_[PHASEC].voltage_offset_ = calibrate_voltage_offset_phase(PHASEC); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_UOFFSETC, this->phase_[PHASEC].voltage_offset_);  // C Voltage offset |  | ||||||
|   this->phase_[PHASEC].current_offset_ = calibrate_current_offset_phase(PHASEC); |  | ||||||
|   this->write16_(ATM90E32_REGISTER_IOFFSETC, this->phase_[PHASEC].current_offset_);  // C Current offset |  | ||||||
|   // Setup voltage and current gain for PHASE C |   // Setup voltage and current gain for PHASE C | ||||||
|   this->write16_(ATM90E32_REGISTER_UGAINC, this->phase_[PHASEC].voltage_gain_);  // C Voltage rms gain |   this->write16_(ATM90E32_REGISTER_UGAINC, this->phase_[PHASEC].voltage_gain_);  // C Voltage rms gain | ||||||
|   this->write16_(ATM90E32_REGISTER_IGAINC, this->phase_[PHASEC].ct_gain_);       // C line current gain |   this->write16_(ATM90E32_REGISTER_IGAINC, this->phase_[PHASEC].ct_gain_);       // C line current gain | ||||||
|   | |||||||
| @@ -1,9 +1,12 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" | #include "atm90e32_reg.h" | ||||||
| #include "esphome/components/sensor/sensor.h" | #include "esphome/components/sensor/sensor.h" | ||||||
| #include "esphome/components/spi/spi.h" | #include "esphome/components/spi/spi.h" | ||||||
| #include "atm90e32_reg.h" | #include "esphome/core/application.h" | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/core/helpers.h" | ||||||
|  | #include "esphome/core/preferences.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace atm90e32 { | namespace atm90e32 { | ||||||
| @@ -20,7 +23,6 @@ class ATM90E32Component : public PollingComponent, | |||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   float get_setup_priority() const override; |   float get_setup_priority() const override; | ||||||
|   void update() override; |   void update() override; | ||||||
|  |  | ||||||
|   void set_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].voltage_sensor_ = obj; } |   void set_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].voltage_sensor_ = obj; } | ||||||
|   void set_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].current_sensor_ = obj; } |   void set_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].current_sensor_ = obj; } | ||||||
|   void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; } |   void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; } | ||||||
| @@ -48,9 +50,11 @@ class ATM90E32Component : public PollingComponent, | |||||||
|   void set_line_freq(int freq) { line_freq_ = freq; } |   void set_line_freq(int freq) { line_freq_ = freq; } | ||||||
|   void set_current_phases(int phases) { current_phases_ = phases; } |   void set_current_phases(int phases) { current_phases_ = phases; } | ||||||
|   void set_pga_gain(uint16_t gain) { pga_gain_ = gain; } |   void set_pga_gain(uint16_t gain) { pga_gain_ = gain; } | ||||||
|  |   void run_offset_calibrations(); | ||||||
|  |   void clear_offset_calibrations(); | ||||||
|  |   void set_enable_offset_calibration(bool flag) { enable_offset_calibration_ = flag; } | ||||||
|   uint16_t calibrate_voltage_offset_phase(uint8_t /*phase*/); |   uint16_t calibrate_voltage_offset_phase(uint8_t /*phase*/); | ||||||
|   uint16_t calibrate_current_offset_phase(uint8_t /*phase*/); |   uint16_t calibrate_current_offset_phase(uint8_t /*phase*/); | ||||||
|  |  | ||||||
|   int32_t last_periodic_millis = millis(); |   int32_t last_periodic_millis = millis(); | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
| @@ -83,10 +87,11 @@ class ATM90E32Component : public PollingComponent, | |||||||
|   float get_chip_temperature_(); |   float get_chip_temperature_(); | ||||||
|   bool get_publish_interval_flag_() { return publish_interval_flag_; }; |   bool get_publish_interval_flag_() { return publish_interval_flag_; }; | ||||||
|   void set_publish_interval_flag_(bool flag) { publish_interval_flag_ = flag; }; |   void set_publish_interval_flag_(bool flag) { publish_interval_flag_ = flag; }; | ||||||
|  |   void restore_calibrations_(); | ||||||
|  |  | ||||||
|   struct ATM90E32Phase { |   struct ATM90E32Phase { | ||||||
|     uint16_t voltage_gain_{7305}; |     uint16_t voltage_gain_{0}; | ||||||
|     uint16_t ct_gain_{27961}; |     uint16_t ct_gain_{0}; | ||||||
|     uint16_t voltage_offset_{0}; |     uint16_t voltage_offset_{0}; | ||||||
|     uint16_t current_offset_{0}; |     uint16_t current_offset_{0}; | ||||||
|     float voltage_{0}; |     float voltage_{0}; | ||||||
| @@ -114,13 +119,21 @@ class ATM90E32Component : public PollingComponent, | |||||||
|     uint32_t cumulative_reverse_active_energy_{0}; |     uint32_t cumulative_reverse_active_energy_{0}; | ||||||
|   } phase_[3]; |   } phase_[3]; | ||||||
|  |  | ||||||
|  |   struct Calibration { | ||||||
|  |     uint16_t voltage_offset_{0}; | ||||||
|  |     uint16_t current_offset_{0}; | ||||||
|  |   } offset_phase_[3]; | ||||||
|  |  | ||||||
|  |   ESPPreferenceObject pref_; | ||||||
|  |  | ||||||
|   sensor::Sensor *freq_sensor_{nullptr}; |   sensor::Sensor *freq_sensor_{nullptr}; | ||||||
|   sensor::Sensor *chip_temperature_sensor_{nullptr}; |   sensor::Sensor *chip_temperature_sensor_{nullptr}; | ||||||
|   uint16_t pga_gain_{0x15}; |   uint16_t pga_gain_{0x15}; | ||||||
|   int line_freq_{60}; |   int line_freq_{60}; | ||||||
|   int current_phases_{3}; |   int current_phases_{3}; | ||||||
|   bool publish_interval_flag_{true}; |   bool publish_interval_flag_{false}; | ||||||
|   bool peak_current_signed_{false}; |   bool peak_current_signed_{false}; | ||||||
|  |   bool enable_offset_calibration_{false}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace atm90e32 | }  // namespace atm90e32 | ||||||
|   | |||||||
| @@ -1,5 +1,7 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include <cinttypes> | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace atm90e32 { | namespace atm90e32 { | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										43
									
								
								esphome/components/atm90e32/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								esphome/components/atm90e32/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | import esphome.codegen as cg | ||||||
|  | from esphome.components import button | ||||||
|  | import esphome.config_validation as cv | ||||||
|  | from esphome.const import CONF_ID, ENTITY_CATEGORY_CONFIG, ICON_CHIP, ICON_SCALE | ||||||
|  |  | ||||||
|  | from .. import atm90e32_ns | ||||||
|  | from ..sensor import ATM90E32Component | ||||||
|  |  | ||||||
|  | CONF_RUN_OFFSET_CALIBRATION = "run_offset_calibration" | ||||||
|  | CONF_CLEAR_OFFSET_CALIBRATION = "clear_offset_calibration" | ||||||
|  |  | ||||||
|  | ATM90E32CalibrationButton = atm90e32_ns.class_( | ||||||
|  |     "ATM90E32CalibrationButton", | ||||||
|  |     button.Button, | ||||||
|  | ) | ||||||
|  | ATM90E32ClearCalibrationButton = atm90e32_ns.class_( | ||||||
|  |     "ATM90E32ClearCalibrationButton", | ||||||
|  |     button.Button, | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA = { | ||||||
|  |     cv.GenerateID(CONF_ID): cv.use_id(ATM90E32Component), | ||||||
|  |     cv.Optional(CONF_RUN_OFFSET_CALIBRATION): button.button_schema( | ||||||
|  |         ATM90E32CalibrationButton, | ||||||
|  |         entity_category=ENTITY_CATEGORY_CONFIG, | ||||||
|  |         icon=ICON_SCALE, | ||||||
|  |     ), | ||||||
|  |     cv.Optional(CONF_CLEAR_OFFSET_CALIBRATION): button.button_schema( | ||||||
|  |         ATM90E32ClearCalibrationButton, | ||||||
|  |         entity_category=ENTITY_CATEGORY_CONFIG, | ||||||
|  |         icon=ICON_CHIP, | ||||||
|  |     ), | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | async def to_code(config): | ||||||
|  |     parent = await cg.get_variable(config[CONF_ID]) | ||||||
|  |     if run_offset := config.get(CONF_RUN_OFFSET_CALIBRATION): | ||||||
|  |         b = await button.new_button(run_offset) | ||||||
|  |         await cg.register_parented(b, parent) | ||||||
|  |     if clear_offset := config.get(CONF_CLEAR_OFFSET_CALIBRATION): | ||||||
|  |         b = await button.new_button(clear_offset) | ||||||
|  |         await cg.register_parented(b, parent) | ||||||
							
								
								
									
										20
									
								
								esphome/components/atm90e32/button/atm90e32_button.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								esphome/components/atm90e32/button/atm90e32_button.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | #include "atm90e32_button.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace atm90e32 { | ||||||
|  |  | ||||||
|  | static const char *const TAG = "atm90e32.button"; | ||||||
|  |  | ||||||
|  | void ATM90E32CalibrationButton::press_action() { | ||||||
|  |   ESP_LOGI(TAG, "Running offset calibrations, Note: CTs and ACVs must be 0 during this process..."); | ||||||
|  |   this->parent_->run_offset_calibrations(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void ATM90E32ClearCalibrationButton::press_action() { | ||||||
|  |   ESP_LOGI(TAG, "Offset calibrations cleared."); | ||||||
|  |   this->parent_->clear_offset_calibrations(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace atm90e32 | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										27
									
								
								esphome/components/atm90e32/button/atm90e32_button.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								esphome/components/atm90e32/button/atm90e32_button.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/components/atm90e32/atm90e32.h" | ||||||
|  | #include "esphome/components/button/button.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace atm90e32 { | ||||||
|  |  | ||||||
|  | class ATM90E32CalibrationButton : public button::Button, public Parented<ATM90E32Component> { | ||||||
|  |  public: | ||||||
|  |   ATM90E32CalibrationButton() = default; | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void press_action() override; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | class ATM90E32ClearCalibrationButton : public button::Button, public Parented<ATM90E32Component> { | ||||||
|  |  public: | ||||||
|  |   ATM90E32ClearCalibrationButton() = default; | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void press_action() override; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace atm90e32 | ||||||
|  | }  // namespace esphome | ||||||
| @@ -1,21 +1,21 @@ | |||||||
| import esphome.codegen as cg | import esphome.codegen as cg | ||||||
| import esphome.config_validation as cv |  | ||||||
| from esphome.components import sensor, spi | from esphome.components import sensor, spi | ||||||
|  | import esphome.config_validation as cv | ||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_ID, |     CONF_APPARENT_POWER, | ||||||
|     CONF_REACTIVE_POWER, |  | ||||||
|     CONF_VOLTAGE, |  | ||||||
|     CONF_CURRENT, |     CONF_CURRENT, | ||||||
|  |     CONF_FORWARD_ACTIVE_ENERGY, | ||||||
|  |     CONF_FREQUENCY, | ||||||
|  |     CONF_ID, | ||||||
|     CONF_PHASE_A, |     CONF_PHASE_A, | ||||||
|  |     CONF_PHASE_ANGLE, | ||||||
|     CONF_PHASE_B, |     CONF_PHASE_B, | ||||||
|     CONF_PHASE_C, |     CONF_PHASE_C, | ||||||
|     CONF_PHASE_ANGLE, |  | ||||||
|     CONF_POWER, |     CONF_POWER, | ||||||
|     CONF_POWER_FACTOR, |     CONF_POWER_FACTOR, | ||||||
|     CONF_APPARENT_POWER, |     CONF_REACTIVE_POWER, | ||||||
|     CONF_FREQUENCY, |  | ||||||
|     CONF_FORWARD_ACTIVE_ENERGY, |  | ||||||
|     CONF_REVERSE_ACTIVE_ENERGY, |     CONF_REVERSE_ACTIVE_ENERGY, | ||||||
|  |     CONF_VOLTAGE, | ||||||
|     DEVICE_CLASS_CURRENT, |     DEVICE_CLASS_CURRENT, | ||||||
|     DEVICE_CLASS_ENERGY, |     DEVICE_CLASS_ENERGY, | ||||||
|     DEVICE_CLASS_POWER, |     DEVICE_CLASS_POWER, | ||||||
| @@ -23,13 +23,13 @@ from esphome.const import ( | |||||||
|     DEVICE_CLASS_TEMPERATURE, |     DEVICE_CLASS_TEMPERATURE, | ||||||
|     DEVICE_CLASS_VOLTAGE, |     DEVICE_CLASS_VOLTAGE, | ||||||
|     ENTITY_CATEGORY_DIAGNOSTIC, |     ENTITY_CATEGORY_DIAGNOSTIC, | ||||||
|     ICON_LIGHTBULB, |  | ||||||
|     ICON_CURRENT_AC, |     ICON_CURRENT_AC, | ||||||
|  |     ICON_LIGHTBULB, | ||||||
|     STATE_CLASS_MEASUREMENT, |     STATE_CLASS_MEASUREMENT, | ||||||
|     STATE_CLASS_TOTAL_INCREASING, |     STATE_CLASS_TOTAL_INCREASING, | ||||||
|     UNIT_AMPERE, |     UNIT_AMPERE, | ||||||
|     UNIT_DEGREES, |  | ||||||
|     UNIT_CELSIUS, |     UNIT_CELSIUS, | ||||||
|  |     UNIT_DEGREES, | ||||||
|     UNIT_HERTZ, |     UNIT_HERTZ, | ||||||
|     UNIT_VOLT, |     UNIT_VOLT, | ||||||
|     UNIT_VOLT_AMPS_REACTIVE, |     UNIT_VOLT_AMPS_REACTIVE, | ||||||
| @@ -37,6 +37,8 @@ from esphome.const import ( | |||||||
|     UNIT_WATT_HOURS, |     UNIT_WATT_HOURS, | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | from . import atm90e32_ns | ||||||
|  |  | ||||||
| CONF_LINE_FREQUENCY = "line_frequency" | CONF_LINE_FREQUENCY = "line_frequency" | ||||||
| CONF_CHIP_TEMPERATURE = "chip_temperature" | CONF_CHIP_TEMPERATURE = "chip_temperature" | ||||||
| CONF_GAIN_PGA = "gain_pga" | CONF_GAIN_PGA = "gain_pga" | ||||||
| @@ -46,6 +48,7 @@ CONF_GAIN_CT = "gain_ct" | |||||||
| CONF_HARMONIC_POWER = "harmonic_power" | CONF_HARMONIC_POWER = "harmonic_power" | ||||||
| CONF_PEAK_CURRENT = "peak_current" | CONF_PEAK_CURRENT = "peak_current" | ||||||
| CONF_PEAK_CURRENT_SIGNED = "peak_current_signed" | CONF_PEAK_CURRENT_SIGNED = "peak_current_signed" | ||||||
|  | CONF_ENABLE_OFFSET_CALIBRATION = "enable_offset_calibration" | ||||||
| UNIT_DEG = "degrees" | UNIT_DEG = "degrees" | ||||||
| LINE_FREQS = { | LINE_FREQS = { | ||||||
|     "50HZ": 50, |     "50HZ": 50, | ||||||
| @@ -61,7 +64,6 @@ PGA_GAINS = { | |||||||
|     "4X": 0x2A, |     "4X": 0x2A, | ||||||
| } | } | ||||||
|  |  | ||||||
| atm90e32_ns = cg.esphome_ns.namespace("atm90e32") |  | ||||||
| ATM90E32Component = atm90e32_ns.class_( | ATM90E32Component = atm90e32_ns.class_( | ||||||
|     "ATM90E32Component", cg.PollingComponent, spi.SPIDevice |     "ATM90E32Component", cg.PollingComponent, spi.SPIDevice | ||||||
| ) | ) | ||||||
| @@ -164,6 +166,7 @@ CONFIG_SCHEMA = ( | |||||||
|             ), |             ), | ||||||
|             cv.Optional(CONF_GAIN_PGA, default="2X"): cv.enum(PGA_GAINS, upper=True), |             cv.Optional(CONF_GAIN_PGA, default="2X"): cv.enum(PGA_GAINS, upper=True), | ||||||
|             cv.Optional(CONF_PEAK_CURRENT_SIGNED, default=False): cv.boolean, |             cv.Optional(CONF_PEAK_CURRENT_SIGNED, default=False): cv.boolean, | ||||||
|  |             cv.Optional(CONF_ENABLE_OFFSET_CALIBRATION, default=False): cv.boolean, | ||||||
|         } |         } | ||||||
|     ) |     ) | ||||||
|     .extend(cv.polling_component_schema("60s")) |     .extend(cv.polling_component_schema("60s")) | ||||||
| @@ -227,3 +230,4 @@ async def to_code(config): | |||||||
|     cg.add(var.set_current_phases(config[CONF_CURRENT_PHASES])) |     cg.add(var.set_current_phases(config[CONF_CURRENT_PHASES])) | ||||||
|     cg.add(var.set_pga_gain(config[CONF_GAIN_PGA])) |     cg.add(var.set_pga_gain(config[CONF_GAIN_PGA])) | ||||||
|     cg.add(var.set_peak_current_signed(config[CONF_PEAK_CURRENT_SIGNED])) |     cg.add(var.set_peak_current_signed(config[CONF_PEAK_CURRENT_SIGNED])) | ||||||
|  |     cg.add(var.set_enable_offset_calibration(config[CONF_ENABLE_OFFSET_CALIBRATION])) | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 13 |     cs_pin: 13 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,11 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 8 |     cs_pin: 8 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,11 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 8 |     cs_pin: 8 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,11 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 13 |     cs_pin: 13 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,11 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 5 |     cs_pin: 5 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,42 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     cs_pin: 4 | ||||||
|  |     id: chip2 | ||||||
|  |     phase_a: | ||||||
|  |       voltage: | ||||||
|  |         name: EMON Line Voltage A | ||||||
|  |       current: | ||||||
|  |         name: EMON CT1 Current | ||||||
|  |       power: | ||||||
|  |         name: EMON Active Power CT1 | ||||||
|  |       reactive_power: | ||||||
|  |         name: EMON Reactive Power CT1 | ||||||
|  |       power_factor: | ||||||
|  |         name: EMON Power Factor CT1 | ||||||
|  |       gain_voltage: 7305 | ||||||
|  |       gain_ct: 27961 | ||||||
|  |     phase_c: | ||||||
|  |       voltage: | ||||||
|  |         name: EMON Line Voltage C | ||||||
|  |       current: | ||||||
|  |         name: EMON CT2 Current | ||||||
|  |       power: | ||||||
|  |         name: EMON Active Power CT2 | ||||||
|  |       reactive_power: | ||||||
|  |         name: EMON Reactive Power CT2 | ||||||
|  |       power_factor: | ||||||
|  |         name: EMON Power Factor CT2 | ||||||
|  |       gain_voltage: 7305 | ||||||
|  |       gain_ct: 27961 | ||||||
|  |     line_frequency: 60Hz | ||||||
|  |     current_phases: 2 | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ spi: | |||||||
| sensor: | sensor: | ||||||
|   - platform: atm90e32 |   - platform: atm90e32 | ||||||
|     cs_pin: 5 |     cs_pin: 5 | ||||||
|  |     id: chip1 | ||||||
|     phase_a: |     phase_a: | ||||||
|       voltage: |       voltage: | ||||||
|         name: EMON Line Voltage A |         name: EMON Line Voltage A | ||||||
| @@ -49,3 +50,11 @@ sensor: | |||||||
|     line_frequency: 60Hz |     line_frequency: 60Hz | ||||||
|     current_phases: 3 |     current_phases: 3 | ||||||
|     gain_pga: 2X |     gain_pga: 2X | ||||||
|  |     enable_offset_calibration: True | ||||||
|  | button: | ||||||
|  |   - platform: atm90e32 | ||||||
|  |     id: chip1 | ||||||
|  |     run_offset_calibration: | ||||||
|  |       name: "Chip1 - Run Offset Calibration" | ||||||
|  |     clear_offset_calibration: | ||||||
|  |       name: "Chip1 - Clear Offset Calibration" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user