mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 12:30:28 +02:00
Make light.addressable_set color parameters behave as documented & consistent with elsewhere (#2009)
This commit is contained in:
@@ -135,39 +135,51 @@ class LightTurnOffTrigger : public Trigger<> {
|
||||
}
|
||||
};
|
||||
|
||||
// This is slightly ugly, but we can't log in headers, and can't make this a static method on AddressableSet
|
||||
// due to the template. It's just a temporary warning anyway.
|
||||
void addressableset_warn_about_scale(const char *field);
|
||||
|
||||
template<typename... Ts> class AddressableSet : public Action<Ts...> {
|
||||
public:
|
||||
explicit AddressableSet(LightState *parent) : parent_(parent) {}
|
||||
|
||||
TEMPLATABLE_VALUE(int32_t, range_from)
|
||||
TEMPLATABLE_VALUE(int32_t, range_to)
|
||||
TEMPLATABLE_VALUE(uint8_t, color_brightness)
|
||||
TEMPLATABLE_VALUE(uint8_t, red)
|
||||
TEMPLATABLE_VALUE(uint8_t, green)
|
||||
TEMPLATABLE_VALUE(uint8_t, blue)
|
||||
TEMPLATABLE_VALUE(uint8_t, white)
|
||||
TEMPLATABLE_VALUE(float, color_brightness)
|
||||
TEMPLATABLE_VALUE(float, red)
|
||||
TEMPLATABLE_VALUE(float, green)
|
||||
TEMPLATABLE_VALUE(float, blue)
|
||||
TEMPLATABLE_VALUE(float, white)
|
||||
|
||||
void play(Ts... x) override {
|
||||
auto *out = (AddressableLight *) this->parent_->get_output();
|
||||
int32_t range_from = this->range_from_.value_or(x..., 0);
|
||||
int32_t range_to = this->range_to_.value_or(x..., out->size() - 1) + 1;
|
||||
uint8_t remote_color_brightness =
|
||||
static_cast<uint8_t>(roundf(this->parent_->remote_values.get_color_brightness() * 255.0f));
|
||||
uint8_t color_brightness = this->color_brightness_.value_or(x..., remote_color_brightness);
|
||||
uint8_t color_brightness =
|
||||
to_uint8_scale(this->color_brightness_.value_or(x..., this->parent_->remote_values.get_color_brightness()));
|
||||
auto range = out->range(range_from, range_to);
|
||||
if (this->red_.has_value())
|
||||
range.set_red(esp_scale8(this->red_.value(x...), color_brightness));
|
||||
range.set_red(esp_scale8(to_uint8_compat(this->red_.value(x...), "red"), color_brightness));
|
||||
if (this->green_.has_value())
|
||||
range.set_green(esp_scale8(this->green_.value(x...), color_brightness));
|
||||
range.set_green(esp_scale8(to_uint8_compat(this->green_.value(x...), "green"), color_brightness));
|
||||
if (this->blue_.has_value())
|
||||
range.set_blue(esp_scale8(this->blue_.value(x...), color_brightness));
|
||||
range.set_blue(esp_scale8(to_uint8_compat(this->blue_.value(x...), "blue"), color_brightness));
|
||||
if (this->white_.has_value())
|
||||
range.set_white(this->white_.value(x...));
|
||||
range.set_white(to_uint8_compat(this->white_.value(x...), "white"));
|
||||
out->schedule_show();
|
||||
}
|
||||
|
||||
protected:
|
||||
LightState *parent_;
|
||||
|
||||
// Historically, this action required uint8_t (0-255) for RGBW values from lambdas. Keep compatibility.
|
||||
static inline uint8_t to_uint8_compat(float value, const char *field) {
|
||||
if (value > 1.0f) {
|
||||
addressableset_warn_about_scale(field);
|
||||
return static_cast<uint8_t>(value);
|
||||
}
|
||||
return to_uint8_scale(value);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
||||
Reference in New Issue
Block a user