Use initial color param for favorite color dialog (#18039)

* Use initial color param for favorite color dialog

* Select right mode if light is off
This commit is contained in:
Paul Bottein 2023-09-27 16:26:16 +02:00 committed by GitHub
parent 7111a21173
commit b2e260d6ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 30 deletions

View File

@ -53,9 +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._color = dialogParams.initialColor ?? this._computeCurrentColor();
this._updateModes(); this._updateModes();
this._loadCurrentColorAndMode(dialogParams.add, dialogParams.defaultMode);
await this.updateComplete;
} }
public closeDialog(): void { public closeDialog(): void {
@ -82,19 +81,20 @@ class DialogLightColorFavorite extends LitElement {
} }
this._modes = modes; this._modes = modes;
if (this._color) {
this._mode = "color_temp_kelvin" in this._color ? "color_temp" : "color";
} else {
this._mode = this._modes[0];
}
} }
private _loadCurrentColorAndMode( private _computeCurrentColor() {
add?: boolean,
defaultMode?: LightPickerMode
) {
const attributes = this.stateObj!.attributes; const attributes = this.stateObj!.attributes;
const color_mode = attributes.color_mode; const color_mode = attributes.color_mode;
let currentColor: LightColor | undefined; let currentColor: LightColor | undefined;
let currentMode: LightPickerMode | undefined;
if (color_mode === LightColorMode.XY) { if (color_mode === LightColorMode.XY) {
currentMode = "color";
// XY color not supported for favorites. Try to grab the hs or rgb instead. // XY color not supported for favorites. Try to grab the hs or rgb instead.
if (attributes.hs_color) { if (attributes.hs_color) {
currentColor = { hs_color: attributes.hs_color }; currentColor = { hs_color: attributes.hs_color };
@ -105,21 +105,16 @@ class DialogLightColorFavorite extends LitElement {
color_mode === LightColorMode.COLOR_TEMP && color_mode === LightColorMode.COLOR_TEMP &&
attributes.color_temp_kelvin attributes.color_temp_kelvin
) { ) {
currentMode = LightColorMode.COLOR_TEMP;
currentColor = { currentColor = {
color_temp_kelvin: attributes.color_temp_kelvin, color_temp_kelvin: attributes.color_temp_kelvin,
}; };
} else if (attributes[color_mode + "_color"]) { } else if (attributes[color_mode + "_color"]) {
currentMode = "color";
currentColor = { currentColor = {
[color_mode + "_color"]: attributes[color_mode + "_color"], [color_mode + "_color"]: attributes[color_mode + "_color"],
} as LightColor; } as LightColor;
} }
if (add) { return currentColor;
this._color = currentColor;
}
this._mode = defaultMode ?? currentMode ?? this._modes[0];
} }
private _colorChanged(ev: CustomEvent) { private _colorChanged(ev: CustomEvent) {
@ -230,7 +225,10 @@ class DialogLightColorFavorite extends LitElement {
<ha-button slot="secondaryAction" dialogAction="cancel"> <ha-button slot="secondaryAction" dialogAction="cancel">
${this.hass.localize("ui.common.cancel")} ${this.hass.localize("ui.common.cancel")}
</ha-button> </ha-button>
<ha-button slot="primaryAction" @click=${this._save} <ha-button
slot="primaryAction"
@click=${this._save}
.disabled=${!this._color}
>${this.hass.localize("ui.common.save")}</ha-button >${this.hass.localize("ui.common.save")}</ha-button
> >
</ha-dialog> </ha-dialog>

View File

@ -1,12 +1,12 @@
import { mdiCheck, mdiMinus, mdiPlus } from "@mdi/js"; import { mdiCheck, mdiMinus, mdiPlus } from "@mdi/js";
import { import {
css,
CSSResultGroup, CSSResultGroup,
html,
LitElement, LitElement,
nothing,
PropertyValues, PropertyValues,
TemplateResult, TemplateResult,
css,
html,
nothing,
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; import { classMap } from "lit/directives/class-map";
@ -19,18 +19,17 @@ import {
updateEntityRegistryEntry, updateEntityRegistryEntry,
} from "../../../../data/entity_registry"; } from "../../../../data/entity_registry";
import { import {
computeDefaultFavoriteColors,
LightColor, LightColor,
LightEntity, LightEntity,
computeDefaultFavoriteColors,
} from "../../../../data/light"; } from "../../../../data/light";
import { actionHandler } from "../../../../panels/lovelace/common/directives/action-handler-directive"; import { actionHandler } from "../../../../panels/lovelace/common/directives/action-handler-directive";
import { import {
loadSortable,
SortableInstance, SortableInstance,
loadSortable,
} from "../../../../resources/sortable.ondemand"; } from "../../../../resources/sortable.ondemand";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { showConfirmationDialog } from "../../../generic/show-dialog-box"; import { showConfirmationDialog } from "../../../generic/show-dialog-box";
import type { LightPickerMode } from "./dialog-light-color-favorite";
import "./ha-favorite-color-button"; import "./ha-favorite-color-button";
import { showLightColorFavoriteDialog } from "./show-dialog-light-color-favorite"; import { showLightColorFavoriteDialog } from "./show-dialog-light-color-favorite";
@ -141,7 +140,6 @@ 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"
), ),
@ -156,13 +154,9 @@ export class HaMoreInfoLightFavoriteColors extends LitElement {
// Make sure the current favorite color is set // Make sure the current favorite color is set
fireEvent(this, "favorite-color-edit-started"); fireEvent(this, "favorite-color-edit-started");
await this._apply(index); await this._apply(index);
const defaultMode: LightPickerMode =
"color_temp_kelvin" in this._favoriteColors[index]
? "color_temp"
: "color";
const color = await showLightColorFavoriteDialog(this, { const color = await showLightColorFavoriteDialog(this, {
entry: this.entry!, entry: this.entry!,
defaultMode, initialColor: this._favoriteColors[index],
title: this.hass.localize( title: this.hass.localize(
"ui.dialogs.more_info_control.light.favorite_color.edit_title" "ui.dialogs.more_info_control.light.favorite_color.edit_title"
), ),

View File

@ -1,13 +1,11 @@
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { ExtEntityRegistryEntry } from "../../../../data/entity_registry"; import { ExtEntityRegistryEntry } from "../../../../data/entity_registry";
import { LightColor } from "../../../../data/light"; import { LightColor } from "../../../../data/light";
import type { LightPickerMode } from "./dialog-light-color-favorite";
export interface LightColorFavoriteDialogParams { export interface LightColorFavoriteDialogParams {
entry: ExtEntityRegistryEntry; entry: ExtEntityRegistryEntry;
title: string; title: string;
defaultMode?: LightPickerMode; initialColor?: LightColor;
add?: boolean;
submit?: (color?: LightColor) => void; submit?: (color?: LightColor) => void;
cancel?: () => void; cancel?: () => void;
} }