ESPHome  2022.9.2
ade7953.cpp
Go to the documentation of this file.
1 #include "ade7953.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace ade7953 {
6 
7 static const char *const TAG = "ade7953";
8 
10  ESP_LOGCONFIG(TAG, "ADE7953:");
11  LOG_PIN(" IRQ Pin: ", irq_pin_);
12  LOG_I2C_DEVICE(this);
13  LOG_UPDATE_INTERVAL(this);
14  LOG_SENSOR(" ", "Voltage Sensor", this->voltage_sensor_);
15  LOG_SENSOR(" ", "Current A Sensor", this->current_a_sensor_);
16  LOG_SENSOR(" ", "Current B Sensor", this->current_b_sensor_);
17  LOG_SENSOR(" ", "Active Power A Sensor", this->active_power_a_sensor_);
18  LOG_SENSOR(" ", "Active Power B Sensor", this->active_power_b_sensor_);
19 }
20 
21 #define ADE_PUBLISH_(name, val, factor) \
22  if (err == i2c::ERROR_OK && this->name##_sensor_) { \
23  float value = (val) / (factor); \
24  this->name##_sensor_->publish_state(value); \
25  }
26 #define ADE_PUBLISH(name, val, factor) ADE_PUBLISH_(name, val, factor)
27 
29  if (!this->is_setup_)
30  return;
31 
32  uint32_t val;
33  i2c::ErrorCode err = ade_read_32_(0x0312, &val);
34  ADE_PUBLISH(active_power_a, (int32_t) val, 154.0f);
35  err = ade_read_32_(0x0313, &val);
36  ADE_PUBLISH(active_power_b, (int32_t) val, 154.0f);
37  err = ade_read_32_(0x031A, &val);
38  ADE_PUBLISH(current_a, (uint32_t) val, 100000.0f);
39  err = ade_read_32_(0x031B, &val);
40  ADE_PUBLISH(current_b, (uint32_t) val, 100000.0f);
41  err = ade_read_32_(0x031C, &val);
42  ADE_PUBLISH(voltage, (uint32_t) val, 26000.0f);
43 
44  // auto apparent_power_a = this->ade_read_<int32_t>(0x0310);
45  // auto apparent_power_b = this->ade_read_<int32_t>(0x0311);
46  // auto reactive_power_a = this->ade_read_<int32_t>(0x0314);
47  // auto reactive_power_b = this->ade_read_<int32_t>(0x0315);
48  // auto power_factor_a = this->ade_read_<int16_t>(0x010A);
49  // auto power_factor_b = this->ade_read_<int16_t>(0x010B);
50 }
51 
52 } // namespace ade7953
53 } // namespace esphome
sensor::Sensor * current_a_sensor_
Definition: ade7953.h:88
sensor::Sensor * current_b_sensor_
Definition: ade7953.h:89
InternalGPIOPin * irq_pin_
Definition: ade7953.h:85
sensor::Sensor * active_power_b_sensor_
Definition: ade7953.h:91
void dump_config() override
Definition: ade7953.cpp:9
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:85
sensor::Sensor * voltage_sensor_
Definition: ade7953.h:87
sensor::Sensor * active_power_a_sensor_
Definition: ade7953.h:90
i2c::ErrorCode ade_read_32_(uint16_t reg, uint32_t *value)
Definition: ade7953.h:66
void update() override
Definition: ade7953.cpp:28