Allow saving the light current color as a favorite (#17992)

This commit is contained in:
karwosts 2023-09-26 09:59:59 -07:00 committed by GitHub
parent c567a61dd7
commit acb32ae5c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 9 deletions

View File

@ -53,7 +53,8 @@ class DialogLightColorFavorite extends LitElement {
): Promise<void> { ): Promise<void> {
this._entry = dialogParams.entry; this._entry = dialogParams.entry;
this._dialogParams = dialogParams; this._dialogParams = dialogParams;
this._updateModes(dialogParams.defaultMode); this._updateModes();
this._loadCurrentColorAndMode(dialogParams.add, dialogParams.defaultMode);
await this.updateComplete; await this.updateComplete;
} }
@ -64,7 +65,7 @@ class DialogLightColorFavorite extends LitElement {
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
} }
private _updateModes(defaultMode?: LightPickerMode) { private _updateModes() {
const supportsTemp = lightSupportsColorMode( const supportsTemp = lightSupportsColorMode(
this.stateObj!, this.stateObj!,
LightColorMode.COLOR_TEMP LightColorMode.COLOR_TEMP
@ -81,13 +82,44 @@ class DialogLightColorFavorite extends LitElement {
} }
this._modes = modes; this._modes = modes;
this._mode = }
defaultMode ??
(this.stateObj!.attributes.color_mode private _loadCurrentColorAndMode(
? this.stateObj!.attributes.color_mode === LightColorMode.COLOR_TEMP add?: boolean,
? LightColorMode.COLOR_TEMP defaultMode?: LightPickerMode
: "color" ) {
: this._modes[0]); const attributes = this.stateObj!.attributes;
const color_mode = attributes.color_mode;
let currentColor: LightColor | undefined;
let currentMode: LightPickerMode | undefined;
if (color_mode === LightColorMode.XY) {
currentMode = "color";
// XY color not supported for favorites. Try to grab the hs or rgb instead.
if (attributes.hs_color) {
currentColor = { hs_color: attributes.hs_color };
} else if (attributes.rgb_color) {
currentColor = { rgb_color: attributes.rgb_color };
}
} else if (
color_mode === LightColorMode.COLOR_TEMP &&
attributes.color_temp_kelvin
) {
currentMode = LightColorMode.COLOR_TEMP;
currentColor = {
color_temp_kelvin: attributes.color_temp_kelvin,
};
} else if (attributes[color_mode + "_color"]) {
currentMode = "color";
currentColor = {
[color_mode + "_color"]: attributes[color_mode + "_color"],
} as LightColor;
}
if (add) {
this._color = currentColor;
}
this._mode = defaultMode ?? currentMode ?? this._modes[0];
} }
private _colorChanged(ev: CustomEvent) { private _colorChanged(ev: CustomEvent) {

View File

@ -141,6 +141,7 @@ export class HaMoreInfoLightFavoriteColors extends LitElement {
private _add = async () => { private _add = async () => {
const color = await showLightColorFavoriteDialog(this, { const color = await showLightColorFavoriteDialog(this, {
entry: this.entry!, entry: this.entry!,
add: true,
title: this.hass.localize( title: this.hass.localize(
"ui.dialogs.more_info_control.light.favorite_color.add_title" "ui.dialogs.more_info_control.light.favorite_color.add_title"
), ),

View File

@ -7,6 +7,7 @@ export interface LightColorFavoriteDialogParams {
entry: ExtEntityRegistryEntry; entry: ExtEntityRegistryEntry;
title: string; title: string;
defaultMode?: LightPickerMode; defaultMode?: LightPickerMode;
add?: boolean;
submit?: (color?: LightColor) => void; submit?: (color?: LightColor) => void;
cancel?: () => void; cancel?: () => void;
} }