try some dedupe

Signed-off-by: Matthew Thode <mthode@mthode.org>
This commit is contained in:
2026-07-11 21:15:52 -05:00
parent 14a7c2ef82
commit d81fb918d0
4 changed files with 45 additions and 136 deletions
+18 -51
View File
@@ -80,25 +80,12 @@ void ATM90E36Component::loop() {
this->get_local_phase_harmonic_active_power_(phase));
}
}
// THD values
if (this->thd_voltage_a_sensor_ != nullptr) {
this->thd_voltage_a_sensor_->publish_state(this->get_thd_voltage_a());
}
if (this->thd_current_a_sensor_ != nullptr) {
this->thd_current_a_sensor_->publish_state(this->get_thd_current_a());
}
if (this->thd_voltage_b_sensor_ != nullptr) {
this->thd_voltage_b_sensor_->publish_state(this->get_thd_voltage_b());
}
if (this->thd_current_b_sensor_ != nullptr) {
this->thd_current_b_sensor_->publish_state(this->get_thd_current_b());
}
if (this->thd_voltage_c_sensor_ != nullptr) {
this->thd_voltage_c_sensor_->publish_state(this->get_thd_voltage_c());
}
if (this->thd_current_c_sensor_ != nullptr) {
this->thd_current_c_sensor_->publish_state(this->get_thd_current_c());
if (this->phase_[phase].thd_voltage_sensor_ != nullptr)
this->phase_[phase].thd_voltage_sensor_->publish_state(this->get_local_phase_thd_voltage_(phase));
if (this->phase_[phase].thd_current_sensor_ != nullptr)
this->phase_[phase].thd_current_sensor_->publish_state(this->get_local_phase_thd_current_(phase));
}
if (this->freq_sensor_ != nullptr)
@@ -229,8 +216,8 @@ void ATM90E36Component::dump_config() {
LOG_SENSOR(" ", "Reactive Power A", this->phase_[PHASEA].reactive_power_sensor_);
LOG_SENSOR(" ", "Apparent Power A", this->phase_[PHASEA].apparent_power_sensor_);
LOG_SENSOR(" ", "PF A", this->phase_[PHASEA].power_factor_sensor_);
LOG_SENSOR(" ", "THD Voltage A", this->thd_voltage_a_sensor_);
LOG_SENSOR(" ", "THD Current A", this->thd_current_a_sensor_);
LOG_SENSOR(" ", "THD Voltage A", this->phase_[PHASEA].thd_voltage_sensor_);
LOG_SENSOR(" ", "THD Current A", this->phase_[PHASEA].thd_current_sensor_);
LOG_SENSOR(" ", "Active Forward Energy A", this->phase_[PHASEA].forward_active_energy_sensor_);
LOG_SENSOR(" ", "Active Reverse Energy A", this->phase_[PHASEA].reverse_active_energy_sensor_);
LOG_SENSOR(" ", "Harmonic Power A", this->phase_[PHASEA].harmonic_active_power_sensor_);
@@ -238,8 +225,8 @@ void ATM90E36Component::dump_config() {
LOG_SENSOR(" ", "Voltage B", this->phase_[PHASEB].voltage_sensor_);
LOG_SENSOR(" ", "Current B", this->phase_[PHASEB].current_sensor_);
LOG_SENSOR(" ", "Power B", this->phase_[PHASEB].power_sensor_);
LOG_SENSOR(" ", "THD Voltage B", this->thd_voltage_b_sensor_);
LOG_SENSOR(" ", "THD Current B", this->thd_current_b_sensor_);
LOG_SENSOR(" ", "THD Voltage B", this->phase_[PHASEB].thd_voltage_sensor_);
LOG_SENSOR(" ", "THD Current B", this->phase_[PHASEB].thd_current_sensor_);
LOG_SENSOR(" ", "Reactive Power B", this->phase_[PHASEB].reactive_power_sensor_);
LOG_SENSOR(" ", "Apparent Power B", this->phase_[PHASEB].apparent_power_sensor_);
LOG_SENSOR(" ", "PF B", this->phase_[PHASEB].power_factor_sensor_);
@@ -253,8 +240,8 @@ void ATM90E36Component::dump_config() {
LOG_SENSOR(" ", "Reactive Power C", this->phase_[PHASEC].reactive_power_sensor_);
LOG_SENSOR(" ", "Apparent Power C", this->phase_[PHASEC].apparent_power_sensor_);
LOG_SENSOR(" ", "PF C", this->phase_[PHASEC].power_factor_sensor_);
LOG_SENSOR(" ", "THD Voltage C", this->thd_voltage_c_sensor_);
LOG_SENSOR(" ", "THD Current C", this->thd_current_c_sensor_);
LOG_SENSOR(" ", "THD Voltage C", this->phase_[PHASEC].thd_voltage_sensor_);
LOG_SENSOR(" ", "THD Current C", this->phase_[PHASEC].thd_current_sensor_);
LOG_SENSOR(" ", "Active Forward Energy C", this->phase_[PHASEC].forward_active_energy_sensor_);
LOG_SENSOR(" ", "Active Reverse Energy C", this->phase_[PHASEC].reverse_active_energy_sensor_);
LOG_SENSOR(" ", "Harmonic Power C", this->phase_[PHASEC].harmonic_active_power_sensor_);
@@ -310,34 +297,14 @@ float ATM90E36Component::get_local_phase_apparent_power_(uint8_t phase) { return
float ATM90E36Component::get_local_phase_power_factor_(uint8_t phase) { return this->phase_[phase].power_factor_; }
float ATM90E36Component::get_thd_voltage_a() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNUA);
return (float)val / 100.0; // Adjust division scale based on your calibration constraints
float ATM90E36Component::get_local_phase_thd_voltage_(uint8_t phase) {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNU + phase);
return (float) val / 100.0; // Adjust division scale based on your calibration constraints
}
float ATM90E36Component::get_thd_current_a() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNIA);
return (float)val / 100.0;
}
float ATM90E36Component::get_thd_voltage_b() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNUB);
return (float)val / 100.0; // Adjust division scale based on your calibration constraints
}
float ATM90E36Component::get_thd_current_b() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNIB);
return (float)val / 100.0;
}
float ATM90E36Component::get_thd_voltage_c() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNUC);
return (float)val / 100.0; // Adjust division scale based on your calibration constraints
}
float ATM90E36Component::get_thd_current_c() {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNIC);
return (float)val / 100.0;
float ATM90E36Component::get_local_phase_thd_current_(uint8_t phase) {
uint16_t val = this->read16_(ATM90E36_REGISTER_THDNI + phase);
return (float) val / 100.0; // Adjust division scale based on your calibration constraints
}
float ATM90E36Component::get_local_phase_forward_active_energy_(uint8_t phase) {
+6 -30
View File
@@ -30,24 +30,8 @@ class ATM90E36Component : public PollingComponent,
void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; }
void set_reactive_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].reactive_power_sensor_ = obj; }
void set_apparent_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].apparent_power_sensor_ = obj; }
void set_thd_voltage_a_sensor(sensor::Sensor *thd_voltage_a_sensor) {
thd_voltage_a_sensor_ = thd_voltage_a_sensor;
}
void set_thd_current_a_sensor(sensor::Sensor *thd_current_a_sensor) {
thd_current_a_sensor_ = thd_current_a_sensor;
}
void set_thd_voltage_b_sensor(sensor::Sensor *thd_voltage_b_sensor) {
thd_voltage_b_sensor_ = thd_voltage_b_sensor;
}
void set_thd_current_b_sensor(sensor::Sensor *thd_current_b_sensor) {
thd_current_b_sensor_ = thd_current_b_sensor;
}
void set_thd_voltage_c_sensor(sensor::Sensor *thd_voltage_c_sensor) {
thd_voltage_c_sensor_ = thd_voltage_c_sensor;
}
void set_thd_current_c_sensor(sensor::Sensor *thd_current_c_sensor) {
thd_current_c_sensor_ = thd_current_c_sensor;
}
void set_thd_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].thd_voltage_sensor_ = obj; }
void set_thd_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].thd_current_sensor_ = obj; }
void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj) {
this->phase_[phase].forward_active_energy_sensor_ = obj;
}
@@ -122,6 +106,8 @@ class ATM90E36Component : public PollingComponent,
float get_local_phase_reverse_active_energy_(uint8_t phase);
float get_local_phase_angle_(uint8_t phase);
float get_local_phase_harmonic_active_power_(uint8_t phase);
float get_local_phase_thd_voltage_(uint8_t phase);
float get_local_phase_thd_current_(uint8_t phase);
float get_phase_voltage_(uint8_t phase);
float get_phase_voltage_avg_(uint8_t phase);
float get_phase_current_(uint8_t phase);
@@ -134,12 +120,6 @@ class ATM90E36Component : public PollingComponent,
float get_phase_reverse_active_energy_(uint8_t phase);
float get_phase_angle_(uint8_t phase);
float get_phase_harmonic_active_power_(uint8_t phase);
float get_thd_voltage_a();
float get_thd_current_a();
float get_thd_voltage_b();
float get_thd_current_b();
float get_thd_voltage_c();
float get_thd_current_c();
float get_frequency_();
float get_chip_temperature_();
bool get_publish_interval_flag_() { return publish_interval_flag_; };
@@ -184,6 +164,8 @@ class ATM90E36Component : public PollingComponent,
sensor::Sensor *reverse_active_energy_sensor_{nullptr};
sensor::Sensor *phase_angle_sensor_{nullptr};
sensor::Sensor *harmonic_active_power_sensor_{nullptr};
sensor::Sensor *thd_voltage_sensor_{nullptr};
sensor::Sensor *thd_current_sensor_{nullptr};
uint32_t cumulative_forward_active_energy_{0};
uint32_t cumulative_reverse_active_energy_{0};
} phase_[3];
@@ -238,12 +220,6 @@ class ATM90E36Component : public PollingComponent,
std::string cs_summary_;
sensor::Sensor *freq_sensor_{nullptr};
sensor::Sensor *thd_voltage_a_sensor_{nullptr};
sensor::Sensor *thd_current_a_sensor_{nullptr};
sensor::Sensor *thd_voltage_b_sensor_{nullptr};
sensor::Sensor *thd_current_b_sensor_{nullptr};
sensor::Sensor *thd_voltage_c_sensor_{nullptr};
sensor::Sensor *thd_current_c_sensor_{nullptr};
#ifdef USE_TEXT_SENSOR
text_sensor::TextSensor *phase_status_text_sensor_{nullptr};
text_sensor::TextSensor *freq_status_text_sensor_{nullptr};
+2
View File
@@ -222,9 +222,11 @@ static const uint16_t ATM90E36_REGISTER_IRMSBLSB = 0xEE; // Lower Word (B RMS
static const uint16_t ATM90E36_REGISTER_IRMSCLSB = 0xEF; // Lower Word (C RMS Current)
/* THD, FREQUENCY, ANGLE & TEMPTEMP REGISTERS*/
static const uint16_t ATM90E36_REGISTER_THDNU = 0xF1; // Phase A Voltage THD
static const uint16_t ATM90E36_REGISTER_THDNUA = 0xF1; // Phase A Voltage THD
static const uint16_t ATM90E36_REGISTER_THDNUB = 0xF2; // Phase B Voltage THD
static const uint16_t ATM90E36_REGISTER_THDNUC = 0xF3; // Phase C Voltage THD
static const uint16_t ATM90E36_REGISTER_THDNI = 0xF5; // Phase A Current THD
static const uint16_t ATM90E36_REGISTER_THDNIA = 0xF5; // Phase A Current THD
static const uint16_t ATM90E36_REGISTER_THDNIB = 0xF6; // Phase B Current THD
static const uint16_t ATM90E36_REGISTER_THDNIC = 0xF7; // Phase C Current THD
+18 -54
View File
@@ -60,12 +60,8 @@ CONF_HARMONIC_POWER = "harmonic_power"
CONF_ENABLE_OFFSET_CALIBRATION = "enable_offset_calibration"
CONF_ENABLE_GAIN_CALIBRATION = "enable_gain_calibration"
CONF_PHASE_STATUS = "phase_status"
CONF_THD_VOLTAGE_A = "thd_voltage_a"
CONF_THD_CURRENT_A = "thd_current_a"
CONF_THD_VOLTAGE_B = "thd_voltage_b"
CONF_THD_CURRENT_B = "thd_current_b"
CONF_THD_VOLTAGE_C = "thd_voltage_c"
CONF_THD_CURRENT_C = "thd_current_c"
CONF_THD_VOLTAGE = "thd_voltage"
CONF_THD_CURRENT = "thd_current"
CONF_FREQUENCY_STATUS = "frequency_status"
UNIT_DEG = "degrees"
LINE_FREQS = {
@@ -161,6 +157,16 @@ ATM90E36_PHASE_SCHEMA = cv.Schema(
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_THD_VOLTAGE): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_CURRENT): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t,
cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t,
cv.Optional(CONF_OFFSET_VOLTAGE, default=0): cv.int_,
@@ -199,36 +205,6 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_GAIN_DPGA, default="1X"): cv.enum(DPGA_GAINS, upper=True),
cv.Optional(CONF_ENABLE_OFFSET_CALIBRATION, default=False): cv.boolean,
cv.Optional(CONF_ENABLE_GAIN_CALIBRATION, default=False): cv.boolean,
cv.Optional(CONF_THD_VOLTAGE_A): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_CURRENT_A): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_VOLTAGE_B): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_CURRENT_B): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_VOLTAGE_C): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
cv.Optional(CONF_THD_CURRENT_C): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon="mdi:sine-wave",
accuracy_decimals=2,
),
}
)
.extend(cv.polling_component_schema("60s"))
@@ -277,6 +253,12 @@ async def to_code(config):
if harmonic_active_power_config := conf.get(CONF_HARMONIC_POWER):
sens = await sensor.new_sensor(harmonic_active_power_config)
cg.add(var.set_harmonic_active_power_sensor(i, sens))
if thd_voltage_config := conf.get(CONF_THD_VOLTAGE):
sens = await sensor.new_sensor(thd_voltage_config)
cg.add(var.set_thd_voltage_sensor(i, sens))
if thd_current_config := conf.get(CONF_THD_CURRENT):
sens = await sensor.new_sensor(thd_current_config)
cg.add(var.set_thd_current_sensor(i, sens))
if frequency_config := config.get(CONF_FREQUENCY):
sens = await sensor.new_sensor(frequency_config)
cg.add(var.set_freq_sensor(sens))
@@ -288,21 +270,3 @@ async def to_code(config):
cg.add(var.set_pga_current(config[CONF_GAIN_CURRENT]))
cg.add(var.set_pga_voltage(config[CONF_GAIN_VOLTAGE]))
cg.add(var.set_dpga_gain(config[CONF_GAIN_DPGA]))
if CONF_THD_VOLTAGE_A in config:
sens = await sensor.new_sensor(config[CONF_THD_VOLTAGE_A])
cg.add(var.set_thd_voltage_a_sensor(sens))
if CONF_THD_CURRENT_A in config:
sens = await sensor.new_sensor(config[CONF_THD_CURRENT_A])
cg.add(var.set_thd_current_a_sensor(sens))
if CONF_THD_VOLTAGE_B in config:
sens = await sensor.new_sensor(config[CONF_THD_VOLTAGE_B])
cg.add(var.set_thd_voltage_b_sensor(sens))
if CONF_THD_CURRENT_B in config:
sens = await sensor.new_sensor(config[CONF_THD_CURRENT_B])
cg.add(var.set_thd_current_b_sensor(sens))
if CONF_THD_VOLTAGE_C in config:
sens = await sensor.new_sensor(config[CONF_THD_VOLTAGE_C])
cg.add(var.set_thd_voltage_c_sensor(sens))
if CONF_THD_CURRENT_C in config:
sens = await sensor.new_sensor(config[CONF_THD_CURRENT_C])
cg.add(var.set_thd_current_c_sensor(sens))