Skip to content

Commit

Permalink
Use initial color param for favorite color dialog (#18039)
Browse files Browse the repository at this point in the history
* Use initial color param for favorite color dialog

* Select right mode if light is off
  • Loading branch information
piitaya authored Sep 27, 2023
1 parent 7111a21 commit b2e260d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ class DialogLightColorFavorite extends LitElement {
): Promise<void> {
this._entry = dialogParams.entry;
this._dialogParams = dialogParams;
this._color = dialogParams.initialColor ?? this._computeCurrentColor();
this._updateModes();
this._loadCurrentColorAndMode(dialogParams.add, dialogParams.defaultMode);
await this.updateComplete;
}

public closeDialog(): void {
Expand All @@ -82,19 +81,20 @@ class DialogLightColorFavorite extends LitElement {
}

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(
add?: boolean,
defaultMode?: LightPickerMode
) {
private _computeCurrentColor() {
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 };
Expand All @@ -105,21 +105,16 @@ class DialogLightColorFavorite extends LitElement {
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];
return currentColor;
}

private _colorChanged(ev: CustomEvent) {
Expand Down Expand Up @@ -230,7 +225,10 @@ class DialogLightColorFavorite extends LitElement {
<ha-button slot="secondaryAction" dialogAction="cancel">
${this.hass.localize("ui.common.cancel")}
</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
>
</ha-dialog>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { mdiCheck, mdiMinus, mdiPlus } from "@mdi/js";
import {
css,
CSSResultGroup,
html,
LitElement,
nothing,
PropertyValues,
TemplateResult,
css,
html,
nothing,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
Expand All @@ -19,18 +19,17 @@ import {
updateEntityRegistryEntry,
} from "../../../../data/entity_registry";
import {
computeDefaultFavoriteColors,
LightColor,
LightEntity,
computeDefaultFavoriteColors,
} from "../../../../data/light";
import { actionHandler } from "../../../../panels/lovelace/common/directives/action-handler-directive";
import {
loadSortable,
SortableInstance,
loadSortable,
} from "../../../../resources/sortable.ondemand";
import { HomeAssistant } from "../../../../types";
import { showConfirmationDialog } from "../../../generic/show-dialog-box";
import type { LightPickerMode } from "./dialog-light-color-favorite";
import "./ha-favorite-color-button";
import { showLightColorFavoriteDialog } from "./show-dialog-light-color-favorite";

Expand Down Expand Up @@ -141,7 +140,6 @@ 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"
),
Expand All @@ -156,13 +154,9 @@ export class HaMoreInfoLightFavoriteColors extends LitElement {
// Make sure the current favorite color is set
fireEvent(this, "favorite-color-edit-started");
await this._apply(index);
const defaultMode: LightPickerMode =
"color_temp_kelvin" in this._favoriteColors[index]
? "color_temp"
: "color";
const color = await showLightColorFavoriteDialog(this, {
entry: this.entry!,
defaultMode,
initialColor: this._favoriteColors[index],
title: this.hass.localize(
"ui.dialogs.more_info_control.light.favorite_color.edit_title"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { fireEvent } from "../../../../common/dom/fire_event";
import { ExtEntityRegistryEntry } from "../../../../data/entity_registry";
import { LightColor } from "../../../../data/light";
import type { LightPickerMode } from "./dialog-light-color-favorite";

export interface LightColorFavoriteDialogParams {
entry: ExtEntityRegistryEntry;
title: string;
defaultMode?: LightPickerMode;
add?: boolean;
initialColor?: LightColor;
submit?: (color?: LightColor) => void;
cancel?: () => void;
}
Expand Down

0 comments on commit b2e260d

Please sign in to comment.