diff --git a/esphome/components/light/light_call.cpp b/esphome/components/light/light_call.cpp index 557d001321..d7e8ce6298 100644 --- a/esphome/components/light/light_call.cpp +++ b/esphome/components/light/light_call.cpp @@ -32,19 +32,26 @@ LightCall &LightCall::parse_color_json(JsonObject &root) { if (root.containsKey("color")) { JsonObject &color = root["color"]; + // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness. + float max_rgb = 0.0f; if (color.containsKey("r")) { - this->set_red(float(color["r"]) / 255.0f); + float r = float(color["r"]) / 255.0f; + max_rgb = fmaxf(max_rgb, r); + this->set_red(r); } if (color.containsKey("g")) { - this->set_green(float(color["g"]) / 255.0f); + float g = float(color["g"]) / 255.0f; + max_rgb = fmaxf(max_rgb, g); + this->set_green(g); } if (color.containsKey("b")) { - this->set_blue(float(color["b"]) / 255.0f); + float b = float(color["b"]) / 255.0f; + max_rgb = fmaxf(max_rgb, b); + this->set_blue(b); + } + if (color.containsKey("r") || color.containsKey("g") || color.containsKey("b")) { + this->set_color_brightness(max_rgb); } - } - - if (root.containsKey("color_brightness")) { - this->set_color_brightness(float(root["color_brightness"]) / 255.0f); } if (root.containsKey("white_value")) { @@ -418,7 +425,7 @@ LightCall &LightCall::set_brightness_if_supported(float brightness) { } LightCall &LightCall::set_color_brightness_if_supported(float brightness) { if (this->parent_->get_traits().get_supports_rgb_white_value()) - this->set_brightness(brightness); + this->set_color_brightness(brightness); return *this; } LightCall &LightCall::set_red_if_supported(float red) { diff --git a/esphome/components/light/light_color_values.h b/esphome/components/light/light_color_values.h index 4b6ca9e576..54dcaea5a3 100644 --- a/esphome/components/light/light_color_values.h +++ b/esphome/components/light/light_color_values.h @@ -135,12 +135,11 @@ class LightColorValues { root["brightness"] = uint8_t(this->get_brightness() * 255); if (traits.get_supports_rgb()) { JsonObject &color = root.createNestedObject("color"); - color["r"] = uint8_t(this->get_red() * 255); - color["g"] = uint8_t(this->get_green() * 255); - color["b"] = uint8_t(this->get_blue() * 255); + color["r"] = uint8_t(this->get_color_brightness() * this->get_red() * 255); + color["g"] = uint8_t(this->get_color_brightness() * this->get_green() * 255); + color["b"] = uint8_t(this->get_color_brightness() * this->get_blue() * 255); } if (traits.get_supports_rgb_white_value()) { - root["color_brightness"] = uint8_t(this->get_color_brightness() * 255); root["white_value"] = uint8_t(this->get_white() * 255); } if (traits.get_supports_color_temperature()) diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index 69d147b6f3..a97e4f6790 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -18,6 +18,7 @@ LightCall LightState::make_call() { return LightCall(this); } struct LightStateRTCState { bool state{false}; float brightness{1.0f}; + float color_brightness{1.0f}; float red{1.0f}; float green{1.0f}; float blue{1.0f}; @@ -65,6 +66,7 @@ void LightState::setup() { call.set_state(recovered.state); call.set_brightness_if_supported(recovered.brightness); + call.set_color_brightness_if_supported(recovered.color_brightness); call.set_red_if_supported(recovered.red); call.set_green_if_supported(recovered.green); call.set_blue_if_supported(recovered.blue); @@ -244,6 +246,7 @@ void LightState::save_remote_values_() { LightStateRTCState saved; saved.state = this->remote_values.is_on(); saved.brightness = this->remote_values.get_brightness(); + saved.color_brightness = this->remote_values.get_color_brightness(); saved.red = this->remote_values.get_red(); saved.green = this->remote_values.get_green(); saved.blue = this->remote_values.get_blue();