mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-14 04:46:34 +00:00
Update lovelace call service action (#8421)
This commit is contained in:
parent
d93d2b5945
commit
a41afcd714
@ -2,6 +2,7 @@ import {
|
|||||||
Connection,
|
Connection,
|
||||||
getCollection,
|
getCollection,
|
||||||
HassEventBase,
|
HassEventBase,
|
||||||
|
HassServiceTarget,
|
||||||
} from "home-assistant-js-websocket";
|
} from "home-assistant-js-websocket";
|
||||||
import { HASSDomEvent } from "../common/dom/fire_event";
|
import { HASSDomEvent } from "../common/dom/fire_event";
|
||||||
import { HuiErrorCard } from "../panels/lovelace/cards/hui-error-card";
|
import { HuiErrorCard } from "../panels/lovelace/cards/hui-error-card";
|
||||||
@ -120,8 +121,8 @@ export interface ToggleActionConfig extends BaseActionConfig {
|
|||||||
export interface CallServiceActionConfig extends BaseActionConfig {
|
export interface CallServiceActionConfig extends BaseActionConfig {
|
||||||
action: "call-service";
|
action: "call-service";
|
||||||
service: string;
|
service: string;
|
||||||
|
target?: HassServiceTarget;
|
||||||
service_data?: {
|
service_data?: {
|
||||||
entity_id?: string | [string];
|
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,12 @@ export const handleAction = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const [domain, service] = actionConfig.service.split(".", 2);
|
const [domain, service] = actionConfig.service.split(".", 2);
|
||||||
hass.callService(domain, service, actionConfig.service_data);
|
hass.callService(
|
||||||
|
domain,
|
||||||
|
service,
|
||||||
|
actionConfig.service_data,
|
||||||
|
actionConfig.target
|
||||||
|
);
|
||||||
forwardHaptic("light");
|
forwardHaptic("light");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,17 @@ import {
|
|||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
import { fireEvent } from "../../../common/dom/fire_event";
|
import { fireEvent } from "../../../common/dom/fire_event";
|
||||||
import "../../../components/ha-help-tooltip";
|
import "../../../components/ha-help-tooltip";
|
||||||
import "../../../components/ha-service-picker";
|
|
||||||
import {
|
import {
|
||||||
ActionConfig,
|
ActionConfig,
|
||||||
CallServiceActionConfig,
|
CallServiceActionConfig,
|
||||||
NavigateActionConfig,
|
NavigateActionConfig,
|
||||||
UrlActionConfig,
|
UrlActionConfig,
|
||||||
} from "../../../data/lovelace";
|
} from "../../../data/lovelace";
|
||||||
|
import { ServiceAction } from "../../../data/script";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { EditorTarget } from "../editor/types";
|
import { EditorTarget } from "../editor/types";
|
||||||
|
import "../../../components/ha-service-control";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
|
|
||||||
@customElement("hui-action-editor")
|
@customElement("hui-action-editor")
|
||||||
export class HuiActionEditor extends LitElement {
|
export class HuiActionEditor extends LitElement {
|
||||||
@ -47,10 +49,15 @@ export class HuiActionEditor extends LitElement {
|
|||||||
return config.url_path || "";
|
return config.url_path || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
get _service(): string {
|
private _serviceAction = memoizeOne(
|
||||||
const config = this.config as CallServiceActionConfig;
|
(config: CallServiceActionConfig): ServiceAction => {
|
||||||
return config.service || "";
|
return {
|
||||||
|
service: config.service || "",
|
||||||
|
data: config.service_data,
|
||||||
|
target: config.target,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this.actions) {
|
if (!this.hass || !this.actions) {
|
||||||
@ -117,17 +124,13 @@ export class HuiActionEditor extends LitElement {
|
|||||||
: ""}
|
: ""}
|
||||||
${this.config?.action === "call-service"
|
${this.config?.action === "call-service"
|
||||||
? html`
|
? html`
|
||||||
<ha-service-picker
|
<ha-service-control
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.value=${this._service}
|
.value=${this._serviceAction(this.config)}
|
||||||
.configValue=${"service"}
|
.showAdvanced=${this.hass.userData?.showAdvanced}
|
||||||
@value-changed=${this._valueChanged}
|
narrow
|
||||||
></ha-service-picker>
|
@value-changed=${this._serviceValueChanged}
|
||||||
<b>
|
></ha-service-control>
|
||||||
${this.hass!.localize(
|
|
||||||
"ui.panel.lovelace.editor.action-editor.editor_service_data"
|
|
||||||
)}
|
|
||||||
</b>
|
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
`;
|
`;
|
||||||
@ -174,6 +177,18 @@ export class HuiActionEditor extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _serviceValueChanged(ev: CustomEvent) {
|
||||||
|
ev.stopPropagation();
|
||||||
|
fireEvent(this, "value-changed", {
|
||||||
|
value: {
|
||||||
|
...this.config!,
|
||||||
|
service: ev.detail.value.service || "",
|
||||||
|
service_data: ev.detail.value.data || {},
|
||||||
|
target: ev.detail.value.target || {},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static get styles(): CSSResult {
|
static get styles(): CSSResult {
|
||||||
return css`
|
return css`
|
||||||
.dropdown {
|
.dropdown {
|
||||||
|
@ -120,20 +120,27 @@ const actionConfigStructConfirmation = union([
|
|||||||
|
|
||||||
const actionConfigStructUrl = object({
|
const actionConfigStructUrl = object({
|
||||||
action: literal("url"),
|
action: literal("url"),
|
||||||
url_path: string(),
|
url_path: optional(string()),
|
||||||
confirmation: optional(actionConfigStructConfirmation),
|
confirmation: optional(actionConfigStructConfirmation),
|
||||||
});
|
});
|
||||||
|
|
||||||
const actionConfigStructService = object({
|
const actionConfigStructService = object({
|
||||||
action: literal("call-service"),
|
action: literal("call-service"),
|
||||||
service: string(),
|
service: optional(string()),
|
||||||
service_data: optional(object()),
|
service_data: optional(object()),
|
||||||
|
target: optional(
|
||||||
|
object({
|
||||||
|
entity_id: optional(union([string(), array(string())])),
|
||||||
|
device_id: optional(union([string(), array(string())])),
|
||||||
|
area_id: optional(union([string(), array(string())])),
|
||||||
|
})
|
||||||
|
),
|
||||||
confirmation: optional(actionConfigStructConfirmation),
|
confirmation: optional(actionConfigStructConfirmation),
|
||||||
});
|
});
|
||||||
|
|
||||||
const actionConfigStructNavigate = object({
|
const actionConfigStructNavigate = object({
|
||||||
action: literal("navigate"),
|
action: literal("navigate"),
|
||||||
navigation_path: string(),
|
navigation_path: optional(string()),
|
||||||
confirmation: optional(actionConfigStructConfirmation),
|
confirmation: optional(actionConfigStructConfirmation),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user