diff --git a/esphome/components/template/binary_sensor/template_binary_sensor.h b/esphome/components/template/binary_sensor/template_binary_sensor.h index 0373f898a8..bc591391b9 100644 --- a/esphome/components/template/binary_sensor/template_binary_sensor.h +++ b/esphome/components/template/binary_sensor/template_binary_sensor.h @@ -9,10 +9,7 @@ namespace template_ { class TemplateBinarySensor : public Component, public binary_sensor::BinarySensor { public: - template void set_template(F &&f) { - this->f_.set(std::forward(f)); - this->enable_loop(); - } + template void set_template(F &&f) { this->f_.set(std::forward(f)); } void setup() override; void loop() override; diff --git a/esphome/components/template/cover/template_cover.h b/esphome/components/template/cover/template_cover.h index 56ab61c3fb..faff69f867 100644 --- a/esphome/components/template/cover/template_cover.h +++ b/esphome/components/template/cover/template_cover.h @@ -18,14 +18,8 @@ class TemplateCover : public cover::Cover, public Component { public: TemplateCover(); - template void set_state_lambda(F &&f) { - this->state_f_.set(std::forward(f)); - this->enable_loop(); - } - template void set_tilt_lambda(F &&f) { - this->tilt_f_.set(std::forward(f)); - this->enable_loop(); - } + template void set_state_lambda(F &&f) { this->state_f_.set(std::forward(f)); } + template void set_tilt_lambda(F &&f) { this->tilt_f_.set(std::forward(f)); } Trigger<> *get_open_trigger() const; Trigger<> *get_close_trigger() const; Trigger<> *get_stop_trigger() const; diff --git a/esphome/components/template/lock/template_lock.cpp b/esphome/components/template/lock/template_lock.cpp index 634924a805..8ed87b9736 100644 --- a/esphome/components/template/lock/template_lock.cpp +++ b/esphome/components/template/lock/template_lock.cpp @@ -11,6 +11,11 @@ static const char *const TAG = "template.lock"; TemplateLock::TemplateLock() : lock_trigger_(new Trigger<>()), unlock_trigger_(new Trigger<>()), open_trigger_(new Trigger<>()) {} +void TemplateLock::setup() { + if (!this->f_.has_value()) + this->disable_loop(); +} + void TemplateLock::loop() { auto val = this->f_(); if (val.has_value()) { diff --git a/esphome/components/template/lock/template_lock.h b/esphome/components/template/lock/template_lock.h index 347c4effb3..14fca4635e 100644 --- a/esphome/components/template/lock/template_lock.h +++ b/esphome/components/template/lock/template_lock.h @@ -12,12 +12,10 @@ class TemplateLock : public lock::Lock, public Component { public: TemplateLock(); + void setup() override; void dump_config() override; - template void set_state_lambda(F &&f) { - this->f_.set(std::forward(f)); - this->enable_loop(); - } + template void set_state_lambda(F &&f) { this->f_.set(std::forward(f)); } Trigger<> *get_lock_trigger() const; Trigger<> *get_unlock_trigger() const; Trigger<> *get_open_trigger() const; diff --git a/esphome/components/template/switch/template_switch.h b/esphome/components/template/switch/template_switch.h index 47154fd047..18a374df35 100644 --- a/esphome/components/template/switch/template_switch.h +++ b/esphome/components/template/switch/template_switch.h @@ -15,10 +15,7 @@ class TemplateSwitch : public switch_::Switch, public Component { void setup() override; void dump_config() override; - template void set_state_lambda(F &&f) { - this->f_.set(std::forward(f)); - this->enable_loop(); - } + template void set_state_lambda(F &&f) { this->f_.set(std::forward(f)); } Trigger<> *get_turn_on_trigger() const; Trigger<> *get_turn_off_trigger() const; void set_optimistic(bool optimistic); diff --git a/esphome/components/template/valve/template_valve.h b/esphome/components/template/valve/template_valve.h index 23a77ff918..d6235f8e5c 100644 --- a/esphome/components/template/valve/template_valve.h +++ b/esphome/components/template/valve/template_valve.h @@ -18,10 +18,7 @@ class TemplateValve : public valve::Valve, public Component { public: TemplateValve(); - template void set_state_lambda(F &&f) { - this->state_f_.set(std::forward(f)); - this->enable_loop(); - } + template void set_state_lambda(F &&f) { this->state_f_.set(std::forward(f)); } Trigger<> *get_open_trigger() const; Trigger<> *get_close_trigger() const; Trigger<> *get_stop_trigger() const; diff --git a/tests/integration/fixtures/runtime_stats.yaml b/tests/integration/fixtures/runtime_stats.yaml index aad1c275fb..fd34cdb939 100644 --- a/tests/integration/fixtures/runtime_stats.yaml +++ b/tests/integration/fixtures/runtime_stats.yaml @@ -32,6 +32,7 @@ switch: name: "Test Switch" id: test_switch optimistic: true + lambda: return false; interval: - interval: 0.5s