Skip to content

Commit

Permalink
Run script in script editor open more info if fields (#19982)
Browse files Browse the repository at this point in the history
* Run script in script editor open more info if fields

* Extract function
  • Loading branch information
balloob authored Mar 4, 2024
1 parent acc229a commit 7ab2d14
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/data/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Trigger,
} from "./automation";
import { BlueprintInput } from "./blueprint";
import { computeObjectId } from "../common/entity/compute_object_id";

export const MODES = ["single", "restart", "queued", "parallel"] as const;
export const MODES_MAX = ["queued", "parallel"] as const;
Expand Down Expand Up @@ -404,3 +405,11 @@ export const getActionType = (action: Action): ActionType => {
}
return "unknown";
};

export const hasScriptFields = (
hass: HomeAssistant,
entityId: string
): boolean => {
const fields = hass.services.script[computeObjectId(entityId)]?.fields;
return fields !== undefined && Object.keys(fields).length > 0;
};
10 changes: 10 additions & 0 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
fetchScriptFileConfig,
getScriptEditorInitData,
getScriptStateConfig,
hasScriptFields,
isMaxMode,
showScriptEditor,
triggerScript,
Expand All @@ -62,6 +63,7 @@ import { showToast } from "../../../util/toast";
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 Down Expand Up @@ -611,6 +613,14 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {

private async _runScript(ev: CustomEvent) {
ev.stopPropagation();

if (hasScriptFields(this.hass, this._entityId!)) {
showMoreInfoDialog(this, {
entityId: this._entityId!,
});
return;
}

await triggerScript(this.hass, this.scriptId!);
showToast(this, {
message: this.hass.localize("ui.notification_toast.triggered", {
Expand Down
8 changes: 2 additions & 6 deletions src/panels/lovelace/entity-rows/hui-script-entity-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import {
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { isUnavailableState } from "../../../data/entity";
import { canRun, ScriptEntity } from "../../../data/script";
import { canRun, hasScriptFields, ScriptEntity } from "../../../data/script";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import { ActionRowConfig, LovelaceRow } from "./types";
import { computeObjectId } from "../../../common/entity/compute_object_id";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";

@customElement("hui-script-entity-row")
Expand Down Expand Up @@ -95,10 +94,7 @@ class HuiScriptEntityRow extends LitElement implements LovelaceRow {
private _runScript(ev): void {
ev.stopPropagation();

const fields =
this.hass!.services.script[computeObjectId(this._config!.entity)]?.fields;

if (fields && Object.keys(fields).length > 0) {
if (hasScriptFields(this.hass!, this._config!.entity)) {
showMoreInfoDialog(this, { entityId: this._config!.entity });
} else {
this._callService("turn_on");
Expand Down
9 changes: 2 additions & 7 deletions src/state-summary/state-card-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { customElement, property } from "lit/decorators";
import "../components/entity/ha-entity-toggle";
import "../components/entity/state-info";
import { isUnavailableState } from "../data/entity";
import { canRun, ScriptEntity } from "../data/script";
import { canRun, hasScriptFields, ScriptEntity } from "../data/script";
import { haStyle } from "../resources/styles";
import { HomeAssistant } from "../types";
import { computeObjectId } from "../common/entity/compute_object_id";
import { showMoreInfoDialog } from "../dialogs/more-info/show-ha-more-info-dialog";

@customElement("state-card-script")
Expand Down Expand Up @@ -59,11 +58,7 @@ class StateCardScript extends LitElement {
private _runScript(ev: Event) {
ev.stopPropagation();

const fields =
this.hass!.services.script[computeObjectId(this.stateObj.entity_id)]
?.fields;

if (fields && Object.keys(fields).length > 0) {
if (hasScriptFields(this.hass, this.stateObj.entity_id)) {
showMoreInfoDialog(this, { entityId: this.stateObj.entity_id });
} else {
this._callService("turn_on");
Expand Down

0 comments on commit 7ab2d14

Please sign in to comment.