mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 13:45:15 +00:00
[light] Pass light reference into lambda light effect (#16815)
This commit is contained in:
@@ -111,7 +111,7 @@ class RandomLightEffect : public LightEffect {
|
|||||||
|
|
||||||
class LambdaLightEffect : public LightEffect {
|
class LambdaLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
LambdaLightEffect(const char *name, void (*f)(bool initial_run), uint32_t update_interval)
|
LambdaLightEffect(const char *name, void (*f)(LightState &, bool initial_run), uint32_t update_interval)
|
||||||
: LightEffect(name), f_(f), update_interval_(update_interval) {}
|
: LightEffect(name), f_(f), update_interval_(update_interval) {}
|
||||||
|
|
||||||
void start() override { this->initial_run_ = true; }
|
void start() override { this->initial_run_ = true; }
|
||||||
@@ -119,7 +119,7 @@ class LambdaLightEffect : public LightEffect {
|
|||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
|
if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
|
||||||
this->last_run_ = now;
|
this->last_run_ = now;
|
||||||
this->f_(this->initial_run_);
|
this->f_(*this->state_, this->initial_run_);
|
||||||
this->initial_run_ = false;
|
this->initial_run_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,7 +129,7 @@ class LambdaLightEffect : public LightEffect {
|
|||||||
uint32_t get_current_index() const { return this->get_index(); }
|
uint32_t get_current_index() const { return this->get_index(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void (*f_)(bool initial_run);
|
void (*f_)(LightState &, bool initial_run);
|
||||||
uint32_t update_interval_;
|
uint32_t update_interval_;
|
||||||
uint32_t last_run_{0};
|
uint32_t last_run_{0};
|
||||||
bool initial_run_;
|
bool initial_run_;
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ from .types import (
|
|||||||
FlickerLightEffect,
|
FlickerLightEffect,
|
||||||
LambdaLightEffect,
|
LambdaLightEffect,
|
||||||
LightColorValues,
|
LightColorValues,
|
||||||
|
LightStateRef,
|
||||||
PulseLightEffect,
|
PulseLightEffect,
|
||||||
RandomLightEffect,
|
RandomLightEffect,
|
||||||
StrobeLightEffect,
|
StrobeLightEffect,
|
||||||
@@ -175,7 +176,9 @@ def register_addressable_effect(
|
|||||||
)
|
)
|
||||||
async def lambda_effect_to_code(config, effect_id):
|
async def lambda_effect_to_code(config, effect_id):
|
||||||
lambda_ = await cg.process_lambda(
|
lambda_ = await cg.process_lambda(
|
||||||
config[CONF_LAMBDA], [(bool, "initial_run")], return_type=cg.void
|
config[CONF_LAMBDA],
|
||||||
|
[(LightStateRef, "it"), (bool, "initial_run")],
|
||||||
|
return_type=cg.void,
|
||||||
)
|
)
|
||||||
return cg.new_Pvariable(
|
return cg.new_Pvariable(
|
||||||
effect_id, config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]
|
effect_id, config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import esphome.codegen as cg
|
|||||||
# Base
|
# Base
|
||||||
light_ns = cg.esphome_ns.namespace("light")
|
light_ns = cg.esphome_ns.namespace("light")
|
||||||
LightState = light_ns.class_("LightState", cg.EntityBase, cg.Component)
|
LightState = light_ns.class_("LightState", cg.EntityBase, cg.Component)
|
||||||
|
LightStateRef = LightState.operator("ref")
|
||||||
AddressableLightState = light_ns.class_("AddressableLightState", LightState)
|
AddressableLightState = light_ns.class_("AddressableLightState", LightState)
|
||||||
LightOutput = light_ns.class_("LightOutput")
|
LightOutput = light_ns.class_("LightOutput")
|
||||||
AddressableLight = light_ns.class_("AddressableLight", LightOutput, cg.Component)
|
AddressableLight = light_ns.class_("AddressableLight", LightOutput, cg.Component)
|
||||||
|
|||||||
@@ -182,6 +182,9 @@ light:
|
|||||||
state += 1;
|
state += 1;
|
||||||
if (state == 4)
|
if (state == 4)
|
||||||
state = 0;
|
state = 0;
|
||||||
|
if (initial_run) {
|
||||||
|
ESP_LOGD("custom_effect", "Effect %s started", it.get_name().c_str());
|
||||||
|
}
|
||||||
- pulse:
|
- pulse:
|
||||||
transition_length: 10s
|
transition_length: 10s
|
||||||
update_interval: 20s
|
update_interval: 20s
|
||||||
|
|||||||
Reference in New Issue
Block a user