From 36aa711966820aeb6e0fd838ef9cbbc5ce970ff6 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 22 Sep 2023 07:22:20 -0700 Subject: [PATCH 1/2] Allow saving the light current color as a favorite --- .../lights/dialog-light-color-favorite.ts | 28 +++++++++++++++++++ .../ha-more-info-light-favorite-colors.ts | 1 + .../show-dialog-light-color-favorite.ts | 1 + 3 files changed, 30 insertions(+) diff --git a/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts b/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts index 8549c906821b..64ceb19ce0ed 100644 --- a/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts +++ b/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts @@ -54,6 +54,9 @@ class DialogLightColorFavorite extends LitElement { this._entry = dialogParams.entry; this._dialogParams = dialogParams; this._updateModes(dialogParams.defaultMode); + if (dialogParams.add) { + this._loadCurrentColor(); + } await this.updateComplete; } @@ -90,6 +93,31 @@ class DialogLightColorFavorite extends LitElement { : this._modes[0]); } + private _loadCurrentColor() { + const attributes = this.stateObj!.attributes; + const color_mode = attributes.color_mode; + + if (attributes.color_mode === LightColorMode.XY) { + // XY color not supported for favorites. Try to grab the hs or rgb instead. + if (attributes.hs_color) { + this._color = { hs_color: attributes.hs_color }; + } else if (attributes.rgb_color) { + this._color = { rgb_color: attributes.rgb_color }; + } + } else if ( + color_mode === LightColorMode.COLOR_TEMP && + attributes.color_temp_kelvin + ) { + this._color = { + color_temp_kelvin: attributes.color_temp_kelvin, + }; + } else if (attributes[color_mode + "_color"]) { + this._color = { + [color_mode + "_color"]: attributes[color_mode + "_color"], + } as LightColor; + } + } + private _colorChanged(ev: CustomEvent) { this._color = ev.detail; } diff --git a/src/dialogs/more-info/components/lights/ha-more-info-light-favorite-colors.ts b/src/dialogs/more-info/components/lights/ha-more-info-light-favorite-colors.ts index ad00a5b105f3..3b1ac5032999 100644 --- a/src/dialogs/more-info/components/lights/ha-more-info-light-favorite-colors.ts +++ b/src/dialogs/more-info/components/lights/ha-more-info-light-favorite-colors.ts @@ -141,6 +141,7 @@ export class HaMoreInfoLightFavoriteColors extends LitElement { private _add = async () => { const color = await showLightColorFavoriteDialog(this, { entry: this.entry!, + add: true, title: this.hass.localize( "ui.dialogs.more_info_control.light.favorite_color.add_title" ), diff --git a/src/dialogs/more-info/components/lights/show-dialog-light-color-favorite.ts b/src/dialogs/more-info/components/lights/show-dialog-light-color-favorite.ts index d8bc86509385..e91e3223ef9e 100644 --- a/src/dialogs/more-info/components/lights/show-dialog-light-color-favorite.ts +++ b/src/dialogs/more-info/components/lights/show-dialog-light-color-favorite.ts @@ -7,6 +7,7 @@ export interface LightColorFavoriteDialogParams { entry: ExtEntityRegistryEntry; title: string; defaultMode?: LightPickerMode; + add?: boolean; submit?: (color?: LightColor) => void; cancel?: () => void; } From 1bafb76bbe7a87fb248740c9b306c46245129d50 Mon Sep 17 00:00:00 2001 From: karwosts Date: Mon, 25 Sep 2023 11:36:43 -0700 Subject: [PATCH 2/2] refactoring --- .../lights/dialog-light-color-favorite.ts | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts b/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts index 64ceb19ce0ed..935b6377ac96 100644 --- a/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts +++ b/src/dialogs/more-info/components/lights/dialog-light-color-favorite.ts @@ -53,10 +53,8 @@ class DialogLightColorFavorite extends LitElement { ): Promise { this._entry = dialogParams.entry; this._dialogParams = dialogParams; - this._updateModes(dialogParams.defaultMode); - if (dialogParams.add) { - this._loadCurrentColor(); - } + this._updateModes(); + this._loadCurrentColorAndMode(dialogParams.add, dialogParams.defaultMode); await this.updateComplete; } @@ -67,7 +65,7 @@ class DialogLightColorFavorite extends LitElement { fireEvent(this, "dialog-closed", { dialog: this.localName }); } - private _updateModes(defaultMode?: LightPickerMode) { + private _updateModes() { const supportsTemp = lightSupportsColorMode( this.stateObj!, LightColorMode.COLOR_TEMP @@ -84,38 +82,44 @@ class DialogLightColorFavorite extends LitElement { } this._modes = modes; - this._mode = - defaultMode ?? - (this.stateObj!.attributes.color_mode - ? this.stateObj!.attributes.color_mode === LightColorMode.COLOR_TEMP - ? LightColorMode.COLOR_TEMP - : "color" - : this._modes[0]); } - private _loadCurrentColor() { + private _loadCurrentColorAndMode( + add?: boolean, + defaultMode?: LightPickerMode + ) { const attributes = this.stateObj!.attributes; const color_mode = attributes.color_mode; - if (attributes.color_mode === LightColorMode.XY) { + 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) { - this._color = { hs_color: attributes.hs_color }; + currentColor = { hs_color: attributes.hs_color }; } else if (attributes.rgb_color) { - this._color = { rgb_color: attributes.rgb_color }; + currentColor = { rgb_color: attributes.rgb_color }; } } else if ( color_mode === LightColorMode.COLOR_TEMP && attributes.color_temp_kelvin ) { - this._color = { + currentMode = LightColorMode.COLOR_TEMP; + currentColor = { color_temp_kelvin: attributes.color_temp_kelvin, }; } else if (attributes[color_mode + "_color"]) { - this._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) {