Skip to content

Commit

Permalink
Only use icon for script
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed May 27, 2024
1 parent 1afbf5c commit ba926b5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import "../../../../components/ha-textfield";
import { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import type { AutomationRenameDialogParams } from "./show-dialog-automation-rename";
import type {
AutomationRenameDialogParams,
ScriptRenameDialogParams,
} from "./show-dialog-automation-rename";

@customElement("ha-dialog-automation-rename")
class DialogAutomationRename extends LitElement implements HassDialog {
Expand All @@ -22,18 +25,20 @@ class DialogAutomationRename extends LitElement implements HassDialog {

@state() private _error?: string;

private _params!: AutomationRenameDialogParams;
private _params!: AutomationRenameDialogParams | ScriptRenameDialogParams;

private _newName?: string;

private _newIcon?: string;

private _newDescription?: string;

public showDialog(params: AutomationRenameDialogParams): void {
public showDialog(
params: AutomationRenameDialogParams | ScriptRenameDialogParams
): void {
this._opened = true;
this._params = params;
this._newIcon = params.icon;
this._newIcon = "icon" in params.config ? params.config.icon : undefined;
this._newName =
params.config.alias ||
this.hass.localize("ui.panel.config.automation.editor.default_name");
Expand Down Expand Up @@ -88,22 +93,24 @@ class DialogAutomationRename extends LitElement implements HassDialog {
@input=${this._valueChanged}
></ha-textfield>
${this._params.supportsIcon
? html`<ha-icon-picker
.hass=${this.hass}
.label=${this.hass.localize(
"ui.panel.config.automation.editor.icon"
)}
.value=${this._newIcon}
@value-changed=${this._iconChanged}
>
<ha-domain-icon
slot="fallback"
domain="automation"
${this._params.domain === "script"
? html`
<ha-icon-picker
.hass=${this.hass}
.label=${this.hass.localize(
"ui.panel.config.automation.editor.icon"
)}
.value=${this._newIcon}
@value-changed=${this._iconChanged}
>
</ha-domain-icon>
</ha-icon-picker>`
<ha-domain-icon
slot="fallback"
domain=${this._params.domain}
.hass=${this.hass}
>
</ha-domain-icon>
</ha-icon-picker>
`
: nothing}
<ha-textarea
.label=${this.hass.localize(
Expand Down Expand Up @@ -152,14 +159,21 @@ class DialogAutomationRename extends LitElement implements HassDialog {
this._error = "Name is required";
return;
}
this._params.updateConfig(
{
if (this._params.domain === "script") {
this._params.updateConfig({
...this._params.config,
alias: this._newName,
description: this._newDescription,
},
this._params.supportsIcon ? this._newIcon : undefined
);
icon: this._newIcon,
});
} else {
this._params.updateConfig({
...this._params.config,
alias: this._newName,
description: this._newDescription,
});
}

this.closeDialog();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ export const loadAutomationRenameDialog = () =>

export interface AutomationRenameDialogParams {
config: AutomationConfig;
supportsIcon: boolean;
updateConfig: (config: AutomationConfig, icon: string | undefined) => void;
icon: string | undefined;
domain: "automation";
updateConfig: (config: AutomationConfig) => void;
onClose: () => void;
}

export interface ScriptnRenameDialogParams {
export interface ScriptRenameDialogParams {
config: ScriptConfig;
supportsIcon: boolean;
domain: "script";
updateConfig: (config: ScriptConfig) => void;
icon: string | undefined;
onClose: () => void;
}

export const showAutomationRenameDialog = (
element: HTMLElement,
dialogParams: AutomationRenameDialogParams | ScriptnRenameDialogParams
dialogParams: AutomationRenameDialogParams | ScriptRenameDialogParams
): void => {
fireEvent(element, "show-dialog", {
dialogTag: "ha-dialog-automation-rename",
Expand Down
24 changes: 2 additions & 22 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { UNAVAILABLE } from "../../../data/entity";
import {
EntityRegistryDisplayEntry,
fetchEntityRegistry,
updateEntityRegistryEntry,
} from "../../../data/entity_registry";
import {
showAlertDialog,
Expand Down Expand Up @@ -104,8 +103,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {

@state() private _config?: AutomationConfig;

@state() private _icon?: string;

@state() private _dirty = false;

@state() private _errors?: string;
Expand Down Expand Up @@ -472,23 +469,13 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
sub(this._config)
);
}

if (changedProps.has("hass")) {
if (this._entry && !this._dirty) {
this._icon = this._entry.icon;
}
}
}

private _setEntityId() {
const automation = this.automations.find(
(entity: AutomationEntity) => entity.attributes.id === this.automationId
);
this._entityId = automation?.entity_id;

if (this._entry) {
this._icon = this._entry.icon;
}
}

private async _checkValidation() {
Expand Down Expand Up @@ -709,11 +696,9 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
return new Promise((resolve) => {
showAutomationRenameDialog(this, {
config: this._config!,
icon: this._icon,
supportsIcon: this._entry !== undefined,
updateConfig: (config, icon) => {
domain: "automation",
updateConfig: (config) => {
this._config = config;
this._icon = icon;
this._dirty = true;
this.requestUpdate();
resolve(true);
Expand Down Expand Up @@ -750,11 +735,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
this._validationErrors = undefined;
try {
await saveAutomationConfig(this.hass, id, this._config!);
if (this._entry && this._entry.icon !== this._icon) {
await updateEntityRegistryEntry(this.hass, this._entry.entity_id, {
icon: this._icon,
});
}
} catch (errors: any) {
this._errors = errors.body.message || errors.error || errors.body;
showToast(this, {
Expand Down
25 changes: 3 additions & 22 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ import "../../../components/ha-button-menu";
import "../../../components/ha-fab";

import "../../../components/ha-icon-button";
import "../../../components/ha-list-item";
import "../../../components/ha-svg-icon";
import "../../../components/ha-yaml-editor";
import "../../../components/ha-list-item";
import { validateConfig } from "../../../data/config";
import { UNAVAILABLE } from "../../../data/entity";
import {
EntityRegistryDisplayEntry,
EntityRegistryEntry,
updateEntityRegistryEntry,
} from "../../../data/entity_registry";
import {
ScriptConfig,
Expand All @@ -54,6 +53,7 @@ import {
triggerScript,
} from "../../../data/script";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
import "../../../layouts/hass-subpage";
import { KeyboardShortcutMixin } from "../../../mixins/keyboard-shortcut-mixin";
import { haStyle } from "../../../resources/styles";
Expand All @@ -64,7 +64,6 @@ import { showAutomationRenameDialog } from "../automation/automation-rename-dial
import "./blueprint-script-editor";
import "./manual-script-editor";
import type { HaManualScriptEditor } from "./manual-script-editor";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";

export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;
Expand All @@ -85,8 +84,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {

@state() private _idError = false;

@state() private _icon?: string;

@state() private _dirty = false;

@state() private _errors?: string;
Expand Down Expand Up @@ -419,12 +416,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
this._dirty = false;
this._readOnly = true;
}

if (changedProps.has("hass")) {
if (this._entry && !this._dirty) {
this._icon = this._entry.icon;
}
}
}

private _setEntityId(id?: string) {
Expand All @@ -434,10 +425,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
} else {
this._idError = false;
}

if (this._entry) {
this._icon = this._entry.icon;
}
}

private async _checkValidation() {
Expand Down Expand Up @@ -674,8 +661,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
return new Promise((resolve) => {
showAutomationRenameDialog(this, {
config: this._config!,
icon: this._icon,
supportsIcon: this._entry !== undefined,
domain: "script",
updateConfig: (config) => {
this._config = config;
this._dirty = true;
Expand Down Expand Up @@ -734,11 +720,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
"config/script/config/" + id,
this._config
);
if (this._entry && this._entry.icon !== this._icon) {
await updateEntityRegistryEntry(this.hass, this._entry.entity_id, {
icon: this._icon,
});
}
} catch (errors: any) {
this._errors = errors.body.message || errors.error || errors.body;
showToast(this, {
Expand Down

0 comments on commit ba926b5

Please sign in to comment.