mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
Added energy sensor to hlw8012 (#1198)
This commit is contained in:
parent
ef89249019
commit
57b56010da
4 changed files with 18 additions and 2 deletions
|
@ -79,6 +79,12 @@ void HLW8012Component::update() {
|
||||||
this->power_sensor_->publish_state(power);
|
this->power_sensor_->publish_state(power);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->energy_sensor_ != nullptr) {
|
||||||
|
cf_total_pulses_ += raw_cf;
|
||||||
|
float energy = cf_total_pulses_ * power_multiplier_micros / 3600 / 1000000.0f;
|
||||||
|
this->energy_sensor_->publish_state(energy);
|
||||||
|
}
|
||||||
|
|
||||||
if (this->change_mode_at_++ == this->change_mode_every_) {
|
if (this->change_mode_at_++ == this->change_mode_every_) {
|
||||||
this->current_mode_ = !this->current_mode_;
|
this->current_mode_ = !this->current_mode_;
|
||||||
ESP_LOGV(TAG, "Changing mode to %s mode", this->current_mode_ ? "CURRENT" : "VOLTAGE");
|
ESP_LOGV(TAG, "Changing mode to %s mode", this->current_mode_ ? "CURRENT" : "VOLTAGE");
|
||||||
|
|
|
@ -29,6 +29,7 @@ class HLW8012Component : public PollingComponent {
|
||||||
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
||||||
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
|
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
|
||||||
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
||||||
|
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t nth_value_{0};
|
uint32_t nth_value_{0};
|
||||||
|
@ -37,6 +38,7 @@ class HLW8012Component : public PollingComponent {
|
||||||
uint32_t change_mode_every_{8};
|
uint32_t change_mode_every_{8};
|
||||||
float current_resistor_{0.001};
|
float current_resistor_{0.001};
|
||||||
float voltage_divider_{2351};
|
float voltage_divider_{2351};
|
||||||
|
uint64_t cf_total_pulses_{0};
|
||||||
GPIOPin *sel_pin_;
|
GPIOPin *sel_pin_;
|
||||||
GPIOPin *cf_pin_;
|
GPIOPin *cf_pin_;
|
||||||
pulse_counter::PulseCounterStorage cf_store_;
|
pulse_counter::PulseCounterStorage cf_store_;
|
||||||
|
@ -45,6 +47,7 @@ class HLW8012Component : public PollingComponent {
|
||||||
sensor::Sensor *voltage_sensor_{nullptr};
|
sensor::Sensor *voltage_sensor_{nullptr};
|
||||||
sensor::Sensor *current_sensor_{nullptr};
|
sensor::Sensor *current_sensor_{nullptr};
|
||||||
sensor::Sensor *power_sensor_{nullptr};
|
sensor::Sensor *power_sensor_{nullptr};
|
||||||
|
sensor::Sensor *energy_sensor_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace hlw8012
|
} // namespace hlw8012
|
||||||
|
|
|
@ -3,8 +3,8 @@ import esphome.config_validation as cv
|
||||||
from esphome import pins
|
from esphome import pins
|
||||||
from esphome.components import sensor
|
from esphome.components import sensor
|
||||||
from esphome.const import CONF_CHANGE_MODE_EVERY, CONF_INITIAL_MODE, CONF_CURRENT, \
|
from esphome.const import CONF_CHANGE_MODE_EVERY, CONF_INITIAL_MODE, CONF_CURRENT, \
|
||||||
CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_SEL_PIN, CONF_VOLTAGE, CONF_VOLTAGE_DIVIDER, \
|
CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_ENERGY, CONF_SEL_PIN, CONF_VOLTAGE, \
|
||||||
ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT
|
CONF_VOLTAGE_DIVIDER, ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, UNIT_WATT_HOURS
|
||||||
|
|
||||||
AUTO_LOAD = ['pulse_counter']
|
AUTO_LOAD = ['pulse_counter']
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ CONFIG_SCHEMA = cv.Schema({
|
||||||
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1),
|
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1),
|
||||||
cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2),
|
cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2),
|
||||||
cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 1),
|
cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 1),
|
||||||
|
cv.Optional(CONF_ENERGY): sensor.sensor_schema(UNIT_WATT_HOURS, ICON_FLASH, 1),
|
||||||
|
|
||||||
cv.Optional(CONF_CURRENT_RESISTOR, default=0.001): cv.resistance,
|
cv.Optional(CONF_CURRENT_RESISTOR, default=0.001): cv.resistance,
|
||||||
cv.Optional(CONF_VOLTAGE_DIVIDER, default=2351): cv.positive_float,
|
cv.Optional(CONF_VOLTAGE_DIVIDER, default=2351): cv.positive_float,
|
||||||
|
@ -57,6 +58,9 @@ def to_code(config):
|
||||||
if CONF_POWER in config:
|
if CONF_POWER in config:
|
||||||
sens = yield sensor.new_sensor(config[CONF_POWER])
|
sens = yield sensor.new_sensor(config[CONF_POWER])
|
||||||
cg.add(var.set_power_sensor(sens))
|
cg.add(var.set_power_sensor(sens))
|
||||||
|
if CONF_ENERGY in config:
|
||||||
|
sens = yield sensor.new_sensor(config[CONF_ENERGY])
|
||||||
|
cg.add(var.set_energy_sensor(sens))
|
||||||
cg.add(var.set_current_resistor(config[CONF_CURRENT_RESISTOR]))
|
cg.add(var.set_current_resistor(config[CONF_CURRENT_RESISTOR]))
|
||||||
cg.add(var.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER]))
|
cg.add(var.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER]))
|
||||||
cg.add(var.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY]))
|
cg.add(var.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY]))
|
||||||
|
|
|
@ -434,6 +434,9 @@ sensor:
|
||||||
power:
|
power:
|
||||||
name: 'HLW8012 Power'
|
name: 'HLW8012 Power'
|
||||||
id: hlw8012_power
|
id: hlw8012_power
|
||||||
|
energy:
|
||||||
|
name: "HLW8012 Energy"
|
||||||
|
id: hlw8012_energy
|
||||||
update_interval: 15s
|
update_interval: 15s
|
||||||
current_resistor: 0.001 ohm
|
current_resistor: 0.001 ohm
|
||||||
voltage_divider: 2351
|
voltage_divider: 2351
|
||||||
|
|
Loading…
Reference in a new issue