From 5a67e723895297f78bae960cbfd38c787192a85e Mon Sep 17 00:00:00 2001 From: John <34163498+CircuitSetup@users.noreply.github.com> Date: Sun, 27 Oct 2019 07:05:13 -0400 Subject: [PATCH] Added more power data to the atm90e32 component (#799) * Added more data to atm90e32 component * ignore * correction * Delete 6chan_energy_meter.yaml * Update sensor.py fixed indents * Update atm90e32.h * Update esphome/components/atm90e32/sensor.py Co-Authored-By: Otto Winter * PR request changes * repository test branch * Update setup.py * Update const.py * backslash * comma! * delete test yaml * corrected chip temp * change to signed int for get_pf_ functions * Update atm90e32.h formatting * adjusted function & variable names * Update atm90e32.h formatting * Update sensor.py Import CONF_POWER_FACTOR from const.py * travis formatting * Update esphome/components/atm90e32/sensor.py Co-Authored-By: Otto Winter * Update esphome/components/atm90e32/atm90e32.h Co-Authored-By: Otto Winter --- esphome/components/atm90e32/atm90e32.cpp | 58 +++++++++++++++++++++++- esphome/components/atm90e32/atm90e32.h | 15 ++++++ esphome/components/atm90e32/sensor.py | 23 ++++++++-- 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/esphome/components/atm90e32/atm90e32.cpp b/esphome/components/atm90e32/atm90e32.cpp index 283319994e..bc1e326147 100644 --- a/esphome/components/atm90e32/atm90e32.cpp +++ b/esphome/components/atm90e32/atm90e32.cpp @@ -40,9 +40,30 @@ void ATM90E32Component::update() { if (this->phase_[2].power_sensor_ != nullptr) { this->phase_[2].power_sensor_->publish_state(this->get_active_power_c_()); } + if (this->phase_[0].reactive_power_sensor_ != nullptr) { + this->phase_[0].reactive_power_sensor_->publish_state(this->get_reactive_power_a_()); + } + if (this->phase_[1].reactive_power_sensor_ != nullptr) { + this->phase_[1].reactive_power_sensor_->publish_state(this->get_reactive_power_b_()); + } + if (this->phase_[2].reactive_power_sensor_ != nullptr) { + this->phase_[2].reactive_power_sensor_->publish_state(this->get_reactive_power_c_()); + } + if (this->phase_[0].power_factor_sensor_ != nullptr) { + this->phase_[0].power_factor_sensor_->publish_state(this->get_power_factor_a_()); + } + if (this->phase_[1].power_factor_sensor_ != nullptr) { + this->phase_[1].power_factor_sensor_->publish_state(this->get_power_factor_b_()); + } + if (this->phase_[2].power_factor_sensor_ != nullptr) { + this->phase_[2].power_factor_sensor_->publish_state(this->get_power_factor_c_()); + } if (this->freq_sensor_ != nullptr) { this->freq_sensor_->publish_state(this->get_frequency_()); } + if (this->chip_temperature_sensor_ != nullptr) { + this->chip_temperature_sensor_->publish_state(this->get_chip_temperature_()); + } this->status_clear_warning(); } @@ -89,13 +110,20 @@ void ATM90E32Component::dump_config() { LOG_SENSOR(" ", "Voltage A", this->phase_[0].voltage_sensor_); LOG_SENSOR(" ", "Current A", this->phase_[0].current_sensor_); 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(" ", "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(" ", "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(" ", "Frequency", this->freq_sensor_) + LOG_SENSOR(" ", "Reactive Power C", this->phase_[2].reactive_power_sensor_); + LOG_SENSOR(" ", "PF C", this->phase_[2].power_factor_sensor_); + LOG_SENSOR(" ", "Frequency", this->freq_sensor_); + LOG_SENSOR(" ", "Chip Temp", this->chip_temperature_sensor_); } float ATM90E32Component::get_setup_priority() const { return setup_priority::DATA; } @@ -180,9 +208,37 @@ float ATM90E32Component::get_active_power_c_() { int val = this->read32_(ATM90E32_REGISTER_PMEANC, ATM90E32_REGISTER_PMEANCLSB); return val * 0.00032f; } +float ATM90E32Component::get_reactive_power_a_() { + int val = this->read32_(ATM90E32_REGISTER_QMEANA, ATM90E32_REGISTER_QMEANALSB); + return val * 0.00032f; +} +float ATM90E32Component::get_reactive_power_b_() { + int val = this->read32_(ATM90E32_REGISTER_QMEANB, ATM90E32_REGISTER_QMEANBLSB); + return val * 0.00032f; +} +float ATM90E32Component::get_reactive_power_c_() { + int val = this->read32_(ATM90E32_REGISTER_QMEANC, ATM90E32_REGISTER_QMEANCLSB); + return val * 0.00032f; +} +float ATM90E32Component::get_power_factor_a_() { + int16_t pf = this->read16_(ATM90E32_REGISTER_PFMEANA); + return (float) pf / 1000; +} +float ATM90E32Component::get_power_factor_b_() { + int16_t pf = this->read16_(ATM90E32_REGISTER_PFMEANB); + return (float) pf / 1000; +} +float ATM90E32Component::get_power_factor_c_() { + int16_t pf = this->read16_(ATM90E32_REGISTER_PFMEANC); + return (float) pf / 1000; +} float ATM90E32Component::get_frequency_() { uint16_t freq = this->read16_(ATM90E32_REGISTER_FREQ); return (float) freq / 100; } +float ATM90E32Component::get_chip_temperature_() { + uint16_t ctemp = this->read16_(ATM90E32_REGISTER_TEMP); + return (float) ctemp; +} } // namespace atm90e32 } // namespace esphome diff --git a/esphome/components/atm90e32/atm90e32.h b/esphome/components/atm90e32/atm90e32.h index 4dd2bd5784..3daa31d15d 100644 --- a/esphome/components/atm90e32/atm90e32.h +++ b/esphome/components/atm90e32/atm90e32.h @@ -19,10 +19,15 @@ class ATM90E32Component : public PollingComponent, 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_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_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; } void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; } + void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) { + chip_temperature_sensor_ = chip_temperature_sensor; + } void set_line_freq(int freq) { line_freq_ = freq; } void set_pga_gain(uint16_t gain) { pga_gain_ = gain; } @@ -40,7 +45,14 @@ class ATM90E32Component : public PollingComponent, float get_active_power_a_(); float get_active_power_b_(); float get_active_power_c_(); + float get_reactive_power_a_(); + float get_reactive_power_b_(); + float get_reactive_power_c_(); + float get_power_factor_a_(); + float get_power_factor_b_(); + float get_power_factor_c_(); float get_frequency_(); + float get_chip_temperature_(); struct ATM90E32Phase { uint16_t volt_gain_{41820}; @@ -48,8 +60,11 @@ class ATM90E32Component : public PollingComponent, sensor::Sensor *voltage_sensor_{nullptr}; sensor::Sensor *current_sensor_{nullptr}; sensor::Sensor *power_sensor_{nullptr}; + sensor::Sensor *reactive_power_sensor_{nullptr}; + sensor::Sensor *power_factor_sensor_{nullptr}; } phase_[3]; sensor::Sensor *freq_sensor_{nullptr}; + sensor::Sensor *chip_temperature_sensor_{nullptr}; uint16_t pga_gain_{0x15}; int line_freq_{60}; }; diff --git a/esphome/components/atm90e32/sensor.py b/esphome/components/atm90e32/sensor.py index 7b62740f8e..030ff90a77 100644 --- a/esphome/components/atm90e32/sensor.py +++ b/esphome/components/atm90e32/sensor.py @@ -2,14 +2,17 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor, spi from esphome.const import \ - CONF_ID, CONF_VOLTAGE, CONF_CURRENT, CONF_POWER, CONF_FREQUENCY, \ - ICON_FLASH, UNIT_HZ, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT + CONF_ID, CONF_VOLTAGE, CONF_CURRENT, CONF_POWER, CONF_POWER_FACTOR, CONF_FREQUENCY, \ + ICON_FLASH, ICON_LIGHTBULB, ICON_CURRENT_AC, ICON_THERMOMETER, \ + UNIT_HZ, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, UNIT_EMPTY, UNIT_CELSIUS CONF_PHASE_A = 'phase_a' CONF_PHASE_B = 'phase_b' CONF_PHASE_C = 'phase_c' +CONF_REACTIVE_POWER = 'reactive_power' CONF_LINE_FREQUENCY = 'line_frequency' +CONF_CHIP_TEMPERATURE = 'chip_temperature' CONF_GAIN_PGA = 'gain_pga' CONF_GAIN_VOLTAGE = 'gain_voltage' CONF_GAIN_CT = 'gain_ct' @@ -28,8 +31,10 @@ ATM90E32Component = atm90e32_ns.class_('ATM90E32Component', cg.PollingComponent, ATM90E32_PHASE_SCHEMA = cv.Schema({ cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 2), - cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), + cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_CURRENT_AC, 2), cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 2), + cv.Optional(CONF_REACTIVE_POWER): sensor.sensor_schema(UNIT_EMPTY, ICON_LIGHTBULB, 2), + cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(UNIT_EMPTY, ICON_FLASH, 2), cv.Optional(CONF_GAIN_VOLTAGE, default=41820): cv.uint16_t, cv.Optional(CONF_GAIN_CT, default=25498): cv.uint16_t, }) @@ -39,7 +44,8 @@ CONFIG_SCHEMA = cv.Schema({ cv.Optional(CONF_PHASE_A): ATM90E32_PHASE_SCHEMA, cv.Optional(CONF_PHASE_B): ATM90E32_PHASE_SCHEMA, cv.Optional(CONF_PHASE_C): ATM90E32_PHASE_SCHEMA, - cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(UNIT_HZ, ICON_FLASH, 1), + cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(UNIT_HZ, ICON_CURRENT_AC, 1), + cv.Optional(CONF_CHIP_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1), cv.Required(CONF_LINE_FREQUENCY): cv.enum(LINE_FREQS, upper=True), cv.Optional(CONF_GAIN_PGA, default='2X'): cv.enum(PGA_GAINS, upper=True), }).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA) @@ -65,8 +71,17 @@ def to_code(config): if CONF_POWER in conf: sens = yield sensor.new_sensor(conf[CONF_POWER]) cg.add(var.set_power_sensor(i, sens)) + if CONF_REACTIVE_POWER in conf: + sens = yield sensor.new_sensor(conf[CONF_REACTIVE_POWER]) + cg.add(var.set_react_pow_sensor(i, sens)) + if CONF_POWER_FACTOR in conf: + sens = yield sensor.new_sensor(conf[CONF_POWER_FACTOR]) + cg.add(var.set_pf_sensor(i, sens)) if CONF_FREQUENCY in config: sens = yield sensor.new_sensor(config[CONF_FREQUENCY]) cg.add(var.set_freq_sensor(sens)) + if CONF_CHIP_TEMPERATURE in config: + sens = yield sensor.new_sensor(config[CONF_CHIP_TEMPERATURE]) + cg.add(var.set_chip_temp_sensor(sens)) cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY])) cg.add(var.set_pga_gain(config[CONF_GAIN_PGA]))