Add initial_run to regular lambda light effect (#3059)

This commit is contained in:
Jesse Hills 2022-01-21 11:09:07 +13:00 committed by GitHub
parent ea11462e1e
commit 1c51cac5ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View file

@ -102,21 +102,24 @@ class RandomLightEffect : public LightEffect {
class LambdaLightEffect : public LightEffect {
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) {}
void start() override { this->initial_run_ = true; }
void apply() override {
const uint32_t now = millis();
if (now - this->last_run_ >= this->update_interval_) {
this->last_run_ = now;
this->f_();
this->f_(this->initial_run_);
this->initial_run_ = false;
}
}
protected:
std::function<void()> f_;
std::function<void(bool initial_run)> f_;
uint32_t update_interval_;
uint32_t last_run_{0};
bool initial_run_;
};
class AutomationLightEffect : public LightEffect {

View file

@ -141,7 +141,9 @@ def register_addressable_effect(
},
)
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(
effect_id, config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]
)