Allow pulse light effect to have separate on and off transition lengths (#5659)

This commit is contained in:
Jesse Hills 2023-11-07 12:39:47 +13:00 committed by GitHub
parent d141e1cd67
commit dd0270207f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 8 deletions

View file

@ -3,8 +3,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "light_effect.h"
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "light_effect.h"
namespace esphome { namespace esphome {
namespace light { namespace light {
@ -27,8 +27,8 @@ class PulseLightEffect : public LightEffect {
auto call = this->state_->turn_on(); auto call = this->state_->turn_on();
float out = this->on_ ? this->max_brightness : this->min_brightness; float out = this->on_ ? this->max_brightness : this->min_brightness;
call.set_brightness_if_supported(out); call.set_brightness_if_supported(out);
call.set_transition_length_if_supported(this->on_ ? this->transition_on_length_ : this->transition_off_length_);
this->on_ = !this->on_; this->on_ = !this->on_;
call.set_transition_length_if_supported(this->transition_length_);
// don't tell HA every change // don't tell HA every change
call.set_publish(false); call.set_publish(false);
call.set_save(false); call.set_save(false);
@ -37,7 +37,8 @@ class PulseLightEffect : public LightEffect {
this->last_color_change_ = now; this->last_color_change_ = now;
} }
void set_transition_length(uint32_t transition_length) { this->transition_length_ = transition_length; } void set_transition_on_length(uint32_t transition_length) { this->transition_on_length_ = transition_length; }
void set_transition_off_length(uint32_t transition_length) { this->transition_off_length_ = transition_length; }
void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; } void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
@ -49,7 +50,8 @@ class PulseLightEffect : public LightEffect {
protected: protected:
bool on_ = false; bool on_ = false;
uint32_t last_color_change_{0}; uint32_t last_color_change_{0};
uint32_t transition_length_{}; uint32_t transition_on_length_{};
uint32_t transition_off_length_{};
uint32_t update_interval_{}; uint32_t update_interval_{};
float min_brightness{0.0}; float min_brightness{0.0};
float max_brightness{1.0}; float max_brightness{1.0};

View file

@ -76,6 +76,8 @@ CONF_ADDRESSABLE_RANDOM_TWINKLE = "addressable_random_twinkle"
CONF_ADDRESSABLE_FIREWORKS = "addressable_fireworks" CONF_ADDRESSABLE_FIREWORKS = "addressable_fireworks"
CONF_ADDRESSABLE_FLICKER = "addressable_flicker" CONF_ADDRESSABLE_FLICKER = "addressable_flicker"
CONF_AUTOMATION = "automation" CONF_AUTOMATION = "automation"
CONF_ON_LENGTH = "on_length"
CONF_OFF_LENGTH = "off_length"
BINARY_EFFECTS = [] BINARY_EFFECTS = []
MONOCHROMATIC_EFFECTS = [] MONOCHROMATIC_EFFECTS = []
@ -170,9 +172,15 @@ async def automation_effect_to_code(config, effect_id):
PulseLightEffect, PulseLightEffect,
"Pulse", "Pulse",
{ {
cv.Optional( cv.Optional(CONF_TRANSITION_LENGTH, default="1s"): cv.Any(
CONF_TRANSITION_LENGTH, default="1s" cv.positive_time_period_milliseconds,
): cv.positive_time_period_milliseconds, cv.Schema(
{
cv.Required(CONF_ON_LENGTH): cv.positive_time_period_milliseconds,
cv.Required(CONF_OFF_LENGTH): cv.positive_time_period_milliseconds,
}
),
),
cv.Optional( cv.Optional(
CONF_UPDATE_INTERVAL, default="1s" CONF_UPDATE_INTERVAL, default="1s"
): cv.positive_time_period_milliseconds, ): cv.positive_time_period_milliseconds,
@ -182,7 +190,21 @@ async def automation_effect_to_code(config, effect_id):
) )
async def pulse_effect_to_code(config, effect_id): async def pulse_effect_to_code(config, effect_id):
effect = cg.new_Pvariable(effect_id, config[CONF_NAME]) effect = cg.new_Pvariable(effect_id, config[CONF_NAME])
cg.add(effect.set_transition_length(config[CONF_TRANSITION_LENGTH])) if isinstance(config[CONF_TRANSITION_LENGTH], dict):
cg.add(
effect.set_transition_on_length(
config[CONF_TRANSITION_LENGTH][CONF_ON_LENGTH]
)
)
cg.add(
effect.set_transition_off_length(
config[CONF_TRANSITION_LENGTH][CONF_OFF_LENGTH]
)
)
else:
transition_length = config[CONF_TRANSITION_LENGTH]
cg.add(effect.set_transition_on_length(transition_length))
cg.add(effect.set_transition_off_length(transition_length))
cg.add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL])) cg.add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL]))
cg.add( cg.add(
effect.set_min_max_brightness( effect.set_min_max_brightness(

View file

@ -2179,6 +2179,20 @@ light:
state += 1; state += 1;
if (state == 4) if (state == 4)
state = 0; state = 0;
- pulse:
transition_length: 10s
update_interval: 20s
min_brightness: 10%
max_brightness: 90%
- pulse:
name: pulse2
transition_length:
on_length: 10s
off_length: 5s
update_interval: 15s
min_brightness: 10%
max_brightness: 90%
- platform: rgb - platform: rgb
name: Living Room Lights name: Living Room Lights
id: ${roomname}_lights id: ${roomname}_lights