mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 05:54:56 +01:00
Stop infinite loop in light on_turn_on (#1219)
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
57eb05c0e3
commit
86736aa480
1 changed files with 12 additions and 4 deletions
|
@ -103,10 +103,14 @@ class LightTurnOnTrigger : public Trigger<> {
|
|||
LightTurnOnTrigger(LightState *a_light) {
|
||||
a_light->add_new_remote_values_callback([this, a_light]() {
|
||||
auto is_on = a_light->current_values.is_on();
|
||||
if (is_on && !last_on_) {
|
||||
// only trigger when going from off to on
|
||||
auto should_trigger = is_on && !last_on_;
|
||||
// Set new state immediately so that trigger() doesn't devolve
|
||||
// into infinite loop
|
||||
last_on_ = is_on;
|
||||
if (should_trigger) {
|
||||
this->trigger();
|
||||
}
|
||||
last_on_ = is_on;
|
||||
});
|
||||
last_on_ = a_light->current_values.is_on();
|
||||
}
|
||||
|
@ -120,10 +124,14 @@ class LightTurnOffTrigger : public Trigger<> {
|
|||
LightTurnOffTrigger(LightState *a_light) {
|
||||
a_light->add_new_remote_values_callback([this, a_light]() {
|
||||
auto is_on = a_light->current_values.is_on();
|
||||
if (!is_on && last_on_) {
|
||||
// only trigger when going from on to off
|
||||
auto should_trigger = !is_on && last_on_;
|
||||
// Set new state immediately so that trigger() doesn't devolve
|
||||
// into infinite loop
|
||||
last_on_ = is_on;
|
||||
if (should_trigger) {
|
||||
this->trigger();
|
||||
}
|
||||
last_on_ = is_on;
|
||||
});
|
||||
last_on_ = a_light->current_values.is_on();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue