Skip to content

Commit

Permalink
Convert Sun to Ha Form (#11647)
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 ac90bb7 commit 76f574f
Showing 1 changed file with 40 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import "@polymer/paper-input/paper-input";
import { css, html, LitElement } from "lit";
import { html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../../common/dom/fire_event";
import "../../../../../components/ha-radio";
import "../../../../../components/ha-formfield";
import type { HaRadio } from "../../../../../components/ha-radio";
import type { SunTrigger } from "../../../../../data/automation";
import type { HomeAssistant } from "../../../../../types";
import {
handleChangeEvent,
TriggerElement,
} from "../ha-automation-trigger-row";
import type { TriggerElement } from "../ha-automation-trigger-row";
import type { HaFormSchema } from "../../../../../components/ha-form/types";
import type { LocalizeFunc } from "../../../../../common/translations/localize";

@customElement("ha-automation-trigger-sun")
export class HaSunTrigger extends LitElement implements TriggerElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public trigger!: SunTrigger;

private _schema = memoizeOne((localize: LocalizeFunc) => [
{
name: "event",
type: "select",
required: true,
options: [
[
"sunrise",
localize(
"ui.panel.config.automation.editor.triggers.type.sun.sunrise"
),
],
[
"sunset",
localize(
"ui.panel.config.automation.editor.triggers.type.sun.sunset"
),
],
],
},
{ name: "offset", selector: { text: {} } },
]);

public static get defaultConfig() {
return {
event: "sunrise" as SunTrigger["event"],
Expand All @@ -26,69 +45,27 @@ export class HaSunTrigger extends LitElement implements TriggerElement {
}

protected render() {
const { offset, event } = this.trigger;
return html`
<label>
${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.sun.event"
)}
<ha-formfield
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.sun.sunrise"
)}
>
<ha-radio
name="event"
value="sunrise"
.checked=${event === "sunrise"}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
<ha-formfield
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.sun.sunset"
)}
>
<ha-radio
name="event"
value="sunset"
.checked=${event === "sunset"}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
</label>
<paper-input
.label=${this.hass.localize(
"ui.panel.config.automation.editor.triggers.type.sun.offset"
)}
name="offset"
.value=${offset}
<ha-form
.schema=${this._schema(this.hass.localize)}
.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);
}

private _radioGroupPicked(ev) {
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 });
}

static styles = css`
label {
display: flex;
align-items: center;
}
`;
private _computeLabelCallback = (schema: HaFormSchema): string =>
this.hass.localize(
`ui.panel.config.automation.editor.triggers.type.sun.${schema.name}`
);
}

declare global {
Expand Down

0 comments on commit 76f574f

Please sign in to comment.