mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 08:16:36 +00:00
HA Trigger to HA Form (#11645)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
9912d427f2
commit
ed84ce9692
@ -1,11 +1,12 @@
|
||||
import "../../../../../components/ha-form/ha-form";
|
||||
import { css, html, LitElement } from "lit";
|
||||
import { customElement, property } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { fireEvent } from "../../../../../common/dom/fire_event";
|
||||
import type { HaRadio } from "../../../../../components/ha-radio";
|
||||
import type { HassTrigger } from "../../../../../data/automation";
|
||||
import type { HomeAssistant } from "../../../../../types";
|
||||
import "../../../../../components/ha-formfield";
|
||||
import "../../../../../components/ha-radio";
|
||||
import type { HaFormSchema } from "../../../../../components/ha-form/types";
|
||||
import type { LocalizeFunc } from "../../../../../common/translations/localize";
|
||||
|
||||
@customElement("ha-automation-trigger-homeassistant")
|
||||
export class HaHassTrigger extends LitElement {
|
||||
@ -13,6 +14,28 @@ export class HaHassTrigger extends LitElement {
|
||||
|
||||
@property({ attribute: false }) public trigger!: HassTrigger;
|
||||
|
||||
private _schema = memoizeOne((localize: LocalizeFunc) => [
|
||||
{
|
||||
name: "event",
|
||||
type: "select",
|
||||
required: true,
|
||||
options: [
|
||||
[
|
||||
"start",
|
||||
localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.homeassistant.start"
|
||||
),
|
||||
],
|
||||
[
|
||||
"shutdown",
|
||||
localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.homeassistant.shutdown"
|
||||
),
|
||||
],
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
public static get defaultConfig() {
|
||||
return {
|
||||
event: "start" as HassTrigger["event"],
|
||||
@ -20,50 +43,28 @@ export class HaHassTrigger extends LitElement {
|
||||
}
|
||||
|
||||
protected render() {
|
||||
const { event } = this.trigger;
|
||||
return html`
|
||||
<label id="eventlabel">
|
||||
${this.hass.localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.homeassistant.event"
|
||||
)}
|
||||
<ha-formfield
|
||||
.label=${this.hass.localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.homeassistant.start"
|
||||
)}
|
||||
>
|
||||
<ha-radio
|
||||
name="event"
|
||||
value="start"
|
||||
.checked=${event === "start"}
|
||||
@change=${this._radioGroupPicked}
|
||||
></ha-radio>
|
||||
</ha-formfield>
|
||||
<ha-formfield
|
||||
.label=${this.hass.localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.homeassistant.shutdown"
|
||||
)}
|
||||
>
|
||||
<ha-radio
|
||||
name="event"
|
||||
value="shutdown"
|
||||
.checked=${event === "shutdown"}
|
||||
@change=${this._radioGroupPicked}
|
||||
></ha-radio>
|
||||
</ha-formfield>
|
||||
</label>
|
||||
<ha-form
|
||||
.schema=${this._schema(this.hass.localize)}
|
||||
.data=${this.trigger}
|
||||
.hass=${this.hass}
|
||||
.computeLabel=${this._computeLabelCallback}
|
||||
@value-changed=${this._valueChanged}
|
||||
></ha-form>
|
||||
`;
|
||||
}
|
||||
|
||||
private _radioGroupPicked(ev) {
|
||||
private _valueChanged(ev: CustomEvent): void {
|
||||
ev.stopPropagation();
|
||||
fireEvent(this, "value-changed", {
|
||||
value: {
|
||||
...this.trigger,
|
||||
event: (ev.target as HaRadio).value,
|
||||
},
|
||||
});
|
||||
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.geo_location.${schema.name}`
|
||||
);
|
||||
|
||||
static styles = css`
|
||||
label {
|
||||
display: flex;
|
||||
|
Loading…
x
Reference in New Issue
Block a user