mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 14:16:40 +00:00
[light] Reduce flash usage by 832 bytes through code optimization (#9924)
This commit is contained in:
parent
6ed9214465
commit
84ed1bcf34
@ -9,11 +9,28 @@ namespace light {
|
|||||||
|
|
||||||
static const char *const TAG = "light";
|
static const char *const TAG = "light";
|
||||||
|
|
||||||
// Helper function to reduce code size for validation warnings
|
// Helper functions to reduce code size for logging
|
||||||
|
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
|
||||||
static void log_validation_warning(const char *name, const char *param_name, float val, float min, float max) {
|
static void log_validation_warning(const char *name, const char *param_name, float val, float min, float max) {
|
||||||
ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, param_name, val, min, max);
|
ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, param_name, val, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void log_feature_not_supported(const char *name, const char *feature) {
|
||||||
|
ESP_LOGW(TAG, "'%s': %s not supported", name, feature);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_color_mode_not_supported(const char *name, const char *feature) {
|
||||||
|
ESP_LOGW(TAG, "'%s': color mode does not support setting %s", name, feature);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_invalid_parameter(const char *name, const char *message) { ESP_LOGW(TAG, "'%s': %s", name, message); }
|
||||||
|
#else
|
||||||
|
#define log_validation_warning(name, param_name, val, min, max)
|
||||||
|
#define log_feature_not_supported(name, feature)
|
||||||
|
#define log_color_mode_not_supported(name, feature)
|
||||||
|
#define log_invalid_parameter(name, message)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Macro to reduce repetitive setter code
|
// Macro to reduce repetitive setter code
|
||||||
#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
|
#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
|
||||||
LightCall &LightCall::set_##name(optional<type>(name)) { \
|
LightCall &LightCall::set_##name(optional<type>(name)) { \
|
||||||
@ -49,11 +66,21 @@ static const LogString *color_mode_to_human(ColorMode color_mode) {
|
|||||||
return LOG_STR("");
|
return LOG_STR("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper to log percentage values
|
||||||
|
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
|
||||||
|
static void log_percent(const char *name, const char *param, float value) {
|
||||||
|
ESP_LOGD(TAG, " %s: %.0f%%", param, value * 100.0f);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define log_percent(name, param, value)
|
||||||
|
#endif
|
||||||
|
|
||||||
void LightCall::perform() {
|
void LightCall::perform() {
|
||||||
const char *name = this->parent_->get_name().c_str();
|
const char *name = this->parent_->get_name().c_str();
|
||||||
LightColorValues v = this->validate_();
|
LightColorValues v = this->validate_();
|
||||||
|
const bool publish = this->get_publish_();
|
||||||
|
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
ESP_LOGD(TAG, "'%s' Setting:", name);
|
ESP_LOGD(TAG, "'%s' Setting:", name);
|
||||||
|
|
||||||
// Only print color mode when it's being changed
|
// Only print color mode when it's being changed
|
||||||
@ -71,11 +98,11 @@ void LightCall::perform() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_brightness()) {
|
if (this->has_brightness()) {
|
||||||
ESP_LOGD(TAG, " Brightness: %.0f%%", v.get_brightness() * 100.0f);
|
log_percent(name, "Brightness", v.get_brightness());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_color_brightness()) {
|
if (this->has_color_brightness()) {
|
||||||
ESP_LOGD(TAG, " Color brightness: %.0f%%", v.get_color_brightness() * 100.0f);
|
log_percent(name, "Color brightness", v.get_color_brightness());
|
||||||
}
|
}
|
||||||
if (this->has_red() || this->has_green() || this->has_blue()) {
|
if (this->has_red() || this->has_green() || this->has_blue()) {
|
||||||
ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
|
ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
|
||||||
@ -83,7 +110,7 @@ void LightCall::perform() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_white()) {
|
if (this->has_white()) {
|
||||||
ESP_LOGD(TAG, " White: %.0f%%", v.get_white() * 100.0f);
|
log_percent(name, "White", v.get_white());
|
||||||
}
|
}
|
||||||
if (this->has_color_temperature()) {
|
if (this->has_color_temperature()) {
|
||||||
ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
|
ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
|
||||||
@ -97,26 +124,26 @@ void LightCall::perform() {
|
|||||||
|
|
||||||
if (this->has_flash_()) {
|
if (this->has_flash_()) {
|
||||||
// FLASH
|
// FLASH
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
|
ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->parent_->start_flash_(v, this->flash_length_, this->get_publish_());
|
this->parent_->start_flash_(v, this->flash_length_, publish);
|
||||||
} else if (this->has_transition_()) {
|
} else if (this->has_transition_()) {
|
||||||
// TRANSITION
|
// TRANSITION
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
|
ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case: Transition and effect can be set when turning off
|
// Special case: Transition and effect can be set when turning off
|
||||||
if (this->has_effect_()) {
|
if (this->has_effect_()) {
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
ESP_LOGD(TAG, " Effect: 'None'");
|
ESP_LOGD(TAG, " Effect: 'None'");
|
||||||
}
|
}
|
||||||
this->parent_->stop_effect_();
|
this->parent_->stop_effect_();
|
||||||
}
|
}
|
||||||
|
|
||||||
this->parent_->start_transition_(v, this->transition_length_, this->get_publish_());
|
this->parent_->start_transition_(v, this->transition_length_, publish);
|
||||||
|
|
||||||
} else if (this->has_effect_()) {
|
} else if (this->has_effect_()) {
|
||||||
// EFFECT
|
// EFFECT
|
||||||
@ -127,7 +154,7 @@ void LightCall::perform() {
|
|||||||
effect_s = this->parent_->effects_[this->effect_ - 1]->get_name().c_str();
|
effect_s = this->parent_->effects_[this->effect_ - 1]->get_name().c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
ESP_LOGD(TAG, " Effect: '%s'", effect_s);
|
ESP_LOGD(TAG, " Effect: '%s'", effect_s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,13 +165,13 @@ void LightCall::perform() {
|
|||||||
this->parent_->set_immediately_(v, true);
|
this->parent_->set_immediately_(v, true);
|
||||||
} else {
|
} else {
|
||||||
// INSTANT CHANGE
|
// INSTANT CHANGE
|
||||||
this->parent_->set_immediately_(v, this->get_publish_());
|
this->parent_->set_immediately_(v, publish);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->has_transition_()) {
|
if (!this->has_transition_()) {
|
||||||
this->parent_->target_state_reached_callback_.call();
|
this->parent_->target_state_reached_callback_.call();
|
||||||
}
|
}
|
||||||
if (this->get_publish_()) {
|
if (publish) {
|
||||||
this->parent_->publish_state();
|
this->parent_->publish_state();
|
||||||
}
|
}
|
||||||
if (this->get_save_()) {
|
if (this->get_save_()) {
|
||||||
@ -174,19 +201,19 @@ LightColorValues LightCall::validate_() {
|
|||||||
|
|
||||||
// Brightness exists check
|
// Brightness exists check
|
||||||
if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||||
ESP_LOGW(TAG, "'%s': setting brightness not supported", name);
|
log_feature_not_supported(name, "brightness");
|
||||||
this->set_flag_(FLAG_HAS_BRIGHTNESS, false);
|
this->set_flag_(FLAG_HAS_BRIGHTNESS, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transition length possible check
|
// Transition length possible check
|
||||||
if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||||
ESP_LOGW(TAG, "'%s': transitions not supported", name);
|
log_feature_not_supported(name, "transitions");
|
||||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color brightness exists check
|
// Color brightness exists check
|
||||||
if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
|
if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
|
||||||
ESP_LOGW(TAG, "'%s': color mode does not support setting RGB brightness", name);
|
log_color_mode_not_supported(name, "RGB brightness");
|
||||||
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, false);
|
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,7 +221,7 @@ LightColorValues LightCall::validate_() {
|
|||||||
if ((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
|
if ((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
|
||||||
(this->has_blue() && this->blue_ > 0.0f)) {
|
(this->has_blue() && this->blue_ > 0.0f)) {
|
||||||
if (!(color_mode & ColorCapability::RGB)) {
|
if (!(color_mode & ColorCapability::RGB)) {
|
||||||
ESP_LOGW(TAG, "'%s': color mode does not support setting RGB color", name);
|
log_color_mode_not_supported(name, "RGB color");
|
||||||
this->set_flag_(FLAG_HAS_RED, false);
|
this->set_flag_(FLAG_HAS_RED, false);
|
||||||
this->set_flag_(FLAG_HAS_GREEN, false);
|
this->set_flag_(FLAG_HAS_GREEN, false);
|
||||||
this->set_flag_(FLAG_HAS_BLUE, false);
|
this->set_flag_(FLAG_HAS_BLUE, false);
|
||||||
@ -204,21 +231,21 @@ LightColorValues LightCall::validate_() {
|
|||||||
// White value exists check
|
// White value exists check
|
||||||
if (this->has_white() && this->white_ > 0.0f &&
|
if (this->has_white() && this->white_ > 0.0f &&
|
||||||
!(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
!(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||||
ESP_LOGW(TAG, "'%s': color mode does not support setting white value", name);
|
log_color_mode_not_supported(name, "white value");
|
||||||
this->set_flag_(FLAG_HAS_WHITE, false);
|
this->set_flag_(FLAG_HAS_WHITE, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color temperature exists check
|
// Color temperature exists check
|
||||||
if (this->has_color_temperature() &&
|
if (this->has_color_temperature() &&
|
||||||
!(color_mode & ColorCapability::COLOR_TEMPERATURE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
!(color_mode & ColorCapability::COLOR_TEMPERATURE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||||
ESP_LOGW(TAG, "'%s': color mode does not support setting color temperature", name);
|
log_color_mode_not_supported(name, "color temperature");
|
||||||
this->set_flag_(FLAG_HAS_COLOR_TEMPERATURE, false);
|
this->set_flag_(FLAG_HAS_COLOR_TEMPERATURE, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cold/warm white value exists check
|
// Cold/warm white value exists check
|
||||||
if ((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) {
|
if ((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) {
|
||||||
if (!(color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
if (!(color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||||
ESP_LOGW(TAG, "'%s': color mode does not support setting cold/warm white value", name);
|
log_color_mode_not_supported(name, "cold/warm white value");
|
||||||
this->set_flag_(FLAG_HAS_COLD_WHITE, false);
|
this->set_flag_(FLAG_HAS_COLD_WHITE, false);
|
||||||
this->set_flag_(FLAG_HAS_WARM_WHITE, false);
|
this->set_flag_(FLAG_HAS_WARM_WHITE, false);
|
||||||
}
|
}
|
||||||
@ -292,7 +319,7 @@ LightColorValues LightCall::validate_() {
|
|||||||
|
|
||||||
// Flash length check
|
// Flash length check
|
||||||
if (this->has_flash_() && this->flash_length_ == 0) {
|
if (this->has_flash_() && this->flash_length_ == 0) {
|
||||||
ESP_LOGW(TAG, "'%s': flash length must be greater than zero", name);
|
log_invalid_parameter(name, "flash length must be greater than zero");
|
||||||
this->set_flag_(FLAG_HAS_FLASH, false);
|
this->set_flag_(FLAG_HAS_FLASH, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,13 +338,13 @@ LightColorValues LightCall::validate_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
|
if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
|
||||||
ESP_LOGW(TAG, "'%s': effect cannot be used with transition/flash", name);
|
log_invalid_parameter(name, "effect cannot be used with transition/flash");
|
||||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||||
this->set_flag_(FLAG_HAS_FLASH, false);
|
this->set_flag_(FLAG_HAS_FLASH, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_flash_() && this->has_transition_()) {
|
if (this->has_flash_() && this->has_transition_()) {
|
||||||
ESP_LOGW(TAG, "'%s': flash cannot be used with transition", name);
|
log_invalid_parameter(name, "flash cannot be used with transition");
|
||||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -334,7 +361,7 @@ LightColorValues LightCall::validate_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->has_transition_() && !supports_transition) {
|
if (this->has_transition_() && !supports_transition) {
|
||||||
ESP_LOGW(TAG, "'%s': transitions not supported", name);
|
log_feature_not_supported(name, "transitions");
|
||||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,7 +371,7 @@ LightColorValues LightCall::validate_() {
|
|||||||
bool target_state = this->has_state() ? this->state_ : v.is_on();
|
bool target_state = this->has_state() ? this->state_ : v.is_on();
|
||||||
if (!this->has_flash_() && !target_state) {
|
if (!this->has_flash_() && !target_state) {
|
||||||
if (this->has_effect_()) {
|
if (this->has_effect_()) {
|
||||||
ESP_LOGW(TAG, "'%s': cannot start effect when turning off", name);
|
log_invalid_parameter(name, "cannot start effect when turning off");
|
||||||
this->set_flag_(FLAG_HAS_EFFECT, false);
|
this->set_flag_(FLAG_HAS_EFFECT, false);
|
||||||
} else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
|
} else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
|
||||||
// Auto turn off effect
|
// Auto turn off effect
|
||||||
@ -368,21 +395,27 @@ void LightCall::transform_parameters_() {
|
|||||||
// - RGBWW lights with color_interlock=true, which also sets "brightness" and
|
// - RGBWW lights with color_interlock=true, which also sets "brightness" and
|
||||||
// "color_temperature" (without color_interlock, CW/WW are set directly)
|
// "color_temperature" (without color_interlock, CW/WW are set directly)
|
||||||
// - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
|
// - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
|
||||||
|
|
||||||
|
// Cache min/max mireds to avoid repeated calls
|
||||||
|
const float min_mireds = traits.get_min_mireds();
|
||||||
|
const float max_mireds = traits.get_max_mireds();
|
||||||
|
|
||||||
if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
|
if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
|
||||||
(this->color_mode_ & ColorCapability::COLD_WARM_WHITE) && //
|
(this->color_mode_ & ColorCapability::COLD_WARM_WHITE) && //
|
||||||
!(this->color_mode_ & ColorCapability::WHITE) && //
|
!(this->color_mode_ & ColorCapability::WHITE) && //
|
||||||
!(this->color_mode_ & ColorCapability::COLOR_TEMPERATURE) && //
|
!(this->color_mode_ & ColorCapability::COLOR_TEMPERATURE) && //
|
||||||
traits.get_min_mireds() > 0.0f && traits.get_max_mireds() > 0.0f) {
|
min_mireds > 0.0f && max_mireds > 0.0f) {
|
||||||
ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
|
ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
|
||||||
this->parent_->get_name().c_str());
|
this->parent_->get_name().c_str());
|
||||||
if (this->has_color_temperature()) {
|
if (this->has_color_temperature()) {
|
||||||
const float color_temp = clamp(this->color_temperature_, traits.get_min_mireds(), traits.get_max_mireds());
|
const float color_temp = clamp(this->color_temperature_, min_mireds, max_mireds);
|
||||||
const float ww_fraction =
|
const float range = max_mireds - min_mireds;
|
||||||
(color_temp - traits.get_min_mireds()) / (traits.get_max_mireds() - traits.get_min_mireds());
|
const float ww_fraction = (color_temp - min_mireds) / range;
|
||||||
const float cw_fraction = 1.0f - ww_fraction;
|
const float cw_fraction = 1.0f - ww_fraction;
|
||||||
const float max_cw_ww = std::max(ww_fraction, cw_fraction);
|
const float max_cw_ww = std::max(ww_fraction, cw_fraction);
|
||||||
this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, this->parent_->get_gamma_correct());
|
const float gamma = this->parent_->get_gamma_correct();
|
||||||
this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, this->parent_->get_gamma_correct());
|
this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, gamma);
|
||||||
|
this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, gamma);
|
||||||
this->set_flag_(FLAG_HAS_COLD_WHITE, true);
|
this->set_flag_(FLAG_HAS_COLD_WHITE, true);
|
||||||
this->set_flag_(FLAG_HAS_WARM_WHITE, true);
|
this->set_flag_(FLAG_HAS_WARM_WHITE, true);
|
||||||
}
|
}
|
||||||
|
@ -84,18 +84,23 @@ class LightColorValues {
|
|||||||
* @return The linearly interpolated LightColorValues.
|
* @return The linearly interpolated LightColorValues.
|
||||||
*/
|
*/
|
||||||
static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion) {
|
static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion) {
|
||||||
|
// Directly interpolate the raw values to avoid getter/setter overhead.
|
||||||
|
// This is safe because:
|
||||||
|
// - All LightColorValues have their values clamped when set via the setters
|
||||||
|
// - std::lerp guarantees output is in the same range as inputs
|
||||||
|
// - Therefore the output doesn't need clamping, so we can skip the setters
|
||||||
LightColorValues v;
|
LightColorValues v;
|
||||||
v.set_color_mode(end.color_mode_);
|
v.color_mode_ = end.color_mode_;
|
||||||
v.set_state(std::lerp(start.get_state(), end.get_state(), completion));
|
v.state_ = std::lerp(start.state_, end.state_, completion);
|
||||||
v.set_brightness(std::lerp(start.get_brightness(), end.get_brightness(), completion));
|
v.brightness_ = std::lerp(start.brightness_, end.brightness_, completion);
|
||||||
v.set_color_brightness(std::lerp(start.get_color_brightness(), end.get_color_brightness(), completion));
|
v.color_brightness_ = std::lerp(start.color_brightness_, end.color_brightness_, completion);
|
||||||
v.set_red(std::lerp(start.get_red(), end.get_red(), completion));
|
v.red_ = std::lerp(start.red_, end.red_, completion);
|
||||||
v.set_green(std::lerp(start.get_green(), end.get_green(), completion));
|
v.green_ = std::lerp(start.green_, end.green_, completion);
|
||||||
v.set_blue(std::lerp(start.get_blue(), end.get_blue(), completion));
|
v.blue_ = std::lerp(start.blue_, end.blue_, completion);
|
||||||
v.set_white(std::lerp(start.get_white(), end.get_white(), completion));
|
v.white_ = std::lerp(start.white_, end.white_, completion);
|
||||||
v.set_color_temperature(std::lerp(start.get_color_temperature(), end.get_color_temperature(), completion));
|
v.color_temperature_ = std::lerp(start.color_temperature_, end.color_temperature_, completion);
|
||||||
v.set_cold_white(std::lerp(start.get_cold_white(), end.get_cold_white(), completion));
|
v.cold_white_ = std::lerp(start.cold_white_, end.cold_white_, completion);
|
||||||
v.set_warm_white(std::lerp(start.get_warm_white(), end.get_warm_white(), completion));
|
v.warm_white_ = std::lerp(start.warm_white_, end.warm_white_, completion);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,32 @@ namespace light {
|
|||||||
|
|
||||||
// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
|
// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
|
||||||
|
|
||||||
|
// Lookup table for color mode strings
|
||||||
|
static constexpr const char *get_color_mode_json_str(ColorMode mode) {
|
||||||
|
switch (mode) {
|
||||||
|
case ColorMode::ON_OFF:
|
||||||
|
return "onoff";
|
||||||
|
case ColorMode::BRIGHTNESS:
|
||||||
|
return "brightness";
|
||||||
|
case ColorMode::WHITE:
|
||||||
|
return "white"; // not supported by HA in MQTT
|
||||||
|
case ColorMode::COLOR_TEMPERATURE:
|
||||||
|
return "color_temp";
|
||||||
|
case ColorMode::COLD_WARM_WHITE:
|
||||||
|
return "cwww"; // not supported by HA
|
||||||
|
case ColorMode::RGB:
|
||||||
|
return "rgb";
|
||||||
|
case ColorMode::RGB_WHITE:
|
||||||
|
return "rgbw";
|
||||||
|
case ColorMode::RGB_COLOR_TEMPERATURE:
|
||||||
|
return "rgbct"; // not supported by HA
|
||||||
|
case ColorMode::RGB_COLD_WARM_WHITE:
|
||||||
|
return "rgbww";
|
||||||
|
default:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
|
void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
|
||||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||||
if (state.supports_effects())
|
if (state.supports_effects())
|
||||||
@ -16,60 +42,36 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
|
|||||||
auto values = state.remote_values;
|
auto values = state.remote_values;
|
||||||
auto traits = state.get_output()->get_traits();
|
auto traits = state.get_output()->get_traits();
|
||||||
|
|
||||||
switch (values.get_color_mode()) {
|
const auto color_mode = values.get_color_mode();
|
||||||
case ColorMode::UNKNOWN: // don't need to set color mode if we don't know it
|
const char *mode_str = get_color_mode_json_str(color_mode);
|
||||||
break;
|
if (mode_str != nullptr) {
|
||||||
case ColorMode::ON_OFF:
|
root["color_mode"] = mode_str;
|
||||||
root["color_mode"] = "onoff";
|
|
||||||
break;
|
|
||||||
case ColorMode::BRIGHTNESS:
|
|
||||||
root["color_mode"] = "brightness";
|
|
||||||
break;
|
|
||||||
case ColorMode::WHITE: // not supported by HA in MQTT
|
|
||||||
root["color_mode"] = "white";
|
|
||||||
break;
|
|
||||||
case ColorMode::COLOR_TEMPERATURE:
|
|
||||||
root["color_mode"] = "color_temp";
|
|
||||||
break;
|
|
||||||
case ColorMode::COLD_WARM_WHITE: // not supported by HA
|
|
||||||
root["color_mode"] = "cwww";
|
|
||||||
break;
|
|
||||||
case ColorMode::RGB:
|
|
||||||
root["color_mode"] = "rgb";
|
|
||||||
break;
|
|
||||||
case ColorMode::RGB_WHITE:
|
|
||||||
root["color_mode"] = "rgbw";
|
|
||||||
break;
|
|
||||||
case ColorMode::RGB_COLOR_TEMPERATURE: // not supported by HA
|
|
||||||
root["color_mode"] = "rgbct";
|
|
||||||
break;
|
|
||||||
case ColorMode::RGB_COLD_WARM_WHITE:
|
|
||||||
root["color_mode"] = "rgbww";
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (values.get_color_mode() & ColorCapability::ON_OFF)
|
if (color_mode & ColorCapability::ON_OFF)
|
||||||
root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF";
|
root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF";
|
||||||
if (values.get_color_mode() & ColorCapability::BRIGHTNESS)
|
if (color_mode & ColorCapability::BRIGHTNESS)
|
||||||
root["brightness"] = uint8_t(values.get_brightness() * 255);
|
root["brightness"] = to_uint8_scale(values.get_brightness());
|
||||||
|
|
||||||
JsonObject color = root["color"].to<JsonObject>();
|
JsonObject color = root["color"].to<JsonObject>();
|
||||||
if (values.get_color_mode() & ColorCapability::RGB) {
|
if (color_mode & ColorCapability::RGB) {
|
||||||
color["r"] = uint8_t(values.get_color_brightness() * values.get_red() * 255);
|
float color_brightness = values.get_color_brightness();
|
||||||
color["g"] = uint8_t(values.get_color_brightness() * values.get_green() * 255);
|
color["r"] = to_uint8_scale(color_brightness * values.get_red());
|
||||||
color["b"] = uint8_t(values.get_color_brightness() * values.get_blue() * 255);
|
color["g"] = to_uint8_scale(color_brightness * values.get_green());
|
||||||
|
color["b"] = to_uint8_scale(color_brightness * values.get_blue());
|
||||||
}
|
}
|
||||||
if (values.get_color_mode() & ColorCapability::WHITE) {
|
if (color_mode & ColorCapability::WHITE) {
|
||||||
color["w"] = uint8_t(values.get_white() * 255);
|
uint8_t white_val = to_uint8_scale(values.get_white());
|
||||||
root["white_value"] = uint8_t(values.get_white() * 255); // legacy API
|
color["w"] = white_val;
|
||||||
|
root["white_value"] = white_val; // legacy API
|
||||||
}
|
}
|
||||||
if (values.get_color_mode() & ColorCapability::COLOR_TEMPERATURE) {
|
if (color_mode & ColorCapability::COLOR_TEMPERATURE) {
|
||||||
// this one isn't under the color subkey for some reason
|
// this one isn't under the color subkey for some reason
|
||||||
root["color_temp"] = uint32_t(values.get_color_temperature());
|
root["color_temp"] = uint32_t(values.get_color_temperature());
|
||||||
}
|
}
|
||||||
if (values.get_color_mode() & ColorCapability::COLD_WARM_WHITE) {
|
if (color_mode & ColorCapability::COLD_WARM_WHITE) {
|
||||||
color["c"] = uint8_t(values.get_cold_white() * 255);
|
color["c"] = to_uint8_scale(values.get_cold_white());
|
||||||
color["w"] = uint8_t(values.get_warm_white() * 255);
|
color["w"] = to_uint8_scale(values.get_warm_white());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,8 @@ void LightState::setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// When supported color temperature range is known, initialize color temperature setting within bounds.
|
// When supported color temperature range is known, initialize color temperature setting within bounds.
|
||||||
float min_mireds = this->get_traits().get_min_mireds();
|
auto traits = this->get_traits();
|
||||||
|
float min_mireds = traits.get_min_mireds();
|
||||||
if (min_mireds > 0) {
|
if (min_mireds > 0) {
|
||||||
this->remote_values.set_color_temperature(min_mireds);
|
this->remote_values.set_color_temperature(min_mireds);
|
||||||
this->current_values.set_color_temperature(min_mireds);
|
this->current_values.set_color_temperature(min_mireds);
|
||||||
@ -43,11 +44,8 @@ void LightState::setup() {
|
|||||||
this->rtc_ = global_preferences->make_preference<LightStateRTCState>(this->get_object_id_hash());
|
this->rtc_ = global_preferences->make_preference<LightStateRTCState>(this->get_object_id_hash());
|
||||||
// Attempt to load from preferences, else fall back to default values
|
// Attempt to load from preferences, else fall back to default values
|
||||||
if (!this->rtc_.load(&recovered)) {
|
if (!this->rtc_.load(&recovered)) {
|
||||||
recovered.state = false;
|
recovered.state = (this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON ||
|
||||||
if (this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON ||
|
this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON);
|
||||||
this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON) {
|
|
||||||
recovered.state = true;
|
|
||||||
}
|
|
||||||
} else if (this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_OFF ||
|
} else if (this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_OFF ||
|
||||||
this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON) {
|
this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON) {
|
||||||
// Inverted restore state
|
// Inverted restore state
|
||||||
@ -88,17 +86,18 @@ void LightState::setup() {
|
|||||||
}
|
}
|
||||||
void LightState::dump_config() {
|
void LightState::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str());
|
||||||
if (this->get_traits().supports_color_capability(ColorCapability::BRIGHTNESS)) {
|
auto traits = this->get_traits();
|
||||||
|
if (traits.supports_color_capability(ColorCapability::BRIGHTNESS)) {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
" Default Transition Length: %.1fs\n"
|
" Default Transition Length: %.1fs\n"
|
||||||
" Gamma Correct: %.2f",
|
" Gamma Correct: %.2f",
|
||||||
this->default_transition_length_ / 1e3f, this->gamma_correct_);
|
this->default_transition_length_ / 1e3f, this->gamma_correct_);
|
||||||
}
|
}
|
||||||
if (this->get_traits().supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) {
|
if (traits.supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
" Min Mireds: %.1f\n"
|
" Min Mireds: %.1f\n"
|
||||||
" Max Mireds: %.1f",
|
" Max Mireds: %.1f",
|
||||||
this->get_traits().get_min_mireds(), this->get_traits().get_max_mireds());
|
traits.get_min_mireds(), traits.get_max_mireds());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void LightState::loop() {
|
void LightState::loop() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user