From 39b35b79ba24146dfd7d399bdceca7c4d25cd75e Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Wed, 29 Apr 2020 00:24:06 +0200 Subject: [PATCH] Make initial run variable available to addressable_lambda (#1035) * Make initial run variable available to addressable_lambda * Fix linting * Remove if clause --- esphome/components/light/addressable_light_effect.h | 10 +++++++--- esphome/components/light/effects.py | 2 +- tests/test1.yaml | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/esphome/components/light/addressable_light_effect.h b/esphome/components/light/addressable_light_effect.h index 78ae41baad..e6528fcd8a 100644 --- a/esphome/components/light/addressable_light_effect.h +++ b/esphome/components/light/addressable_light_effect.h @@ -50,21 +50,25 @@ class AddressableLightEffect : public LightEffect { class AddressableLambdaLightEffect : public AddressableLightEffect { public: - AddressableLambdaLightEffect(const std::string &name, const std::function &f, + AddressableLambdaLightEffect(const std::string &name, + const std::function &f, uint32_t update_interval) : AddressableLightEffect(name), f_(f), update_interval_(update_interval) {} + void start() override { this->initial_run_ = true; } void apply(AddressableLight &it, const ESPColor ¤t_color) override { const uint32_t now = millis(); if (now - this->last_run_ >= this->update_interval_) { this->last_run_ = now; - this->f_(it, current_color); + this->f_(it, current_color, this->initial_run_); + this->initial_run_ = false; } } protected: - std::function f_; + std::function f_; uint32_t update_interval_; uint32_t last_run_{0}; + bool initial_run_; }; class AddressableRainbowLightEffect : public AddressableLightEffect { diff --git a/esphome/components/light/effects.py b/esphome/components/light/effects.py index d08e7f08f5..d8c709b8ad 100644 --- a/esphome/components/light/effects.py +++ b/esphome/components/light/effects.py @@ -162,7 +162,7 @@ def flicker_effect_to_code(config, effect_id): } ) def addressable_lambda_effect_to_code(config, effect_id): - args = [(AddressableLightRef, 'it'), (ESPColor, 'current_color')] + args = [(AddressableLightRef, 'it'), (ESPColor, 'current_color'), (bool, 'initial_run')] lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], args, return_type=cg.void) var = cg.new_Pvariable(effect_id, config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]) diff --git a/tests/test1.yaml b/tests/test1.yaml index 59b64b0b6d..18adcf70a3 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1155,7 +1155,9 @@ light: - addressable_lambda: name: "Test For Custom Lambda Effect" lambda: |- - it[0] = current_color; + if (initial_run) { + it[0] = current_color; + } - automation: name: Custom Effect