Fix RGBWW colors (#9039)

This commit is contained in:
Bram Kragten 2021-04-29 21:53:22 +02:00 committed by GitHub
parent ffc92a7b63
commit 3c61d709b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 39 deletions

View File

@ -15,7 +15,6 @@ import { computeActiveState } from "../../common/entity/compute_active_state";
import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { stateIcon } from "../../common/entity/state_icon"; import { stateIcon } from "../../common/entity/state_icon";
import { iconColorCSS } from "../../common/style/icon_color_css"; import { iconColorCSS } from "../../common/style/icon_color_css";
import { getLightRgbColor, LightEntity } from "../../data/light";
import type { HomeAssistant } from "../../types"; import type { HomeAssistant } from "../../types";
import "../ha-icon"; import "../ha-icon";
@ -100,14 +99,8 @@ export class StateBadge extends LitElement {
hostStyle.backgroundImage = `url(${imageUrl})`; hostStyle.backgroundImage = `url(${imageUrl})`;
this._showIcon = false; this._showIcon = false;
} else if (stateObj.state === "on") { } else if (stateObj.state === "on") {
if ( if (this.stateColor !== false && stateObj.attributes.rgb_color) {
computeStateDomain(stateObj) === "light" && iconStyle.color = `rgb(${stateObj.attributes.rgb_color.join(",")})`;
this.stateColor !== false
) {
const rgb = getLightRgbColor(stateObj as LightEntity);
if (rgb) {
iconStyle.color = `rgb(${rgb.slice(0, 3).join(",")})`;
}
} }
if (stateObj.attributes.brightness && this.stateColor !== false) { if (stateObj.attributes.brightness && this.stateColor !== false) {
const brightness = stateObj.attributes.brightness; const brightness = stateObj.attributes.brightness;

View File

@ -56,7 +56,9 @@ export const lightSupportsDimming = (entity: LightEntity) => {
); );
}; };
export const getLightRgbColor = (entity: LightEntity): number[] | undefined => export const getLightCurrentModeRgbColor = (
entity: LightEntity
): number[] | undefined =>
entity.attributes.color_mode === LightColorModes.RGBWW entity.attributes.color_mode === LightColorModes.RGBWW
? entity.attributes.rgbww_color ? entity.attributes.rgbww_color
: entity.attributes.color_mode === LightColorModes.RGBW : entity.attributes.color_mode === LightColorModes.RGBW

View File

@ -18,7 +18,7 @@ import "../../../components/ha-icon-button";
import "../../../components/ha-labeled-slider"; import "../../../components/ha-labeled-slider";
import "../../../components/ha-paper-dropdown-menu"; import "../../../components/ha-paper-dropdown-menu";
import { import {
getLightRgbColor, getLightCurrentModeRgbColor,
LightColorModes, LightColorModes,
LightEntity, LightEntity,
lightIsInColorMode, lightIsInColorMode,
@ -34,6 +34,7 @@ const toggleButtons = [
{ label: "Color", value: "color" }, { label: "Color", value: "color" },
{ label: "Temperature", value: LightColorModes.COLOR_TEMP }, { label: "Temperature", value: LightColorModes.COLOR_TEMP },
]; ];
@customElement("more-info-light") @customElement("more-info-light")
class MoreInfoLight extends LitElement { class MoreInfoLight extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -155,7 +156,7 @@ class MoreInfoLight extends LitElement {
)} )}
icon="hass:brightness-7" icon="hass:brightness-7"
max="100" max="100"
.value=${this._colorBrightnessSliderValue ?? 255} .value=${this._colorBrightnessSliderValue ?? 100}
@change=${this._colorBrightnessSliderChanged} @change=${this._colorBrightnessSliderChanged}
pin pin
></ha-labeled-slider>` ></ha-labeled-slider>`
@ -302,9 +303,10 @@ class MoreInfoLight extends LitElement {
) )
: undefined; : undefined;
this._colorPickerColor = getLightRgbColor(stateObj)?.slice(0, 3) as this._colorPickerColor = getLightCurrentModeRgbColor(stateObj)?.slice(
| [number, number, number] 0,
| undefined; 3
) as [number, number, number] | undefined;
} else { } else {
this._brightnessSliderValue = 0; this._brightnessSliderValue = 0;
} }
@ -379,9 +381,9 @@ class MoreInfoLight extends LitElement {
return; return;
} }
wv = (wv * 255) / 100; wv = Math.min(255, Math.round((wv * 255) / 100));
const rgb = getLightRgbColor(this.stateObj!); const rgb = getLightCurrentModeRgbColor(this.stateObj!);
if (name === "wv") { if (name === "wv") {
const rgbw_color = rgb || [0, 0, 0, 0]; const rgbw_color = rgb || [0, 0, 0, 0];
@ -406,9 +408,15 @@ class MoreInfoLight extends LitElement {
private _colorBrightnessSliderChanged(ev: CustomEvent) { private _colorBrightnessSliderChanged(ev: CustomEvent) {
const target = ev.target as any; const target = ev.target as any;
const value = Number(target.value); let value = Number(target.value);
const rgb = (getLightRgbColor(this.stateObj!)?.slice(0, 3) || [ if (isNaN(value)) {
return;
}
value = (value * 255) / 100;
const rgb = (getLightCurrentModeRgbColor(this.stateObj!)?.slice(0, 3) || [
255, 255,
255, 255,
255, 255,
@ -420,7 +428,7 @@ class MoreInfoLight extends LitElement {
this._colorBrightnessSliderValue this._colorBrightnessSliderValue
? this._adjustColorBrightness( ? this._adjustColorBrightness(
rgb, rgb,
this._colorBrightnessSliderValue, (this._colorBrightnessSliderValue * 255) / 100,
true true
) )
: rgb, : rgb,
@ -449,9 +457,9 @@ class MoreInfoLight extends LitElement {
if (invert) { if (invert) {
ratio = 1 / ratio; ratio = 1 / ratio;
} }
rgbColor[0] *= ratio; rgbColor[0] = Math.min(255, Math.round(rgbColor[0] * ratio));
rgbColor[1] *= ratio; rgbColor[1] = Math.min(255, Math.round(rgbColor[1] * ratio));
rgbColor[2] *= ratio; rgbColor[2] = Math.min(255, Math.round(rgbColor[2] * ratio));
} }
return rgbColor; return rgbColor;
} }
@ -491,7 +499,7 @@ class MoreInfoLight extends LitElement {
this._colorBrightnessSliderValue this._colorBrightnessSliderValue
? this._adjustColorBrightness( ? this._adjustColorBrightness(
[ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b], [ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b],
this._colorBrightnessSliderValue (this._colorBrightnessSliderValue * 255) / 100
) )
: [ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b] : [ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b]
); );

View File

@ -28,7 +28,7 @@ import { stateIcon } from "../../../common/entity/state_icon";
import { isValidEntityId } from "../../../common/entity/valid_entity_id"; import { isValidEntityId } from "../../../common/entity/valid_entity_id";
import { iconColorCSS } from "../../../common/style/icon_color_css"; import { iconColorCSS } from "../../../common/style/icon_color_css";
import "../../../components/ha-card"; import "../../../components/ha-card";
import { getLightRgbColor, LightEntity } from "../../../data/light"; import { LightEntity } from "../../../data/light";
import { ActionHandlerEvent } from "../../../data/lovelace"; import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive"; import { actionHandler } from "../common/directives/action-handler-directive";
@ -301,14 +301,10 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
} }
private _computeColor(stateObj: HassEntity | LightEntity): string { private _computeColor(stateObj: HassEntity | LightEntity): string {
if ( if (this._config?.state_color && stateObj.attributes.rgb_color) {
!this._config?.state_color || return `rgb(${stateObj.attributes.rgb_color.join(",")})`;
computeStateDomain(stateObj) !== "light"
) {
return "";
} }
const rgb = getLightRgbColor(stateObj as LightEntity); return "";
return rgb ? `rgb(${rgb.slice(0, 3).join(",")})` : "";
} }
private _handleAction(ev: ActionHandlerEvent) { private _handleAction(ev: ActionHandlerEvent) {

View File

@ -21,11 +21,7 @@ import { stateIcon } from "../../../common/entity/state_icon";
import "../../../components/ha-card"; import "../../../components/ha-card";
import "../../../components/ha-icon-button"; import "../../../components/ha-icon-button";
import { UNAVAILABLE, UNAVAILABLE_STATES } from "../../../data/entity"; import { UNAVAILABLE, UNAVAILABLE_STATES } from "../../../data/entity";
import { import { LightEntity, lightSupportsDimming } from "../../../data/light";
getLightRgbColor,
LightEntity,
lightSupportsDimming,
} from "../../../data/light";
import { ActionHandlerEvent } from "../../../data/lovelace"; import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive"; import { actionHandler } from "../common/directives/action-handler-directive";
@ -247,8 +243,9 @@ export class HuiLightCard extends LitElement implements LovelaceCard {
if (stateObj.state === "off") { if (stateObj.state === "off") {
return ""; return "";
} }
const rgb = getLightRgbColor(stateObj); return stateObj.attributes.rgb_color
return rgb ? `rgb(${rgb.slice(0, 3).join(",")})` : ""; ? `rgb(${stateObj.attributes.rgb_color.join(",")})`
: "";
} }
private _handleAction(ev: ActionHandlerEvent) { private _handleAction(ev: ActionHandlerEvent) {