Add restore_value to template number (#2041)

This commit is contained in:
Jesse Hills 2021-07-20 15:40:42 +12:00
parent 53a4689ed1
commit c7a52c3894
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
3 changed files with 39 additions and 16 deletions

View file

@ -9,6 +9,7 @@ from esphome.const import (
CONF_MAX_VALUE,
CONF_MIN_VALUE,
CONF_OPTIMISTIC,
CONF_RESTORE_VALUE,
CONF_STEP,
)
from .. import template_ns
@ -26,6 +27,17 @@ def validate_min_max(config):
return config
def validate(config):
if CONF_LAMBDA in config:
if CONF_OPTIMISTIC in config:
raise cv.Invalid("optimistic cannot be used with lambda")
if CONF_INITIAL_VALUE in config:
raise cv.Invalid("initial_value cannot be used with lambda")
if CONF_RESTORE_VALUE in config:
raise cv.Invalid("restore_value cannot be used with lambda")
return config
CONFIG_SCHEMA = cv.All(
number.NUMBER_SCHEMA.extend(
{
@ -33,13 +45,15 @@ CONFIG_SCHEMA = cv.All(
cv.Required(CONF_MAX_VALUE): cv.float_,
cv.Required(CONF_MIN_VALUE): cv.float_,
cv.Required(CONF_STEP): cv.positive_float,
cv.Exclusive(CONF_LAMBDA, "lambda-optimistic"): cv.returning_lambda,
cv.Exclusive(CONF_OPTIMISTIC, "lambda-optimistic"): cv.boolean,
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
cv.Optional(CONF_OPTIMISTIC): cv.boolean,
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
}
).extend(cv.polling_component_schema("60s")),
validate_min_max,
validate,
)
@ -60,13 +74,15 @@ async def to_code(config):
)
cg.add(var.set_template(template_))
elif CONF_OPTIMISTIC in config:
else:
if CONF_OPTIMISTIC in config:
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
if CONF_INITIAL_VALUE in config:
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
if CONF_RESTORE_VALUE in config:
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
if CONF_SET_ACTION in config:
await automation.build_automation(
var.get_set_trigger(), [(float, "x")], config[CONF_SET_ACTION]
)
if CONF_INITIAL_VALUE in config:
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))

View file

@ -7,17 +7,21 @@ namespace template_ {
static const char *const TAG = "template.number";
void TemplateNumber::setup() {
if (this->f_.has_value() || !this->optimistic_)
if (this->f_.has_value())
return;
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
float value;
if (!this->restore_value_) {
value = this->initial_value_;
} else {
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
if (!this->pref_.load(&value)) {
if (!isnan(this->initial_value_))
value = this->initial_value_;
else
value = this->traits.get_min_value();
}
}
this->publish_state(value);
}
@ -35,11 +39,12 @@ void TemplateNumber::update() {
void TemplateNumber::control(float value) {
this->set_trigger_->trigger(value);
if (this->optimistic_) {
if (this->optimistic_)
this->publish_state(value);
if (this->restore_value_)
this->pref_.save(&value);
}
}
void TemplateNumber::dump_config() {
LOG_NUMBER("", "Template Number", this);
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));

View file

@ -20,11 +20,13 @@ class TemplateNumber : public number::Number, public PollingComponent {
Trigger<float> *get_set_trigger() const { return set_trigger_; }
void set_optimistic(bool optimistic) { optimistic_ = optimistic; }
void set_initial_value(float initial_value) { initial_value_ = initial_value; }
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
protected:
void control(float value) override;
bool optimistic_{false};
float initial_value_{NAN};
bool restore_value_{false};
Trigger<float> *set_trigger_ = new Trigger<float>();
optional<std::function<optional<float>()>> f_;