Skip to content

Commit

Permalink
MQTT Trigger to Ha-Form (#11643)
Browse files Browse the repository at this point in the history
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
  • Loading branch information
zsarnett and balloob committed Feb 10, 2022
1 parent ce9f83e commit ac90bb7
Showing 1 changed file with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import "@polymer/paper-input/paper-input";
import { html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import { fireEvent } from "../../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../../components/ha-form/types";
import { MqttTrigger } from "../../../../../data/automation";
import { HomeAssistant } from "../../../../../types";
import {
handleChangeEvent,
TriggerElement,
} from "../ha-automation-trigger-row";
import type { TriggerElement } from "../ha-automation-trigger-row";

const SCHEMA: HaFormSchema[] = [
{ name: "topic", required: true, selector: { text: {} } },
{ name: "payload", selector: { text: {} } },
];

@customElement("ha-automation-trigger-mqtt")
export class HaMQTTTrigger extends LitElement implements TriggerElement {
Expand All @@ -19,30 +22,27 @@ export class HaMQTTTrigger extends LitElement implements TriggerElement {
}

protected render() {
const { topic, payload } = this.trigger;
return html`
<paper-input
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.mqtt.topic"
)}
name="topic"
.value=${topic}
@value-changed=${this._valueChanged}
></paper-input>
<paper-input
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.mqtt.payload"
)}
name="payload"
.value=${payload}
<ha-form
.schema=${SCHEMA}
.data=${this.trigger}
.hass=${this.hass}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></paper-input>
></ha-form>
`;
}

private _valueChanged(ev: CustomEvent): void {
handleChangeEvent(this, ev);
ev.stopPropagation();
const newTrigger = ev.detail.value;
fireEvent(this, "value-changed", { value: newTrigger });
}

private _computeLabelCallback = (schema: HaFormSchema): string =>
this.hass.localize(
`ui.panel.config.automation.editor.triggers.type.mqtt.${schema.name}`
);
}

declare global {
Expand Down

0 comments on commit ac90bb7

Please sign in to comment.