From 5cd7f23065d2925eeb507e5cbc2cdfc31e1078fd Mon Sep 17 00:00:00 2001 From: rnauber Date: Wed, 19 Jun 2019 11:33:14 +0200 Subject: [PATCH] Provide the lights current color to the addressable_lambda_effect. (#646) * Provide the lights current color to the addressable_lambda_effect. * Fix formatting * More formatting * Change the call signature of the lambda * lint Co-authored-by: olg --- esphome/components/light/addressable_light_effect.h | 6 +++--- esphome/components/light/effects.py | 4 ++-- esphome/components/light/types.py | 2 ++ tests/test1.yaml | 5 +++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/esphome/components/light/addressable_light_effect.h b/esphome/components/light/addressable_light_effect.h index e357d7c136..9cdc9628c6 100644 --- a/esphome/components/light/addressable_light_effect.h +++ b/esphome/components/light/addressable_light_effect.h @@ -50,19 +50,19 @@ 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 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); + this->f_(it, current_color); } } protected: - std::function f_; + std::function f_; uint32_t update_interval_; uint32_t last_run_{0}; }; diff --git a/esphome/components/light/effects.py b/esphome/components/light/effects.py index eb4e196d4e..c2250e7e0c 100644 --- a/esphome/components/light/effects.py +++ b/esphome/components/light/effects.py @@ -11,7 +11,7 @@ from .types import LambdaLightEffect, RandomLightEffect, StrobeLightEffect, \ FlickerLightEffect, AddressableRainbowLightEffect, AddressableColorWipeEffect, \ AddressableColorWipeEffectColor, AddressableScanEffect, AddressableTwinkleEffect, \ AddressableRandomTwinkleEffect, AddressableFireworksEffect, AddressableFlickerEffect, \ - AutomationLightEffect + AutomationLightEffect, ESPColor CONF_ADD_LED_INTERVAL = 'add_led_interval' CONF_REVERSE = 'reverse' @@ -129,7 +129,7 @@ def flicker_effect_to_code(config, effect_id): cv.Optional(CONF_UPDATE_INTERVAL, default='0ms'): cv.positive_time_period_milliseconds, }) def addressable_lambda_effect_to_code(config, effect_id): - args = [(AddressableLightRef, 'it')] + args = [(AddressableLightRef, 'it'), (ESPColor, 'current_color')] 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/esphome/components/light/types.py b/esphome/components/light/types.py index 185105831d..fb88d021f2 100644 --- a/esphome/components/light/types.py +++ b/esphome/components/light/types.py @@ -9,6 +9,8 @@ AddressableLightState = light_ns.class_('LightState', LightState) LightOutput = light_ns.class_('LightOutput') AddressableLight = light_ns.class_('AddressableLight', cg.Component) AddressableLightRef = AddressableLight.operator('ref') + +ESPColor = light_ns.class_('ESPColor') LightColorValues = light_ns.class_('LightColorValues') # Actions diff --git a/tests/test1.yaml b/tests/test1.yaml index 62773b68c8..5ffd8180fc 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -893,6 +893,11 @@ light: name: Flicker Effect With Custom Values update_interval: 16ms intensity: 5% + - addressable_lambda: + name: "Test For Custom Lambda Effect" + lambda: |- + it[0] = current_color; + - automation: name: Custom Effect sequence: