75 ESP_LOGCONFIG(TAG,
"CSE7761:");
76 ESP_LOGCONFIG(TAG,
" Current input: %s",
78 LOG_UPDATE_INTERVAL(
this);
79 LOG_SENSOR(
" ",
"Voltage", this->voltage_sensor_);
80 LOG_SENSOR(
" ",
"Current 1", this->current_1_sensor_);
81 LOG_SENSOR(
" ",
"Current 2", this->current_2_sensor_);
82 LOG_SENSOR(
" ",
"Power 1", this->active_power_1_sensor_);
83 LOG_SENSOR(
" ",
"Power 2", this->active_power_2_sensor_);
84 LOG_SENSOR(
" ",
"Frequency", this->frequency_sensor_);
85 LOG_SENSOR(
" ",
"Power Factor 1", this->power_factor_1_sensor_);
185 uint16_t calc_chksum = 0xFFFF;
190 calc_chksum = ~calc_chksum;
192 uint16_t coeff_chksum = this->
read_(CSE7761_REG_COEFFCHKSUM, 2);
193 if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
194 ESP_LOGD(TAG,
"Default calibration");
198 if (energy_configured) {
199 ESP_LOGW(TAG,
"No energy calibration, energy unavailable");
201 }
else if (energy_configured) {
203 if (!this->
read_(CSE7761_REG_HFCONST, 2, &hf_const)) {
204 ESP_LOGW(TAG,
"HFConst read failed, energy unavailable");
208 for (uint8_t channel = 0; channel < 2; channel++) {
210 this->
energy_[channel].
wh_per_pulse =
static_cast<float>(scale) * CSE7761_ENERGY_WH_SCALE * input_scale;
214 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
216 uint8_t sys_status = this->
read_(CSE7761_REG_SYSSTATUS, 1);
217 if (sys_status & 0x10) {
218 this->
write_(CSE7761_REG_SYSCON | 0x80,
220 this->
write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
222 this->
write_(CSE7761_REG_EMUCON2 | 0x80, this->frequency_sensor_ !=
nullptr ? 0x0FE5 : 0x0FC1);
223 this->
write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
225 ESP_LOGD(TAG,
"Write failed at chip_init");
256 PUBLISH_STATE(this->voltage_sensor_, voltage);
259 if (this->frequency_sensor_ !=
nullptr && this->
read_(CSE7761_REG_UFREQ, 2, &raw_reg)) {
261 float frequency = (raw_reg == 0 || raw_reg >= 0x8000) ? 0.0f : CSE7761_UFREQ_CLOCK / raw_reg;
262 this->frequency_sensor_->publish_state(
frequency);
265 if (this->power_factor_1_sensor_ !=
nullptr && this->
read_(CSE7761_REG_POWERFACTOR, 3, &raw_reg)) {
267 int24_t pf{
static_cast<int32_t
>(raw_reg)};
268 this->power_factor_1_sensor_->publish_state(
static_cast<float>(
static_cast<int32_t
>(pf)) / 0x800000);
271 for (uint8_t channel = 0; channel < 2; channel++) {
276 ESP_LOGD(TAG,
"Channel %d power %f W, current %f A", channel + 1, active_power, amps);
279 uint8_t energy_reg = (channel == 0) ? CSE7761_REG_ENERGY_PA : CSE7761_REG_ENERGY_PB;
281 if (energy_sensor !=
nullptr && energy.
wh_per_pulse > 0.0f && this->read_(energy_reg, 3, &
raw)) {
288 PUBLISH_STATE(this->active_power_1_sensor_, active_power);
289 PUBLISH_STATE(this->current_1_sensor_, amps);
290 }
else if (channel == 1) {
291 PUBLISH_STATE(this->active_power_2_sensor_, active_power);
292 PUBLISH_STATE(this->current_2_sensor_, amps);