From 9e5774525fd7a3b29cffcc38fbb6b553a90ddfbf Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 27 Jun 2023 18:22:02 +0200 Subject: [PATCH] Add service response support to dev tools (#17044) * Add service response support to dev tools * Change to `response_variable` --- src/data/script.ts | 2 + src/data/service.ts | 4 +- .../service/developer-tools-service.ts | 43 ++++++++++++++++--- src/translations/en.json | 1 + 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/data/script.ts b/src/data/script.ts index b679b0a22f..942296cef8 100644 --- a/src/data/script.ts +++ b/src/data/script.ts @@ -116,6 +116,7 @@ export interface ServiceAction extends BaseAction { entity_id?: string; target?: HassServiceTarget; data?: Record; + response_variable?: string; } export interface DeviceAction extends BaseAction { @@ -221,6 +222,7 @@ export interface VariablesAction extends BaseAction { export interface StopAction extends BaseAction { stop: string; + response_variable?: string; error?: boolean; } diff --git a/src/data/service.ts b/src/data/service.ts index b9ca93fe87..f93312ecf2 100644 --- a/src/data/service.ts +++ b/src/data/service.ts @@ -1,10 +1,10 @@ -import { HomeAssistant } from "../types"; +import { Context, HomeAssistant } from "../types"; import { Action } from "./script"; export const callExecuteScript = ( hass: HomeAssistant, sequence: Action | Action[] -) => +): Promise<{ context: Context; response: Record }> => hass.callWS({ type: "execute_script", sequence, diff --git a/src/panels/developer-tools/service/developer-tools-service.ts b/src/panels/developer-tools/service/developer-tools-service.ts index 6096bd16b4..996f377f5f 100644 --- a/src/panels/developer-tools/service/developer-tools-service.ts +++ b/src/panels/developer-tools/service/developer-tools-service.ts @@ -1,7 +1,7 @@ import { mdiHelpCircle } from "@mdi/js"; import { ERR_CONNECTION_LOST } from "home-assistant-js-websocket"; import { load } from "js-yaml"; -import { css, CSSResultGroup, html, LitElement } from "lit"; +import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { property, query, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { storage } from "../../../common/decorators/storage"; @@ -20,7 +20,7 @@ import "../../../components/ha-service-picker"; import "../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../components/ha-yaml-editor"; import { forwardHaptic } from "../../../data/haptics"; -import { ServiceAction } from "../../../data/script"; +import { Action, ServiceAction } from "../../../data/script"; import { callExecuteScript, serviceCallWillDisconnect, @@ -38,6 +38,8 @@ class HaPanelDevService extends LitElement { @state() private _uiAvailable = true; + @state() private _response?: Record; + @storage({ key: "panel-dev-service-state-service-data", state: true, @@ -52,7 +54,7 @@ class HaPanelDevService extends LitElement { }) private _yamlMode = false; - @query("ha-yaml-editor") private _yamlEditor?: HaYamlEditor; + @query("#yaml-editor") private _yamlEditor?: HaYamlEditor; protected firstUpdated(params) { super.firstUpdated(params); @@ -109,6 +111,7 @@ class HaPanelDevService extends LitElement { @value-changed=${this._serviceChanged} > - + ${this._response + ? html`
+ +
+ +
+
+
` + : nothing} ${(this._yamlMode ? fields : this._filterSelectorFields(fields)).length ? html`
${this._yamlMode - ? html`
+ ? html`

${target ? html` @@ -317,10 +336,20 @@ class HaPanelDevService extends LitElement { if (!this._serviceData?.service) { return; } + const [domain, service] = this._serviceData.service.split(".", 2); + const script: Action[] = []; + if ("response" in this.hass.services[domain][service]) { + script.push({ + ...this._serviceData, + response_variable: "service_result", + }); + script.push({ stop: "done", response_variable: "service_result" }); + } else { + script.push(this._serviceData); + } try { - await callExecuteScript(this.hass, [this._serviceData]); + this._response = (await callExecuteScript(this.hass, script)).response; } catch (err: any) { - const [domain, service] = this._serviceData.service.split(".", 2); if ( err.error?.code === ERR_CONNECTION_LOST && serviceCallWillDisconnect(domain, service) diff --git a/src/translations/en.json b/src/translations/en.json index 14c9a0123b..865a1d23db 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5322,6 +5322,7 @@ "title": "Services", "description": "The service dev tool allows you to call any available service in Home Assistant.", "call_service": "Call Service", + "response": "Response", "column_parameter": "Parameter", "column_description": "Description", "column_example": "Example",