Add ability to lock to set mode (#5924)

This commit is contained in:
Yorick Smilda 2023-12-20 11:52:46 +01:00 committed by GitHub
parent 84174aeb80
commit 23ceddafed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 5 deletions

View file

@ -96,7 +96,7 @@ void HLW8012Component::update() {
this->energy_sensor_->publish_state(energy); this->energy_sensor_->publish_state(energy);
} }
if (this->change_mode_at_++ == this->change_mode_every_) { if (this->change_mode_every_ != 0 && 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");
this->change_mode_at_ = 0; this->change_mode_at_ = 0;

View file

@ -79,8 +79,9 @@ CONFIG_SCHEMA = cv.Schema(
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,
cv.Optional(CONF_MODEL, default="HLW8012"): cv.enum(MODELS, upper=True), cv.Optional(CONF_MODEL, default="HLW8012"): cv.enum(MODELS, upper=True),
cv.Optional(CONF_CHANGE_MODE_EVERY, default=8): cv.All( cv.Optional(CONF_CHANGE_MODE_EVERY, default=8): cv.Any(
cv.uint32_t, cv.Range(min=1) "never",
cv.All(cv.uint32_t, cv.Range(min=1)),
), ),
cv.Optional(CONF_INITIAL_MODE, default=CONF_VOLTAGE): cv.one_of( cv.Optional(CONF_INITIAL_MODE, default=CONF_VOLTAGE): cv.one_of(
*INITIAL_MODES, lower=True *INITIAL_MODES, lower=True
@ -114,6 +115,10 @@ async def to_code(config):
cg.add(var.set_energy_sensor(sens)) 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_initial_mode(INITIAL_MODES[config[CONF_INITIAL_MODE]])) cg.add(var.set_initial_mode(INITIAL_MODES[config[CONF_INITIAL_MODE]]))
cg.add(var.set_sensor_model(config[CONF_MODEL])) cg.add(var.set_sensor_model(config[CONF_MODEL]))
interval = config[CONF_CHANGE_MODE_EVERY]
if interval == "never":
interval = 0
cg.add(var.set_change_mode_every(interval))

View file

@ -794,7 +794,7 @@ sensor:
update_interval: 15s update_interval: 15s
current_resistor: 0.001 ohm current_resistor: 0.001 ohm
voltage_divider: 2351 voltage_divider: 2351
change_mode_every: 16 change_mode_every: "never"
initial_mode: VOLTAGE initial_mode: VOLTAGE
model: hlw8012 model: hlw8012
- platform: total_daily_energy - platform: total_daily_energy