Skip to content

Commit

Permalink
HA Trigger to HA Form (#11645)
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 9912d42 commit ed84ce9
Showing 1 changed file with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
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 {
@property({ attribute: false }) public hass!: HomeAssistant;

@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"],
};
}

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;
Expand Down

0 comments on commit ed84ce9

Please sign in to comment.