mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 03:36:44 +00:00
Add webhook trigger allowed_methods/local_only options (#11680)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
0478aed28c
commit
b1f9469002
@ -123,6 +123,8 @@ export interface TimePatternTrigger extends BaseTrigger {
|
|||||||
export interface WebhookTrigger extends BaseTrigger {
|
export interface WebhookTrigger extends BaseTrigger {
|
||||||
platform: "webhook";
|
platform: "webhook";
|
||||||
webhook_id: string;
|
webhook_id: string;
|
||||||
|
allowed_methods?: string[];
|
||||||
|
local_only?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ZoneTrigger extends BaseTrigger {
|
export interface ZoneTrigger extends BaseTrigger {
|
||||||
|
@ -1,21 +1,26 @@
|
|||||||
import "../../../../../components/ha-icon-button";
|
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
|
||||||
import "../../../../../components/ha-textfield";
|
import { mdiCog, mdiContentCopy } from "@mdi/js";
|
||||||
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { mdiContentCopy } from "@mdi/js";
|
|
||||||
import { css, html, LitElement, PropertyValues } from "lit";
|
import { css, html, LitElement, PropertyValues } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { fireEvent } from "../../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../../common/dom/fire_event";
|
||||||
import { slugify } from "../../../../../common/string/slugify";
|
import { slugify } from "../../../../../common/string/slugify";
|
||||||
import { copyToClipboard } from "../../../../../common/util/copy-clipboard";
|
import { copyToClipboard } from "../../../../../common/util/copy-clipboard";
|
||||||
|
import "../../../../../components/ha-button-menu";
|
||||||
|
import "../../../../../components/ha-check-list-item";
|
||||||
|
import "../../../../../components/ha-icon-button";
|
||||||
|
import "../../../../../components/ha-textfield";
|
||||||
import type { HaTextField } from "../../../../../components/ha-textfield";
|
import type { HaTextField } from "../../../../../components/ha-textfield";
|
||||||
import { showToast } from "../../../../../util/toast";
|
|
||||||
import {
|
import {
|
||||||
WebhookTrigger,
|
|
||||||
AutomationConfig,
|
AutomationConfig,
|
||||||
|
WebhookTrigger,
|
||||||
} from "../../../../../data/automation";
|
} from "../../../../../data/automation";
|
||||||
import { HomeAssistant } from "../../../../../types";
|
import { HomeAssistant } from "../../../../../types";
|
||||||
|
import { showToast } from "../../../../../util/toast";
|
||||||
import { handleChangeEvent } from "../ha-automation-trigger-row";
|
import { handleChangeEvent } from "../ha-automation-trigger-row";
|
||||||
|
|
||||||
|
const SUPPORTED_METHODS = ["GET", "HEAD", "POST", "PUT"];
|
||||||
|
const DEFAULT_METHODS = ["POST", "PUT"];
|
||||||
const DEFAULT_WEBHOOK_ID = "";
|
const DEFAULT_WEBHOOK_ID = "";
|
||||||
|
|
||||||
@customElement("ha-automation-trigger-webhook")
|
@customElement("ha-automation-trigger-webhook")
|
||||||
@ -32,6 +37,8 @@ export class HaWebhookTrigger extends LitElement {
|
|||||||
|
|
||||||
public static get defaultConfig() {
|
public static get defaultConfig() {
|
||||||
return {
|
return {
|
||||||
|
allowed_methods: [...DEFAULT_METHODS],
|
||||||
|
local_only: true,
|
||||||
webhook_id: DEFAULT_WEBHOOK_ID,
|
webhook_id: DEFAULT_WEBHOOK_ID,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -72,6 +79,12 @@ export class HaWebhookTrigger extends LitElement {
|
|||||||
public willUpdate(changedProperties: PropertyValues) {
|
public willUpdate(changedProperties: PropertyValues) {
|
||||||
super.willUpdate(changedProperties);
|
super.willUpdate(changedProperties);
|
||||||
if (changedProperties.has("trigger")) {
|
if (changedProperties.has("trigger")) {
|
||||||
|
if (this.trigger.allowed_methods === undefined) {
|
||||||
|
this.trigger.allowed_methods = [...DEFAULT_METHODS];
|
||||||
|
}
|
||||||
|
if (this.trigger.local_only === undefined) {
|
||||||
|
this.trigger.local_only = true;
|
||||||
|
}
|
||||||
if (this.trigger.webhook_id === DEFAULT_WEBHOOK_ID) {
|
if (this.trigger.webhook_id === DEFAULT_WEBHOOK_ID) {
|
||||||
this.trigger.webhook_id = this._generateWebhookId();
|
this.trigger.webhook_id = this._generateWebhookId();
|
||||||
}
|
}
|
||||||
@ -79,31 +92,68 @@ export class HaWebhookTrigger extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const { webhook_id: webhookId } = this.trigger;
|
const {
|
||||||
|
allowed_methods: allowedMethods,
|
||||||
|
local_only: localOnly,
|
||||||
|
webhook_id: webhookId,
|
||||||
|
} = this.trigger;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-textfield
|
<div class="flex">
|
||||||
name="webhook_id"
|
<ha-textfield
|
||||||
.label=${this.hass.localize(
|
name="webhook_id"
|
||||||
"ui.panel.config.automation.editor.triggers.type.webhook.webhook_id"
|
|
||||||
)}
|
|
||||||
.helper=${this.hass.localize(
|
|
||||||
"ui.panel.config.automation.editor.triggers.type.webhook.webhook_id_helper"
|
|
||||||
)}
|
|
||||||
.disabled=${this.disabled}
|
|
||||||
iconTrailing
|
|
||||||
.value=${webhookId || ""}
|
|
||||||
@input=${this._valueChanged}
|
|
||||||
>
|
|
||||||
<ha-icon-button
|
|
||||||
@click=${this._copyUrl}
|
|
||||||
slot="trailingIcon"
|
|
||||||
.label=${this.hass.localize(
|
.label=${this.hass.localize(
|
||||||
"ui.panel.config.automation.editor.triggers.type.webhook.copy_url"
|
"ui.panel.config.automation.editor.triggers.type.webhook.webhook_id"
|
||||||
)}
|
)}
|
||||||
.path=${mdiContentCopy}
|
.helper=${this.hass.localize(
|
||||||
></ha-icon-button>
|
"ui.panel.config.automation.editor.triggers.type.webhook.webhook_id_helper"
|
||||||
</ha-textfield>
|
)}
|
||||||
|
.disabled=${this.disabled}
|
||||||
|
iconTrailing
|
||||||
|
.value=${webhookId || ""}
|
||||||
|
@input=${this._valueChanged}
|
||||||
|
>
|
||||||
|
<ha-icon-button
|
||||||
|
@click=${this._copyUrl}
|
||||||
|
slot="trailingIcon"
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.panel.config.automation.editor.triggers.type.webhook.copy_url"
|
||||||
|
)}
|
||||||
|
.path=${mdiContentCopy}
|
||||||
|
></ha-icon-button>
|
||||||
|
</ha-textfield>
|
||||||
|
<ha-button-menu multi>
|
||||||
|
<ha-icon-button
|
||||||
|
slot="trigger"
|
||||||
|
.label=${this.hass!.localize(
|
||||||
|
"ui.panel.config.automation.editor.triggers.type.webhook.webhook_settings"
|
||||||
|
)}
|
||||||
|
.path=${mdiCog}
|
||||||
|
></ha-icon-button>
|
||||||
|
${SUPPORTED_METHODS.map(
|
||||||
|
(method) => html`
|
||||||
|
<ha-check-list-item
|
||||||
|
left
|
||||||
|
.value=${method}
|
||||||
|
@request-selected=${this._allowedMethodsChanged}
|
||||||
|
.selected=${allowedMethods!.includes(method)}
|
||||||
|
>
|
||||||
|
${method}
|
||||||
|
</ha-check-list-item>
|
||||||
|
`
|
||||||
|
)}
|
||||||
|
<li divider role="separator"></li>
|
||||||
|
<ha-check-list-item
|
||||||
|
left
|
||||||
|
@request-selected=${this._localOnlyChanged}
|
||||||
|
.selected=${localOnly!}
|
||||||
|
>
|
||||||
|
${this.hass!.localize(
|
||||||
|
"ui.panel.config.automation.editor.triggers.type.webhook.local_only"
|
||||||
|
)}
|
||||||
|
</ha-check-list-item>
|
||||||
|
</ha-button-menu>
|
||||||
|
</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,6 +161,39 @@ export class HaWebhookTrigger extends LitElement {
|
|||||||
handleChangeEvent(this, ev);
|
handleChangeEvent(this, ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _localOnlyChanged(ev: CustomEvent<RequestSelectedDetail>): void {
|
||||||
|
ev.stopPropagation();
|
||||||
|
if (this.trigger.local_only === ev.detail.selected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newTrigger = {
|
||||||
|
...this.trigger,
|
||||||
|
local_only: ev.detail.selected,
|
||||||
|
};
|
||||||
|
fireEvent(this, "value-changed", { value: newTrigger });
|
||||||
|
}
|
||||||
|
|
||||||
|
private _allowedMethodsChanged(ev: CustomEvent<RequestSelectedDetail>): void {
|
||||||
|
ev.stopPropagation();
|
||||||
|
const method = (ev.target as any).value;
|
||||||
|
const selected = ev.detail.selected;
|
||||||
|
|
||||||
|
if (selected === this.trigger.allowed_methods?.includes(method)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const methods = this.trigger.allowed_methods ?? [];
|
||||||
|
const newMethods = [...methods];
|
||||||
|
|
||||||
|
if (selected) {
|
||||||
|
newMethods.push(method);
|
||||||
|
} else {
|
||||||
|
newMethods.splice(newMethods.indexOf(method), 1);
|
||||||
|
}
|
||||||
|
const newTrigger = { ...this.trigger, allowed_methods: newMethods };
|
||||||
|
fireEvent(this, "value-changed", { value: newTrigger });
|
||||||
|
}
|
||||||
|
|
||||||
private async _copyUrl(ev): Promise<void> {
|
private async _copyUrl(ev): Promise<void> {
|
||||||
const inputElement = ev.target.parentElement as HaTextField;
|
const inputElement = ev.target.parentElement as HaTextField;
|
||||||
const url = this.hass.hassUrl(`/api/webhook/${inputElement.value}`);
|
const url = this.hass.hassUrl(`/api/webhook/${inputElement.value}`);
|
||||||
@ -122,14 +205,22 @@ export class HaWebhookTrigger extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
|
.flex {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
ha-textfield {
|
ha-textfield {
|
||||||
display: block;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ha-textfield > ha-icon-button {
|
ha-textfield > ha-icon-button {
|
||||||
--mdc-icon-button-size: 24px;
|
--mdc-icon-button-size: 24px;
|
||||||
--mdc-icon-size: 18px;
|
--mdc-icon-size: 18px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ha-button-menu {
|
||||||
|
padding-top: 4px;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2259,8 +2259,10 @@
|
|||||||
"webhook": {
|
"webhook": {
|
||||||
"copy_url": "Copy URL to Clipboard",
|
"copy_url": "Copy URL to Clipboard",
|
||||||
"label": "Webhook",
|
"label": "Webhook",
|
||||||
|
"local_only": "Only accessible from the local network",
|
||||||
"webhook_id": "Webhook ID",
|
"webhook_id": "Webhook ID",
|
||||||
"webhook_id_helper": "Treat this ID like a password: keep it secret, and make it hard to guess."
|
"webhook_id_helper": "Treat this ID like a password: keep it secret, and make it hard to guess.",
|
||||||
|
"webhook_settings": "Webhook Settings"
|
||||||
},
|
},
|
||||||
"zone": {
|
"zone": {
|
||||||
"label": "Zone",
|
"label": "Zone",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user