14static const LogString *offset_calibration_name(
bool power_offsets) {
15 return power_offsets ? LOG_STR(
"Power offset") : LOG_STR(
"Offset");
18static uint32_t pref_hash(
const char *prefix,
const char *name_space) {
24static int migrate_legacy_pref_if_needed(ESPPreferenceObject ¤t_pref, ESPPreferenceObject &legacy_pref,
27 if (current_pref.load(¤t)) {
30 if (!legacy_pref.load(
scratch)) {
33 return current_pref.save(
scratch) ? 1 : -1;
39 for (uint8_t phase = 0; phase < 3; phase++) {
40 if (this->
phase_[phase].voltage_sensor_ !=
nullptr)
43 if (this->
phase_[phase].current_sensor_ !=
nullptr)
46 if (this->
phase_[phase].power_sensor_ !=
nullptr)
49 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr)
52 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr)
55 if (this->
phase_[phase].apparent_power_sensor_ !=
nullptr)
58 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr)
61 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr)
64 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr)
67 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr)
70 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr)
86 PUBLISH_STATE(this->
phase_[phase].forward_active_energy_sensor_,
89 PUBLISH_STATE(this->
phase_[phase].reverse_active_energy_sensor_,
94 PUBLISH_STATE(this->
phase_[phase].harmonic_active_power_sensor_,
110#ifdef USE_TEXT_SENSOR
131 const bool has_distinct_legacy_namespace = strcmp(cs, legacy_cs) != 0;
133 uint16_t mmode0 = 0x87;
144 this->
write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A,
false);
146 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
148 ESP_LOGW(TAG,
"Could not initialize ATM90E32 IC, check SPI settings");
153 this->
write16_(ATM90E32_REGISTER_METEREN, 0x0001);
154 this->
write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F);
155 this->
write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861);
156 this->
write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468);
157 this->
write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654);
158 this->
write16_(ATM90E32_REGISTER_MMODE0, mmode0);
160 this->
write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C);
161 this->
write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C);
162 this->
write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);
163 this->
write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);
164 this->
write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);
168 uint32_t o_hash = pref_hash(
"_offset_calibration_", cs);
170 bool migrated_offset =
false;
171 if (has_distinct_legacy_namespace) {
172 uint32_t legacy_o_hash = pref_hash(
"_offset_calibration_", legacy_cs);
175 int migration_status = migrate_legacy_pref_if_needed(this->
offset_pref_, legacy_offset_pref, &offset_data);
176 migrated_offset = migration_status > 0;
177 if (migration_status > 0) {
178 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated offset calibrations from legacy storage.", cs);
179 }
else if (migration_status < 0) {
180 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate offset calibrations from legacy storage.", cs);
185 uint32_t po_hash = pref_hash(
"_power_offset_calibration_", cs);
187 bool migrated_power_offset =
false;
188 if (has_distinct_legacy_namespace) {
189 uint32_t legacy_po_hash = pref_hash(
"_power_offset_calibration_", legacy_cs);
192 int migration_status =
193 migrate_legacy_pref_if_needed(this->
power_offset_pref_, legacy_power_offset_pref, &power_offset_data);
194 migrated_power_offset = migration_status > 0;
195 if (migration_status > 0) {
196 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated power offset calibrations from legacy storage.", cs);
197 }
else if (migration_status < 0) {
198 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate power offset calibrations from legacy storage.", cs);
202 if (migrated_offset || migrated_power_offset) {
209 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
211 for (uint8_t phase = 0; phase < 3; ++phase) {
213 static_cast<uint16_t
>(this->
offset_phase_[phase].first_offset));
215 static_cast<uint16_t
>(this->
offset_phase_[phase].second_offset));
225 uint32_t g_hash = pref_hash(
"_gain_calibration_", cs);
227 bool migrated_gain =
false;
228 if (has_distinct_legacy_namespace) {
229 uint32_t legacy_g_hash = pref_hash(
"_gain_calibration_", legacy_cs);
232 int migration_status =
234 migrated_gain = migration_status > 0;
235 if (migration_status > 0) {
236 ESP_LOGI(TAG,
"[CALIBRATION][%s] Migrated gain calibrations from legacy storage.", cs);
237 }
else if (migration_status < 0) {
238 ESP_LOGW(TAG,
"[CALIBRATION][%s] Failed to migrate gain calibrations from legacy storage.", cs);
248 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
249 for (uint8_t phase = 0; phase < 3; ++phase) {
256 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
260 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
266 bool offset_mismatch =
false;
267 bool power_mismatch =
false;
268 bool gain_mismatch =
false;
270 for (uint8_t phase = 0; phase < 3; ++phase) {
276 if (offset_mismatch) {
277 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
279 "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
280 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
282 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
283 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
284 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
286 for (uint8_t phase = 0; phase < 3; ++phase) {
287 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
292 "[CALIBRATION][%s] ===============================================================================", cs);
294 if (power_mismatch) {
295 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
297 "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
298 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
300 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
301 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
302 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
304 for (uint8_t phase = 0; phase < 3; ++phase) {
305 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d | %6d | %6d |", cs,
'A' + phase,
310 "[CALIBRATION][%s] ===============================================================================", cs);
313 ESP_LOGW(TAG,
"[CALIBRATION][%s] ", cs);
315 "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
316 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
318 ESP_LOGW(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
319 ESP_LOGW(TAG,
"[CALIBRATION][%s] | | config | flash | config | flash |", cs);
320 ESP_LOGW(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------------------",
322 for (uint8_t phase = 0; phase < 3; ++phase) {
323 ESP_LOGW(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u | %6u | %6u |", cs,
'A' + phase,
328 "[CALIBRATION][%s] ===============================================================================", cs);
331 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
334 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
335 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
336 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
337 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
338 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
339 for (uint8_t phase = 0; phase < 3; phase++) {
340 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
343 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\\n", cs);
347 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
348 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
349 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
350 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
351 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
352 for (uint8_t phase = 0; phase < 3; phase++) {
353 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
356 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
359 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
361 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
362 ESP_LOGI(TAG,
"[CALIBRATION][%s] ============ Restoring saved gain calibrations to registers ============", cs);
363 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
364 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
365 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
366 for (uint8_t phase = 0; phase < 3; phase++) {
367 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
370 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\\n", cs);
371 ESP_LOGI(TAG,
"[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
377 ESP_LOGCONFIG(
"",
"ATM90E32:");
378 LOG_PIN(
" CS Pin: ", this->
cs_);
379 LOG_UPDATE_INTERVAL(
this);
380 LOG_SENSOR(
" ",
"Voltage A", this->
phase_[
PHASEA].voltage_sensor_);
381 LOG_SENSOR(
" ",
"Current A", this->
phase_[
PHASEA].current_sensor_);
382 LOG_SENSOR(
" ",
"Power A", this->
phase_[
PHASEA].power_sensor_);
383 LOG_SENSOR(
" ",
"Reactive Power A", this->
phase_[
PHASEA].reactive_power_sensor_);
384 LOG_SENSOR(
" ",
"Apparent Power A", this->
phase_[
PHASEA].apparent_power_sensor_);
385 LOG_SENSOR(
" ",
"PF A", this->
phase_[
PHASEA].power_factor_sensor_);
386 LOG_SENSOR(
" ",
"Active Forward Energy A", this->
phase_[
PHASEA].forward_active_energy_sensor_);
387 LOG_SENSOR(
" ",
"Active Reverse Energy A", this->
phase_[
PHASEA].reverse_active_energy_sensor_);
388 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEA].harmonic_active_power_sensor_);
389 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEA].phase_angle_sensor_);
390 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEA].peak_current_sensor_);
391 LOG_SENSOR(
" ",
"Voltage B", this->
phase_[
PHASEB].voltage_sensor_);
392 LOG_SENSOR(
" ",
"Current B", this->
phase_[
PHASEB].current_sensor_);
393 LOG_SENSOR(
" ",
"Power B", this->
phase_[
PHASEB].power_sensor_);
394 LOG_SENSOR(
" ",
"Reactive Power B", this->
phase_[
PHASEB].reactive_power_sensor_);
395 LOG_SENSOR(
" ",
"Apparent Power B", this->
phase_[
PHASEB].apparent_power_sensor_);
396 LOG_SENSOR(
" ",
"PF B", this->
phase_[
PHASEB].power_factor_sensor_);
397 LOG_SENSOR(
" ",
"Active Forward Energy B", this->
phase_[
PHASEB].forward_active_energy_sensor_);
398 LOG_SENSOR(
" ",
"Active Reverse Energy B", this->
phase_[
PHASEB].reverse_active_energy_sensor_);
399 LOG_SENSOR(
" ",
"Harmonic Power B", this->
phase_[
PHASEB].harmonic_active_power_sensor_);
400 LOG_SENSOR(
" ",
"Phase Angle B", this->
phase_[
PHASEB].phase_angle_sensor_);
401 LOG_SENSOR(
" ",
"Peak Current B", this->
phase_[
PHASEB].peak_current_sensor_);
402 LOG_SENSOR(
" ",
"Voltage C", this->
phase_[
PHASEC].voltage_sensor_);
403 LOG_SENSOR(
" ",
"Current C", this->
phase_[
PHASEC].current_sensor_);
404 LOG_SENSOR(
" ",
"Power C", this->
phase_[
PHASEC].power_sensor_);
405 LOG_SENSOR(
" ",
"Reactive Power C", this->
phase_[
PHASEC].reactive_power_sensor_);
406 LOG_SENSOR(
" ",
"Apparent Power C", this->
phase_[
PHASEC].apparent_power_sensor_);
407 LOG_SENSOR(
" ",
"PF C", this->
phase_[
PHASEC].power_factor_sensor_);
408 LOG_SENSOR(
" ",
"Active Forward Energy C", this->
phase_[
PHASEC].forward_active_energy_sensor_);
409 LOG_SENSOR(
" ",
"Active Reverse Energy C", this->
phase_[
PHASEC].reverse_active_energy_sensor_);
410 LOG_SENSOR(
" ",
"Harmonic Power C", this->
phase_[
PHASEC].harmonic_active_power_sensor_);
411 LOG_SENSOR(
" ",
"Phase Angle C", this->
phase_[
PHASEC].phase_angle_sensor_);
412 LOG_SENSOR(
" ",
"Peak Current C", this->
phase_[
PHASEC].peak_current_sensor_);
413 LOG_SENSOR(
" ",
"Frequency", this->freq_sensor_);
414 LOG_SENSOR(
" ",
"Chip Temp", this->chip_temperature_sensor_);
429 uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
430 uint8_t addrl = (a_register & 0xFF);
431 uint8_t data[4] = {addrh, addrl, 0x00, 0x00};
434 ESP_LOGVV(TAG,
"read16_ 0x%04" PRIX16
" output 0x%04" PRIX16, a_register, output);
442 const uint16_t val_h = this->
read16_(addr_h);
443 const uint16_t val_l = this->
read16_(addr_l);
444 const int32_t
val = (val_h << 16) | val_l;
447 "read32_ addr_h 0x%04" PRIX16
" val_h 0x%04" PRIX16
" addr_l 0x%04" PRIX16
" val_l 0x%04" PRIX16
449 addr_h, val_h, addr_l, val_l,
val);
455 ESP_LOGVV(TAG,
"write16_ 0x%04" PRIX16
" val 0x%04" PRIX16, a_register,
val);
456 uint8_t addrh = ((a_register >> 8) & 0x03);
457 uint8_t addrl = (a_register & 0xFF);
458 uint8_t data[4] = {addrh, addrl, uint8_t((
val >> 8) & 0xFF), uint8_t(
val & 0xFF)};
498 const uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
500 return (
float) voltage / 100;
504 const uint8_t reads = 10;
506 uint16_t voltage = 0;
507 for (uint8_t i = 0; i < reads; i++) {
508 voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
510 accumulation += voltage;
512 voltage = accumulation / reads;
518 const uint8_t reads = 10;
520 uint16_t current = 0;
521 for (uint8_t i = 0; i < reads; i++) {
522 current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
524 accumulation += current;
526 current = accumulation / reads;
532 const uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
534 return (
float) current / 1000;
538 const int val = this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
539 return val * 0.00032f;
543 const int val = this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase);
544 return val * 0.00032f;
548 const int val = this->
read32_(ATM90E32_REGISTER_SMEAN + phase, ATM90E32_REGISTER_SMEANLSB + phase);
549 return val * 0.00032f;
553 uint16_t powerfactor = this->
read16_(ATM90E32_REGISTER_PFMEAN + phase);
555 return (
float) ((int16_t) powerfactor) / 1000;
559 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGY + phase);
560 if ((UINT32_MAX - this->
phase_[phase].cumulative_forward_active_energy_) >
val) {
566 return ((
float) this->
phase_[phase].cumulative_forward_active_energy_ * (10.0f / 3200.0f));
570 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGY + phase);
571 if (UINT32_MAX - this->
phase_[phase].cumulative_reverse_active_energy_ >
val) {
577 return ((
float) this->
phase_[phase].cumulative_reverse_active_energy_ * (10.0f / 3200.0f));
581 int val = this->
read32_(ATM90E32_REGISTER_PMEANH + phase, ATM90E32_REGISTER_PMEANHLSB + phase);
582 return val * 0.00032f;
586 float val = this->
read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0f;
587 return (
val > 180.0f) ?
val - 360.0f :
val;
591 int16_t
val = (float) this->
read16_(ATM90E32_REGISTER_IPEAK + phase);
595 return (
val * this->
phase_[phase].ct_gain_ / 8192000.0);
599 const uint16_t freq = this->
read16_(ATM90E32_REGISTER_FREQ);
600 return (
float) freq / 100;
604 const uint16_t ctemp = this->
read16_(ATM90E32_REGISTER_TEMP);
605 return (
float) ctemp;
609 const LogString *hint,
const LogString *hint2) {
610 ESP_LOGI(TAG,
"%s", LOG_STR_ARG(entity->
get_log_name()));
611 if (hint !=
nullptr) {
612 ESP_LOGI(TAG,
"%s", LOG_STR_ARG(hint));
614 if (hint2 !=
nullptr) {
615 ESP_LOGI(TAG,
"%s", LOG_STR_ARG(hint2));
623 ESP_LOGW(TAG,
"[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
628 float ref_voltages[3] = {
636 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
637 ESP_LOGI(TAG,
"[CALIBRATION][%s] ========================= Gain Calibration =========================", cs);
638 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
641 "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref | V_gain (old→new) | I_gain (old→new) |",
643 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
647 for (uint8_t phase = 0; phase < 3; phase++) {
651 float ref_voltage = ref_voltages[phase];
652 float ref_current = ref_currents[phase];
657 bool did_voltage =
false;
658 bool did_current =
false;
661 if (ref_voltage <= 0.0f) {
662 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
664 }
else if (measured_voltage == 0.0f) {
665 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
668 uint32_t new_voltage_gain =
static_cast<uint32_t>((ref_voltage / measured_voltage) * current_voltage_gain);
669 if (new_voltage_gain == 0) {
670 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
673 if (new_voltage_gain >= 65535) {
675 "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
678 new_voltage_gain = 65535;
686 if (ref_current == 0.0f) {
687 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
689 }
else if (measured_current == 0.0f) {
690 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
693 uint32_t new_current_gain =
static_cast<uint32_t>((ref_current / measured_current) * current_current_gain);
694 if (new_current_gain == 0) {
695 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
698 if (new_current_gain >= 65535) {
699 ESP_LOGW(TAG,
"[CALIBRATION][%s] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
701 new_current_gain = 65535;
709 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %9.2f | %9.4f | %5.2f | %6.4f | %5u → %-5u | %5u → %-5u |", cs,
710 'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
711 did_voltage ? this->
gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
712 did_current ? this->
gain_phase_[phase].current_gain : current_current_gain);
715 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
721template<
typename T,
typename F>
724 bool &has_stored,
bool &restored,
bool (&mismatches)[3], F &&rollback) {
725 static_assert(
sizeof(values) == CALIBRATION_PAYLOAD_SIZE,
"Calibration payload size mismatch");
727 const bool previous_has_stored = has_stored;
728 const bool previous_restored = restored;
730 uint8_t previous_stored[CALIBRATION_PAYLOAD_SIZE]{};
731 bool attempt = writes_verified;
732 if (writes_verified && previous_has_stored && !preference.
load(&previous_stored)) {
735 "[CALIBRATION][%s] Failed to load stored %s calibration "
736 "values; refusing to overwrite them. "
737 "Clear stored %s calibrations and retry.",
738 cs, LOG_STR_ARG(noun), LOG_STR_ARG(noun));
741 bool persisted =
false;
743 const bool saved = preference.
save(&values);
745 persisted = saved && synced;
751 for (
bool &mismatch : mismatches)
753 ESP_LOGI(TAG,
"[CALIBRATION][%s] %s calibration saved to memory. %s calibration completed and verified.", cs,
754 LOG_STR_ARG(name), LOG_STR_ARG(name));
759 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to save %s calibration to memory!", cs, LOG_STR_ARG(noun));
762 const bool rollback_verified = rollback();
765 const bool rollback_saved = preference.
save(&previous_stored);
767 if (!rollback_saved || !rollback_synced) {
768 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to persist restored %s calibration values!", cs, LOG_STR_ARG(noun));
772 restored = previous_restored && rollback_verified;
773 has_stored = previous_has_stored;
774 if (!rollback_verified) {
775 for (
bool &mismatch : mismatches)
777 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration failed; rollback readback verification failed.", cs,
781 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration failed; previous values restored.", cs, LOG_STR_ARG(name));
788 for (uint8_t phase = 0; phase < 3; phase++)
798 offset_calibration_name(power_offsets), offset_calibration_name(power_offsets),
804 [
this, &previous,
type]() {
805 for (uint8_t phase = 0; phase < 3; phase++)
815 "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
820 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
821 ESP_LOGI(TAG,
"[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
822 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
823 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
824 ESP_LOGI(TAG,
"[CALIBRATION][%s] ------------------------------------------------------------------", cs);
828 for (uint8_t phase = 0; phase < 3; phase++) {
835 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
839 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==================================================================\n", cs);
849 "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
854 ESP_LOGI(TAG,
"[CALIBRATION][%s] ", cs);
855 ESP_LOGI(TAG,
"[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
856 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
857 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
858 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
861 this->power_offset_phase_[2]};
863 for (uint8_t phase = 0; phase < 3; ++phase) {
870 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
873 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
879 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
881 for (
int phase = 0; phase < 3; phase++) {
887 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
891 static constexpr uint16_t LEGACY_OITH = 0xC000;
893 uint16_t min{UINT16_MAX};
897 auto read_gain_range = [
this](
const bool (&configured)[3],
const uint16_t (®isters)[3]) {
899 for (uint8_t phase = 0; phase < 3; phase++) {
900 if (configured[phase]) {
901 const uint16_t
gain = this->
read16_(registers[phase]);
913#ifdef USE_TEXT_SENSOR
916 ESP_LOGW(TAG,
"Phase voltage gains differ; using per-phase RMS voltage alarms");
921 ESP_LOGW(TAG,
"Phase current gains differ; using per-phase RMS over-current alarms");
927 const uint16_t oith = current_configured
930 ESP_LOGD(TAG,
"Thresholds: SAGTH=0x%04" PRIX16
", OVTH=0x%04" PRIX16
", OITH=0x%04" PRIX16, sagth, ovth, oith);
931 this->
write16_(ATM90E32_REGISTER_SAGTH, sagth);
932 this->
write16_(ATM90E32_REGISTER_OVTH, ovth);
933 this->
write16_(ATM90E32_REGISTER_OITH, oith);
951 const uint16_t *second_registers =
953 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
954 this->
write16_(first_registers[phase],
static_cast<uint16_t
>(first_offset));
955 this->
write16_(second_registers[phase],
static_cast<uint16_t
>(second_offset));
956 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
961 for (uint8_t i = 0; i < 3; ++i) {
968 bool all_zero =
true;
969 bool same_as_config =
true;
971 for (uint8_t phase = 0; phase < 3; ++phase) {
974 if (saved.voltage_gain != 0 || saved.current_gain != 0)
976 if (saved.voltage_gain != config.voltage_gain || saved.current_gain != config.current_gain)
977 same_as_config =
false;
985 for (uint8_t phase = 0; phase < 3; ++phase)
987 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
992 if (initial_values_verified) {
993 for (uint8_t phase = 0; phase < 3; ++phase) {
1005 for (uint8_t phase = 0; phase < 3; ++phase) {
1012 if (fallback_verified) {
1013 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration restore failed verification; config values verified.", cs,
1014 LOG_STR_LITERAL(
"gain"));
1016 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration restore and config fallback both failed verification.", cs,
1017 LOG_STR_LITERAL(
"gain"));
1024 const LogString *name = power_offsets ? LOG_STR(
"power offset") : LOG_STR(
"offset");
1036 for (uint8_t i = 0; i < 3; ++i)
1037 (*config_offsets)[i] = (*offsets)[i];
1039 const bool have_data = preference->
load(offsets);
1040 bool all_zero =
true;
1042 for (
const auto &phase : *offsets) {
1043 if (phase.first_offset != 0 || phase.second_offset != 0) {
1050 *has_stored = have_data && !all_zero;
1053 for (uint8_t phase = 0; phase < 3; phase++)
1054 (*offsets)[phase] = (*config_offsets)[phase];
1055 ESP_LOGW(TAG,
"[CALIBRATION][%s] No stored %s calibrations found. Using default values.", cs, LOG_STR_ARG(name));
1058 for (uint8_t phase = 0; phase < 3; phase++) {
1062 if (initial_values_verified) {
1063 for (uint8_t phase = 0; phase < 3; phase++) {
1066 ((has_first[phase] && (*offsets)[phase].first_offset != (*config_offsets)[phase].first_offset) ||
1067 (has_second[phase] && (*offsets)[phase].second_offset != (*config_offsets)[phase].second_offset));
1069 *restored = *has_stored;
1070 ESP_LOGI(TAG,
"[CALIBRATION][%s] %s calibration values verified.", cs, LOG_STR_ARG(name));
1074 for (uint8_t phase = 0; phase < 3; phase++)
1075 mismatches[phase] =
false;
1076 for (uint8_t phase = 0; phase < 3; phase++) {
1077 (*offsets)[phase] = (*config_offsets)[phase];
1082 if (fallback_verified) {
1083 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration restore failed verification; config values verified.", cs,
1086 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s calibration restore and config fallback both failed verification.", cs,
1092 bool (&mismatches)[3]) {
1093 const uint8_t cleared[CALIBRATION_PAYLOAD_SIZE]{};
1094 const bool saved = preference.
save(&cleared);
1096 if (saved && synced)
1099 for (
bool &mismatch : mismatches)
1101 return saved && synced;
1107 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
1108 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1109 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1110 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1111 for (
int phase = 0; phase < 3; phase++) {
1112 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase,
1115 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
1119 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
1120 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1121 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
1122 ESP_LOGI(TAG,
"[CALIBRATION][%s] ----------------------------------------------------------", cs);
1124 for (
int phase = 0; phase < 3; phase++) {
1133 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6u | %6u |", cs,
'A' + phase, voltage_gain, current_gain);
1135 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==========================================================\n", cs);
1139 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
1148 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
1149 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1150 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1151 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1152 for (uint8_t phase = 0; phase < 3; phase++) {
1153 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1156 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1160 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
1161 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1162 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
1163 ESP_LOGI(TAG,
"[CALIBRATION][%s] --------------------------------------------------------------", cs);
1165 for (uint8_t phase = 0; phase < 3; phase++) {
1166 int16_t voltage_offset =
1168 int16_t current_offset =
1172 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, voltage_offset,
1175 ESP_LOGI(TAG,
"[CALIBRATION][%s] ==============================================================\n", cs);
1179 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to clear offset calibrations!", cs);
1181 ESP_LOGI(TAG,
"[CALIBRATION][%s] Offsets cleared.", cs);
1188 ESP_LOGI(TAG,
"[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
1189 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1190 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1191 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1192 for (uint8_t phase = 0; phase < 3; phase++) {
1193 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase,
1196 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1200 ESP_LOGI(TAG,
"[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
1201 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1202 ESP_LOGI(TAG,
"[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
1203 ESP_LOGI(TAG,
"[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
1205 for (uint8_t phase = 0; phase < 3; phase++) {
1206 int16_t active_offset =
1208 int16_t reactive_offset =
1212 ESP_LOGI(TAG,
"[CALIBRATION][%s] | %c | %6d | %6d |", cs,
'A' + phase, active_offset,
1215 ESP_LOGI(TAG,
"[CALIBRATION][%s] =====================================================================\n", cs);
1220 ESP_LOGE(TAG,
"[CALIBRATION][%s] Failed to clear power offsets!", cs);
1222 ESP_LOGI(TAG,
"[CALIBRATION][%s] Power offsets cleared.", cs);
1227 const uint8_t num_reads = 5;
1228 uint64_t total_value = 0;
1230 for (uint8_t i = 0; i < num_reads; ++i) {
1231 uint32_t reading = voltage ? this->
read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase)
1232 : this->
read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
1233 total_value += reading;
1236 const uint32_t average_value = total_value / num_reads;
1237 const uint32_t shifted = average_value >> 7;
1238 const uint32_t offset = ~shifted + 1;
1239 return static_cast<int16_t
>(offset);
1243 const uint8_t num_reads = 5;
1244 int64_t total_value = 0;
1246 for (uint8_t i = 0; i < num_reads; ++i) {
1247 int32_t reading = reactive ? this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
1248 : this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
1249 total_value += reading;
1252 int32_t average_value = total_value / num_reads;
1253 int32_t power_offset = -average_value;
1254 return static_cast<int16_t
>(power_offset);
1259 bool success =
true;
1260 for (uint8_t phase = 0; phase < 3; phase++) {
1264 if (read_voltage != this->
gain_phase_[phase].voltage_gain ||
1265 read_current != this->
gain_phase_[phase].current_gain) {
1266 ESP_LOGE(TAG,
"[CALIBRATION][%s] Mismatch detected for Phase %s!", cs,
phase_labels[phase]);
1276 const LogString *name = offset_calibration_name(power_offsets);
1277 const LogString *first_name = power_offsets ? LOG_STR(
"active") : LOG_STR(
"voltage");
1278 const LogString *second_name = power_offsets ? LOG_STR(
"reactive") : LOG_STR(
"current");
1281 const uint16_t *second_registers =
1283 bool success =
true;
1284 for (uint8_t phase = 0; phase < 3; phase++) {
1285 const uint16_t first = this->
read16_(first_registers[phase]);
1286 const uint16_t
second = this->
read16_(second_registers[phase]);
1289 ESP_LOGE(TAG,
"[CALIBRATION][%s] %s readback failed for Phase %s: %s %d/%d, %s %d/%d.", cs, LOG_STR_ARG(name),
1290 phase_labels[phase], LOG_STR_ARG(first_name),
static_cast<int16_t
>(first), offsets[phase].first_offset,
1291 LOG_STR_ARG(second_name),
static_cast<int16_t
>(
second), offsets[phase].second_offset);
1298#ifdef USE_TEXT_SENSOR
1300 static constexpr uint16_t OVER_CURRENT_FLAGS[3] = {ATM90E32_STATUS_S0_OIPHASEAST, ATM90E32_STATUS_S0_OIPHASEBST,
1301 ATM90E32_STATUS_S0_OIPHASECST};
1302 static constexpr float LEGACY_OVER_CURRENT_A = 65.53f;
1303 uint16_t state0 = this->
read16_(ATM90E32_REGISTER_EMMSTATE0);
1304 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1308 for (
int phase = 0; phase < 3; phase++) {
1310 if (sensor ==
nullptr)
1313 char status[
sizeof(
"Over Current; Over Voltage; Voltage Sag; Phase Loss; ")];
1325 current > current_limit)
1337 ESP_LOGW(TAG,
"%s: %s", LOG_STR_ARG(sensor->get_log_name()),
status);
1340 sensor->publish_state(
"Okay");
1346 uint16_t state1 = this->
read16_(ATM90E32_REGISTER_EMMSTATE1);
1348 std::string freq_status;
1350 if (state1 & ATM90E32_STATUS_S1_FREQHIST) {
1351 freq_status =
"HIGH";
1352 }
else if (state1 & ATM90E32_STATUS_S1_FREQLOST) {
1353 freq_status =
"LOW";
1355 freq_status =
"Normal";
1357 if (this->freq_status_text_sensor_ !=
nullptr) {
1358 if (freq_status ==
"Normal") {
1359 ESP_LOGD(TAG,
"Frequency status: %s", freq_status.c_str());
1361 ESP_LOGW(TAG,
"Frequency status: %s", freq_status.c_str());
1363 this->freq_status_text_sensor_->publish_state(freq_status);
1369static uint16_t encode_threshold(
float value) {
1370 if (!std::isfinite(
value) ||
value > UINT16_MAX) {
1371 ESP_LOGW(TAG,
"Threshold exceeds the 16-bit register range; clamping to 65535");
1374 return static_cast<uint16_t
>(std::round(std::max(
value, 0.0f)));
1379static uint16_t calculate_threshold(
float rms,
float scale, uint16_t
gain) {
1382 return encode_threshold(rms * scale * std::numbers::sqrt2_v<float> / (
gain / 16384.0f));
1386 return calculate_threshold(voltage_rms, 100.0f, ugain);
1390 return calculate_threshold(current_rms, 1000.0f, igain);
1394 uint16_t last = this->
read16_(ATM90E32_REGISTER_LASTSPIDATA);
1395 if (last != expected) {
1396 if (context !=
nullptr) {
1397 ESP_LOGW(TAG,
"[%s] SPI read mismatch: expected 0x%04X, got 0x%04X", context, expected, last);
1399 ESP_LOGW(TAG,
"SPI read mismatch: expected 0x%04X, got 0x%04X", expected, last);
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
bool enable_offset_calibration_
float threshold_voltage_peak_v_
float get_local_phase_reactive_power_(uint8_t phase)
float get_phase_forward_active_energy_(uint8_t phase)
bool restored_gain_calibration_
bool has_config_voltage_gain_[3]
void run_gain_calibrations()
bool gain_calibration_mismatch_[3]
OffsetCalibration config_power_offset_phase_[3]
bool has_config_active_power_offset_[3]
float get_phase_current_avg_(uint8_t phase)
bool has_stored_power_offset_calibration_
float get_local_phase_apparent_power_(uint8_t phase)
void write16_(uint16_t a_register, uint16_t val, bool validate=true)
void finish_gain_calibration_(const GainCalibration(&previous)[3])
bool has_config_reactive_power_offset_[3]
bool use_voltage_threshold_flags_
uint16_t frequency_low_threshold_
ESPPreferenceObject power_offset_pref_
const uint16_t voltage_gain_registers[3]
float threshold_current_peak_a_
void restore_offset_calibrations_(OffsetCalibrationType type)
float get_phase_voltage_avg_(uint8_t phase)
const uint16_t current_gain_registers[3]
void run_offset_calibrations()
float get_reference_voltage(uint8_t phase)
bool get_publish_interval_flag_()
void press_calibration_button(EntityBase *entity, void(ATM90E32Component::*action)(), const LogString *hint=nullptr, const LogString *hint2=nullptr)
const uint16_t current_offset_registers[3]
static const uint8_t PHASEB
float get_phase_reverse_active_energy_(uint8_t phase)
GainCalibration gain_phase_[3]
float get_local_phase_harmonic_active_power_(uint8_t phase)
void check_phase_status()
bool has_stored_gain_calibration_
float get_phase_angle_(uint8_t phase)
float get_local_phase_current_(uint8_t phase)
bool validate_spi_read_(uint16_t expected, const char *context=nullptr)
const uint16_t reactive_power_offset_registers[3]
const uint16_t over_voltage_flags[3]
void clear_power_offset_calibrations()
bool has_stored_offset_calibration_
float get_phase_voltage_(uint8_t phase)
float get_chip_temperature_()
GainCalibration config_gain_phase_[3]
bool restored_power_offset_calibration_
int16_t calibrate_offset(uint8_t phase, bool voltage)
float get_local_phase_reverse_active_energy_(uint8_t phase)
void dump_config() override
void restore_gain_calibrations_()
float get_local_phase_forward_active_energy_(uint8_t phase)
OffsetCalibration config_offset_phase_[3]
bool peak_current_signed_
static uint16_t calculate_voltage_threshold_from_volts(float voltage_rms, uint16_t ugain)
float get_local_phase_power_factor_(uint8_t phase)
bool calibration_message_printed_
float get_phase_reactive_power_(uint8_t phase)
void clear_offset_calibrations()
uint16_t frequency_high_threshold_
bool has_config_current_offset_[3]
const uint16_t phase_loss_flags[3]
void log_calibration_status_()
float get_phase_apparent_power_(uint8_t phase)
void write_gains_to_registers_()
float get_local_phase_voltage_(uint8_t phase)
ESPPreferenceObject gain_calibration_pref_
static const uint8_t PHASEA
float get_reference_current(uint8_t phase)
bool verify_gain_writes_()
float get_phase_peak_current_(uint8_t phase)
void finish_offset_calibration_(const OffsetCalibration(&previous)[3], OffsetCalibrationType type)
void clear_gain_calibrations()
float get_phase_harmonic_active_power_(uint8_t phase)
bool has_config_current_gain_[3]
const uint16_t voltage_sag_flags[3]
bool restored_offset_calibration_
float get_phase_active_power_(uint8_t phase)
void run_power_offset_calibrations()
const char * get_calibration_id_()
bool enable_gain_calibration_
void get_cs_summary_(std::span< char, GPIO_SUMMARY_MAX_LEN > buffer)
float threshold_voltage_sag_v_
bool use_current_threshold_flags_
static const uint8_t PHASEC
bool power_offset_calibration_mismatch_[3]
const uint16_t power_offset_registers[3]
float get_setup_priority() const override
uint16_t read16_(uint16_t a_register)
const char * instance_id_
int read32_(uint16_t addr_h, uint16_t addr_l)
void write_offsets_to_registers_(uint8_t phase, int16_t first_offset, int16_t second_offset, OffsetCalibrationType type)
float get_phase_power_factor_(uint8_t phase)
OffsetCalibration power_offset_phase_[3]
float get_local_phase_angle_(uint8_t phase)
const uint16_t voltage_offset_registers[3]
OffsetCalibration offset_phase_[3]
bool has_config_voltage_offset_[3]
bool offset_calibration_mismatch_[3]
bool clear_calibration_pref_(ESPPreferenceObject &preference, bool &has_stored, bool &restored, bool(&mismatches)[3])
static uint16_t calculate_current_threshold_from_amps(float current_rms, uint16_t igain)
text_sensor::TextSensor * phase_status_text_sensor_[3]
float get_phase_current_(uint8_t phase)
float get_local_phase_peak_current_(uint8_t phase)
void set_publish_interval_flag_(bool flag)
int16_t calibrate_power_offset(uint8_t phase, bool reactive)
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
bool verify_offset_writes_(OffsetCalibrationType type)
const char * phase_labels[3]
ESPPreferenceObject offset_pref_
void finish_calibration_(const LogString *name, const LogString *noun, ESPPreferenceObject &preference, const T(&values)[3], bool writes_verified, bool &has_stored, bool &restored, bool(&mismatches)[3], F &&rollback)
float get_local_phase_active_power_(uint8_t phase)
void spi_setup() override
void transfer_array(uint8_t *data, size_t length)
void write_array(const uint8_t *data, size_t length)
bool offset_register_value_matches(uint16_t actual, int16_t expected)
@ OFFSET_CALIBRATION_TYPE_VOLTAGE_CURRENT
@ OFFSET_CALIBRATION_TYPE_POWER
ESPHOME_LOG_TAG(TAG, "atm90e32")
constexpr float IO
For components that represent GPIO pins like PCF8573.
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value)
Extend a FNV-1 hash with an integer (hashes each byte).
ESPPreferences * global_preferences
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
void HOT delay(uint32_t ms)
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
bool sync()
Commit pending writes to flash.
uint32_t cumulative_reverse_active_energy_
float reverse_active_energy_
int16_t active_power_offset_
float forward_active_energy_
int16_t reactive_power_offset_
float harmonic_active_power_
uint32_t cumulative_forward_active_energy_