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 <x>
This commit is contained in:
rnauber 2019-06-19 11:33:14 +02:00 committed by Otto Winter
parent 27453afa4e
commit 5cd7f23065
4 changed files with 12 additions and 5 deletions

View File

@ -50,19 +50,19 @@ class AddressableLightEffect : public LightEffect {
class AddressableLambdaLightEffect : public AddressableLightEffect {
public:
AddressableLambdaLightEffect(const std::string &name, const std::function<void(AddressableLight &)> &f,
AddressableLambdaLightEffect(const std::string &name, const std::function<void(AddressableLight &, ESPColor)> &f,
uint32_t update_interval)
: AddressableLightEffect(name), f_(f), update_interval_(update_interval) {}
void apply(AddressableLight &it, const ESPColor &current_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<void(AddressableLight &)> f_;
std::function<void(AddressableLight &, ESPColor)> f_;
uint32_t update_interval_;
uint32_t last_run_{0};
};

View File

@ -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])

View File

@ -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

View File

@ -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: