Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston 2025-07-26 21:55:34 -10:00
commit 9cf0535d39
No known key found for this signature in database
2 changed files with 45 additions and 47 deletions

View File

@ -68,7 +68,7 @@ static const LogString *color_mode_to_human(ColorMode color_mode) {
// Helper to log percentage values
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
static inline void log_percent(const char *name, const char *param, float value) {
static void log_percent(const char *name, const char *param, float value) {
ESP_LOGD(TAG, " %s: %.0f%%", param, value * 100.0f);
}
#else
@ -184,26 +184,26 @@ LightColorValues LightCall::validate_() {
auto traits = this->parent_->get_traits();
// Cache frequently used flags
const bool has_color_mode = this->has_color_mode();
const bool has_state = this->has_state();
const bool has_brightness = this->has_brightness();
const bool has_color_brightness = this->has_color_brightness();
const bool has_red = this->has_red();
const bool has_green = this->has_green();
const bool has_blue = this->has_blue();
const bool has_white = this->has_white();
const bool has_color_temperature = this->has_color_temperature();
const bool has_cold_white = this->has_cold_white();
const bool has_warm_white = this->has_warm_white();
const bool has_color_mode_flag = this->has_color_mode();
const bool has_state_flag = this->has_state();
const bool has_brightness_flag = this->has_brightness();
const bool has_color_brightness_flag = this->has_color_brightness();
const bool has_red_flag = this->has_red();
const bool has_green_flag = this->has_green();
const bool has_blue_flag = this->has_blue();
const bool has_white_flag = this->has_white();
const bool has_color_temperature_flag = this->has_color_temperature();
const bool has_cold_white_flag = this->has_cold_white();
const bool has_warm_white_flag = this->has_warm_white();
// Color mode check
if (has_color_mode && !traits.supports_color_mode(this->color_mode_)) {
if (has_color_mode_flag && !traits.supports_color_mode(this->color_mode_)) {
ESP_LOGW(TAG, "'%s' does not support color mode %s", name, LOG_STR_ARG(color_mode_to_human(this->color_mode_)));
this->set_flag_(FLAG_HAS_COLOR_MODE, false);
}
// Ensure there is always a color mode set
if (!has_color_mode) {
if (!has_color_mode_flag) {
this->color_mode_ = this->compute_color_mode_();
this->set_flag_(FLAG_HAS_COLOR_MODE, true);
}
@ -213,7 +213,7 @@ LightColorValues LightCall::validate_() {
this->transform_parameters_();
// Brightness exists check
if (has_brightness && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
if (has_brightness_flag && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
log_feature_not_supported(name, "brightness");
this->set_flag_(FLAG_HAS_BRIGHTNESS, false);
}
@ -225,13 +225,14 @@ LightColorValues LightCall::validate_() {
}
// Color brightness exists check
if (has_color_brightness && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
if (has_color_brightness_flag && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
log_color_mode_not_supported(name, "RGB brightness");
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, false);
}
// RGB exists check
if ((has_red && this->red_ > 0.0f) || (has_green && this->green_ > 0.0f) || (has_blue && this->blue_ > 0.0f)) {
if ((has_red_flag && this->red_ > 0.0f) || (has_green_flag && this->green_ > 0.0f) ||
(has_blue_flag && this->blue_ > 0.0f)) {
if (!(color_mode & ColorCapability::RGB)) {
log_color_mode_not_supported(name, "RGB color");
this->set_flag_(FLAG_HAS_RED, false);
@ -241,21 +242,21 @@ LightColorValues LightCall::validate_() {
}
// White value exists check
if (has_white && this->white_ > 0.0f &&
if (has_white_flag && this->white_ > 0.0f &&
!(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
log_color_mode_not_supported(name, "white value");
this->set_flag_(FLAG_HAS_WHITE, false);
}
// Color temperature exists check
if (has_color_temperature &&
if (has_color_temperature_flag &&
!(color_mode & ColorCapability::COLOR_TEMPERATURE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
log_color_mode_not_supported(name, "color temperature");
this->set_flag_(FLAG_HAS_COLOR_TEMPERATURE, false);
}
// Cold/warm white value exists check
if ((has_cold_white && this->cold_white_ > 0.0f) || (has_warm_white && this->warm_white_ > 0.0f)) {
if ((has_cold_white_flag && this->cold_white_ > 0.0f) || (has_warm_white_flag && this->warm_white_ > 0.0f)) {
if (!(color_mode & ColorCapability::COLD_WARM_WHITE)) {
log_color_mode_not_supported(name, "cold/warm white value");
this->set_flag_(FLAG_HAS_COLD_WHITE, false);
@ -285,18 +286,18 @@ LightColorValues LightCall::validate_() {
VALIDATE_RANGE_(color_temperature, "Color temperature", traits.get_min_mireds(), traits.get_max_mireds())
// Flag whether an explicit turn off was requested, in which case we'll also stop the effect.
bool explicit_turn_off_request = has_state && !this->state_;
bool explicit_turn_off_request = has_state_flag && !this->state_;
// Turn off when brightness is set to zero, and reset brightness (so that it has nonzero brightness when turned on).
if (has_brightness && this->brightness_ == 0.0f) {
if (has_brightness_flag && this->brightness_ == 0.0f) {
this->state_ = false;
this->set_flag_(FLAG_HAS_STATE, true);
this->brightness_ = 1.0f;
}
// Set color brightness to 100% if currently zero and a color is set.
if (has_red || has_green || has_blue) {
if (!has_color_brightness && this->parent_->remote_values.get_color_brightness() == 0.0f) {
if (has_red_flag || has_green_flag || has_blue_flag) {
if (!has_color_brightness_flag && this->parent_->remote_values.get_color_brightness() == 0.0f) {
this->color_brightness_ = 1.0f;
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, true);
}
@ -304,27 +305,27 @@ LightColorValues LightCall::validate_() {
// Create color values for the light with this call applied.
auto v = this->parent_->remote_values;
if (has_color_mode)
if (has_color_mode_flag)
v.set_color_mode(this->color_mode_);
if (has_state)
if (has_state_flag)
v.set_state(this->state_);
if (has_brightness)
if (has_brightness_flag)
v.set_brightness(this->brightness_);
if (has_color_brightness)
if (has_color_brightness_flag)
v.set_color_brightness(this->color_brightness_);
if (has_red)
if (has_red_flag)
v.set_red(this->red_);
if (has_green)
if (has_green_flag)
v.set_green(this->green_);
if (has_blue)
if (has_blue_flag)
v.set_blue(this->blue_);
if (has_white)
if (has_white_flag)
v.set_white(this->white_);
if (has_color_temperature)
if (has_color_temperature_flag)
v.set_color_temperature(this->color_temperature_);
if (has_cold_white)
if (has_cold_white_flag)
v.set_cold_white(this->cold_white_);
if (has_warm_white)
if (has_warm_white_flag)
v.set_warm_white(this->warm_white_);
v.normalize_color();
@ -380,7 +381,7 @@ LightColorValues LightCall::validate_() {
// If not a flash and turning the light off, then disable the light
// Do not use light color values directly, so that effects can set 0% brightness
// Reason: When user turns off the light in frontend, the effect should also stop
bool target_state = has_state ? this->state_ : v.is_on();
bool target_state = has_state_flag ? this->state_ : v.is_on();
if (!this->has_flash_() && !target_state) {
if (this->has_effect_()) {
log_invalid_parameter(name, "cannot start effect when turning off");

View File

@ -8,11 +8,8 @@ namespace light {
// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
// Helper to convert float 0-1 to uint8_t 0-255
static inline uint8_t to_uint8_scaled(float value) { return uint8_t(value * 255); }
// Lookup table for color mode strings
static const char *get_color_mode_json_str(ColorMode mode) {
static constexpr const char *get_color_mode_json_str(ColorMode mode) {
switch (mode) {
case ColorMode::ON_OFF:
return "onoff";
@ -54,17 +51,17 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
if (color_mode & ColorCapability::ON_OFF)
root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF";
if (color_mode & ColorCapability::BRIGHTNESS)
root["brightness"] = to_uint8_scaled(values.get_brightness());
root["brightness"] = to_uint8_scale(values.get_brightness());
JsonObject color = root["color"].to<JsonObject>();
if (color_mode & ColorCapability::RGB) {
float color_brightness = values.get_color_brightness();
color["r"] = to_uint8_scaled(color_brightness * values.get_red());
color["g"] = to_uint8_scaled(color_brightness * values.get_green());
color["b"] = to_uint8_scaled(color_brightness * values.get_blue());
color["r"] = to_uint8_scale(color_brightness * values.get_red());
color["g"] = to_uint8_scale(color_brightness * values.get_green());
color["b"] = to_uint8_scale(color_brightness * values.get_blue());
}
if (color_mode & ColorCapability::WHITE) {
uint8_t white_val = to_uint8_scaled(values.get_white());
uint8_t white_val = to_uint8_scale(values.get_white());
color["w"] = white_val;
root["white_value"] = white_val; // legacy API
}
@ -73,8 +70,8 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
root["color_temp"] = uint32_t(values.get_color_temperature());
}
if (color_mode & ColorCapability::COLD_WARM_WHITE) {
color["c"] = to_uint8_scaled(values.get_cold_white());
color["w"] = to_uint8_scaled(values.get_warm_white());
color["c"] = to_uint8_scale(values.get_cold_white());
color["w"] = to_uint8_scale(values.get_warm_white());
}
}