From cc6d1e85ccc9bb38d51b95ea9fa7a833168ac664 Mon Sep 17 00:00:00 2001 From: elyorkhakimov Date: Wed, 28 Apr 2021 12:15:50 -0700 Subject: [PATCH] Addition of forward and reverse active energy counters to ATM90E32 sensor component (#1271) Co-authored-by: Elyor Khakimov --- esphome/components/atm90e32/atm90e32.cpp | 48 ++++++++++++++++++++++++ esphome/components/atm90e32/atm90e32.h | 14 +++++++ esphome/components/atm90e32/sensor.py | 16 ++++++++ esphome/const.py | 2 + 4 files changed, 80 insertions(+) diff --git a/esphome/components/atm90e32/atm90e32.cpp b/esphome/components/atm90e32/atm90e32.cpp index 85e38fce3e..d732212cdd 100644 --- a/esphome/components/atm90e32/atm90e32.cpp +++ b/esphome/components/atm90e32/atm90e32.cpp @@ -58,6 +58,24 @@ void ATM90E32Component::update() { if (this->phase_[2].power_factor_sensor_ != nullptr) { this->phase_[2].power_factor_sensor_->publish_state(this->get_power_factor_c_()); } + if (this->phase_[0].forward_active_energy_sensor_ != nullptr) { + this->phase_[0].forward_active_energy_sensor_->publish_state(this->get_forward_active_energy_a_()); + } + if (this->phase_[1].forward_active_energy_sensor_ != nullptr) { + this->phase_[1].forward_active_energy_sensor_->publish_state(this->get_forward_active_energy_b_()); + } + if (this->phase_[2].forward_active_energy_sensor_ != nullptr) { + this->phase_[2].forward_active_energy_sensor_->publish_state(this->get_forward_active_energy_c_()); + } + if (this->phase_[0].reverse_active_energy_sensor_ != nullptr) { + this->phase_[0].reverse_active_energy_sensor_->publish_state(this->get_reverse_active_energy_a_()); + } + if (this->phase_[1].reverse_active_energy_sensor_ != nullptr) { + this->phase_[1].reverse_active_energy_sensor_->publish_state(this->get_reverse_active_energy_b_()); + } + if (this->phase_[2].reverse_active_energy_sensor_ != nullptr) { + this->phase_[2].reverse_active_energy_sensor_->publish_state(this->get_reverse_active_energy_c_()); + } if (this->freq_sensor_ != nullptr) { this->freq_sensor_->publish_state(this->get_frequency_()); } @@ -119,16 +137,22 @@ void ATM90E32Component::dump_config() { LOG_SENSOR(" ", "Power A", this->phase_[0].power_sensor_); LOG_SENSOR(" ", "Reactive Power A", this->phase_[0].reactive_power_sensor_); LOG_SENSOR(" ", "PF A", this->phase_[0].power_factor_sensor_); + LOG_SENSOR(" ", "Active Forward Energy A", this->phase_[0].forward_active_energy_sensor_); + LOG_SENSOR(" ", "Active Reverse Energy A", this->phase_[0].reverse_active_energy_sensor_); LOG_SENSOR(" ", "Voltage B", this->phase_[1].voltage_sensor_); LOG_SENSOR(" ", "Current B", this->phase_[1].current_sensor_); LOG_SENSOR(" ", "Power B", this->phase_[1].power_sensor_); LOG_SENSOR(" ", "Reactive Power B", this->phase_[1].reactive_power_sensor_); LOG_SENSOR(" ", "PF B", this->phase_[1].power_factor_sensor_); + LOG_SENSOR(" ", "Active Forward Energy B", this->phase_[1].forward_active_energy_sensor_); + LOG_SENSOR(" ", "Active Reverse Energy B", this->phase_[1].reverse_active_energy_sensor_); LOG_SENSOR(" ", "Voltage C", this->phase_[2].voltage_sensor_); LOG_SENSOR(" ", "Current C", this->phase_[2].current_sensor_); LOG_SENSOR(" ", "Power C", this->phase_[2].power_sensor_); LOG_SENSOR(" ", "Reactive Power C", this->phase_[2].reactive_power_sensor_); LOG_SENSOR(" ", "PF C", this->phase_[2].power_factor_sensor_); + LOG_SENSOR(" ", "Active Forward Energy C", this->phase_[2].forward_active_energy_sensor_); + LOG_SENSOR(" ", "Active Reverse Energy C", this->phase_[2].reverse_active_energy_sensor_); LOG_SENSOR(" ", "Frequency", this->freq_sensor_); LOG_SENSOR(" ", "Chip Temp", this->chip_temperature_sensor_); } @@ -239,6 +263,30 @@ float ATM90E32Component::get_power_factor_c_() { int16_t pf = this->read16_(ATM90E32_REGISTER_PFMEANC); return (float) pf / 1000; } +float ATM90E32Component::get_forward_active_energy_a_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_APENERGYA); + return (float) val * 10 / 3200; // convert register value to WattHours +} +float ATM90E32Component::get_forward_active_energy_b_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_APENERGYB); + return (float) val * 10 / 3200; +} +float ATM90E32Component::get_forward_active_energy_c_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_APENERGYC); + return (float) val * 10 / 3200; +} +float ATM90E32Component::get_reverse_active_energy_a_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGYA); + return (float) val * 10 / 3200; +} +float ATM90E32Component::get_reverse_active_energy_b_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGYB); + return (float) val * 10 / 3200; +} +float ATM90E32Component::get_reverse_active_energy_c_() { + uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGYC); + return (float) val * 10 / 3200; +} float ATM90E32Component::get_frequency_() { uint16_t freq = this->read16_(ATM90E32_REGISTER_FREQ); return (float) freq / 100; diff --git a/esphome/components/atm90e32/atm90e32.h b/esphome/components/atm90e32/atm90e32.h index eb5de3878c..89d62adaf6 100644 --- a/esphome/components/atm90e32/atm90e32.h +++ b/esphome/components/atm90e32/atm90e32.h @@ -20,6 +20,12 @@ class ATM90E32Component : public PollingComponent, 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_reactive_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].reactive_power_sensor_ = obj; } + void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj) { + this->phase_[phase].forward_active_energy_sensor_ = obj; + } + void set_reverse_active_energy_sensor(int phase, sensor::Sensor *obj) { + this->phase_[phase].reverse_active_energy_sensor_ = obj; + } void set_power_factor_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_factor_sensor_ = obj; } void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].volt_gain_ = gain; } void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; } @@ -52,6 +58,12 @@ class ATM90E32Component : public PollingComponent, float get_power_factor_a_(); float get_power_factor_b_(); float get_power_factor_c_(); + float get_forward_active_energy_a_(); + float get_forward_active_energy_b_(); + float get_forward_active_energy_c_(); + float get_reverse_active_energy_a_(); + float get_reverse_active_energy_b_(); + float get_reverse_active_energy_c_(); float get_frequency_(); float get_chip_temperature_(); @@ -63,6 +75,8 @@ class ATM90E32Component : public PollingComponent, sensor::Sensor *power_sensor_{nullptr}; sensor::Sensor *reactive_power_sensor_{nullptr}; sensor::Sensor *power_factor_sensor_{nullptr}; + sensor::Sensor *forward_active_energy_sensor_{nullptr}; + sensor::Sensor *reverse_active_energy_sensor_{nullptr}; } phase_[3]; sensor::Sensor *freq_sensor_{nullptr}; sensor::Sensor *chip_temperature_sensor_{nullptr}; diff --git a/esphome/components/atm90e32/sensor.py b/esphome/components/atm90e32/sensor.py index d0813cfa52..4a9100d9d6 100644 --- a/esphome/components/atm90e32/sensor.py +++ b/esphome/components/atm90e32/sensor.py @@ -8,8 +8,11 @@ from esphome.const import ( CONF_POWER, CONF_POWER_FACTOR, CONF_FREQUENCY, + CONF_FORWARD_ACTIVE_ENERGY, + CONF_REVERSE_ACTIVE_ENERGY, DEVICE_CLASS_CURRENT, DEVICE_CLASS_EMPTY, + DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_POWER_FACTOR, DEVICE_CLASS_TEMPERATURE, @@ -24,6 +27,7 @@ from esphome.const import ( UNIT_EMPTY, UNIT_CELSIUS, UNIT_VOLT_AMPS_REACTIVE, + UNIT_WATT_HOURS, ) CONF_PHASE_A = "phase_a" @@ -73,6 +77,12 @@ ATM90E32_PHASE_SCHEMA = cv.Schema( cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema( UNIT_EMPTY, ICON_EMPTY, 2, DEVICE_CLASS_POWER_FACTOR ), + cv.Optional(CONF_FORWARD_ACTIVE_ENERGY): sensor.sensor_schema( + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + ), + cv.Optional(CONF_REVERSE_ACTIVE_ENERGY): sensor.sensor_schema( + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + ), cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t, cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t, } @@ -129,6 +139,12 @@ def to_code(config): if CONF_POWER_FACTOR in conf: sens = yield sensor.new_sensor(conf[CONF_POWER_FACTOR]) cg.add(var.set_power_factor_sensor(i, sens)) + if CONF_FORWARD_ACTIVE_ENERGY in conf: + sens = yield sensor.new_sensor(conf[CONF_FORWARD_ACTIVE_ENERGY]) + cg.add(var.set_forward_active_energy_sensor(i, sens)) + if CONF_REVERSE_ACTIVE_ENERGY in conf: + sens = yield sensor.new_sensor(conf[CONF_REVERSE_ACTIVE_ENERGY]) + cg.add(var.set_reverse_active_energy_sensor(i, sens)) if CONF_FREQUENCY in config: sens = yield sensor.new_sensor(config[CONF_FREQUENCY]) cg.add(var.set_freq_sensor(sens)) diff --git a/esphome/const.py b/esphome/const.py index 629495bdd1..618c9fd922 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -213,6 +213,7 @@ CONF_FOR = "for" CONF_FORCE_UPDATE = "force_update" CONF_FORMALDEHYDE = "formaldehyde" CONF_FORMAT = "format" +CONF_FORWARD_ACTIVE_ENERGY = "forward_active_energy" CONF_FREQUENCY = "frequency" CONF_FROM = "from" CONF_FULL_UPDATE_EVERY = "full_update_every" @@ -447,6 +448,7 @@ CONF_RESTORE_MODE = "restore_mode" CONF_RESTORE_STATE = "restore_state" CONF_RESTORE_VALUE = "restore_value" CONF_RETAIN = "retain" +CONF_REVERSE_ACTIVE_ENERGY = "reverse_active_energy" CONF_RGB_ORDER = "rgb_order" CONF_RGBW = "rgbw" CONF_RISING_EDGE = "rising_edge"