mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 21:44:55 +01:00
Add initial_run to regular lambda light effect (#3059)
This commit is contained in:
parent
ea11462e1e
commit
1c51cac5ba
2 changed files with 9 additions and 4 deletions
|
@ -102,21 +102,24 @@ class RandomLightEffect : public LightEffect {
|
||||||
|
|
||||||
class LambdaLightEffect : public LightEffect {
|
class LambdaLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
LambdaLightEffect(const std::string &name, std::function<void()> f, uint32_t update_interval)
|
LambdaLightEffect(const std::string &name, std::function<void(bool initial_run)> f, uint32_t update_interval)
|
||||||
: LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
: LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||||
|
|
||||||
|
void start() override { this->initial_run_ = true; }
|
||||||
void apply() override {
|
void apply() override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - this->last_run_ >= this->update_interval_) {
|
if (now - this->last_run_ >= this->update_interval_) {
|
||||||
this->last_run_ = now;
|
this->last_run_ = now;
|
||||||
this->f_();
|
this->f_(this->initial_run_);
|
||||||
|
this->initial_run_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::function<void()> f_;
|
std::function<void(bool initial_run)> f_;
|
||||||
uint32_t update_interval_;
|
uint32_t update_interval_;
|
||||||
uint32_t last_run_{0};
|
uint32_t last_run_{0};
|
||||||
|
bool initial_run_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class AutomationLightEffect : public LightEffect {
|
class AutomationLightEffect : public LightEffect {
|
||||||
|
|
|
@ -141,7 +141,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(config[CONF_LAMBDA], [], return_type=cg.void)
|
lambda_ = await cg.process_lambda(
|
||||||
|
config[CONF_LAMBDA], [(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]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue