From cbd01f2d68d9afae94c1bcd3b22f3139db2331d7 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Fri, 6 Sep 2019 22:13:05 -0500 Subject: [PATCH] Create more-for timer with action buttons (#3621) * Create more-for timer with action buttons Closes https://github.com/home-assistant/home-assistant-polymer/issues/3594 * center actions * Address review comments * address review comments --- src/common/const.ts | 1 + src/data/timer.ts | 11 ++ .../more-info/controls/more-info-content.ts | 1 + .../more-info/controls/more-info-timer.ts | 104 ++++++++++++++++++ src/translations/en.json | 8 ++ 5 files changed, 125 insertions(+) create mode 100644 src/data/timer.ts create mode 100644 src/dialogs/more-info/controls/more-info-timer.ts diff --git a/src/common/const.ts b/src/common/const.ts index 961ffd9d2e..c1f27c4e9d 100644 --- a/src/common/const.ts +++ b/src/common/const.ts @@ -45,6 +45,7 @@ export const DOMAINS_WITH_MORE_INFO = [ "media_player", "script", "sun", + "timer", "updater", "vacuum", "water_heater", diff --git a/src/data/timer.ts b/src/data/timer.ts new file mode 100644 index 0000000000..e55b936d15 --- /dev/null +++ b/src/data/timer.ts @@ -0,0 +1,11 @@ +import { + HassEntityBase, + HassEntityAttributeBase, +} from "home-assistant-js-websocket"; + +export type TimerEntity = HassEntityBase & { + attributes: HassEntityAttributeBase & { + duration: string; + remaining: string; + }; +}; diff --git a/src/dialogs/more-info/controls/more-info-content.ts b/src/dialogs/more-info/controls/more-info-content.ts index 2d0f0e4cc7..51dfab0c68 100644 --- a/src/dialogs/more-info/controls/more-info-content.ts +++ b/src/dialogs/more-info/controls/more-info-content.ts @@ -21,6 +21,7 @@ import "./more-info-lock"; import "./more-info-media_player"; import "./more-info-script"; import "./more-info-sun"; +import "./more-info-timer"; import "./more-info-updater"; import "./more-info-vacuum"; import "./more-info-water_heater"; diff --git a/src/dialogs/more-info/controls/more-info-timer.ts b/src/dialogs/more-info/controls/more-info-timer.ts new file mode 100644 index 0000000000..e0ce0c5bb5 --- /dev/null +++ b/src/dialogs/more-info/controls/more-info-timer.ts @@ -0,0 +1,104 @@ +import { + LitElement, + html, + TemplateResult, + CSSResult, + css, + property, + PropertyValues, + customElement, +} from "lit-element"; +import "@material/mwc-button"; + +import { HomeAssistant } from "../../../types"; +import { TimerEntity } from "../../../data/timer"; + +@customElement("more-info-timer") +class MoreInfoTimer extends LitElement { + @property() public hass!: HomeAssistant; + + @property() public stateObj?: TimerEntity; + + protected render(): TemplateResult | void { + if (!this.hass || !this.stateObj) { + return html``; + } + + return html` + +
+ ${this.stateObj.state === "idle" || this.stateObj.state === "paused" + ? html` + + ${this.hass!.localize("ui.card.timer.actions.start")} + + ` + : ""} + ${this.stateObj.state === "active" + ? html` + + ${this.hass!.localize("ui.card.timer.actions.pause")} + + ` + : ""} + ${this.stateObj.state === "active" || this.stateObj.state === "paused" + ? html` + + ${this.hass!.localize("ui.card.timer.actions.cancel")} + + + ${this.hass!.localize("ui.card.timer.actions.finish")} + + ` + : ""} +
+ `; + } + + protected updated(changedProps: PropertyValues) { + super.updated(changedProps); + if (!changedProps.has("stateObj") || !this.stateObj) { + return; + } + } + + private _handleActionClick(e: MouseEvent): void { + const action = (e.currentTarget as any).action; + this.hass.callService("timer", action, { + entity_id: this.stateObj!.entity_id, + }); + } + + static get styles(): CSSResult { + return css` + .actions { + margin: 0 8px; + padding-top: 20px; + display: flex; + flex-wrap: wrap; + justify-content: center; + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "more-info-timer": MoreInfoTimer; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 90eda4c055..3af209aa68 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -446,6 +446,14 @@ "script": { "execute": "Execute" }, + "timer": { + "actions": { + "start": "start", + "pause": "pause", + "cancel": "cancel", + "finish": "finish" + } + }, "vacuum": { "actions": { "resume_cleaning": "Resume cleaning",