diff --git a/src/common/entity/timer_time_remaining.ts b/src/common/entity/timer_time_remaining.ts deleted file mode 100644 index 5b2f54654d..0000000000 --- a/src/common/entity/timer_time_remaining.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { HassEntity } from "home-assistant-js-websocket"; -import durationToSeconds from "../datetime/duration_to_seconds"; - -export const timerTimeRemaining = ( - stateObj: HassEntity -): undefined | number => { - if (!stateObj.attributes.remaining) { - return undefined; - } - let timeRemaining = durationToSeconds(stateObj.attributes.remaining); - - if (stateObj.state === "active") { - const now = new Date().getTime(); - const madeActive = new Date(stateObj.last_changed).getTime(); - timeRemaining = Math.max(timeRemaining - (now - madeActive) / 1000, 0); - } - - return timeRemaining; -}; diff --git a/src/components/entity/ha-state-label-badge.ts b/src/components/entity/ha-state-label-badge.ts index 7bed0b3002..eaa8f89c53 100644 --- a/src/components/entity/ha-state-label-badge.ts +++ b/src/components/entity/ha-state-label-badge.ts @@ -15,7 +15,7 @@ import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { computeStateName } from "../../common/entity/compute_state_name"; import { domainIcon } from "../../common/entity/domain_icon"; import { stateIcon } from "../../common/entity/state_icon"; -import { timerTimeRemaining } from "../../common/entity/timer_time_remaining"; +import { timerTimeRemaining } from "../../data/timer"; import { formatNumber } from "../../common/string/format_number"; import { UNAVAILABLE, UNKNOWN } from "../../data/entity"; import { HomeAssistant } from "../../types"; diff --git a/src/data/timer.ts b/src/data/timer.ts index a963d64ea3..cff601b60a 100644 --- a/src/data/timer.ts +++ b/src/data/timer.ts @@ -1,7 +1,11 @@ import { + HassEntity, HassEntityAttributeBase, HassEntityBase, } from "home-assistant-js-websocket"; +import durationToSeconds from "../common/datetime/duration_to_seconds"; +import secondsToDuration from "../common/datetime/seconds_to_duration"; +import { computeStateDisplay } from "../common/entity/compute_state_display"; import { HomeAssistant } from "../types"; export type TimerEntity = HassEntityBase & { @@ -55,3 +59,46 @@ export const deleteTimer = (hass: HomeAssistant, id: string) => type: "timer/delete", timer_id: id, }); + +export const timerTimeRemaining = ( + stateObj: HassEntity +): undefined | number => { + if (!stateObj.attributes.remaining) { + return undefined; + } + let timeRemaining = durationToSeconds(stateObj.attributes.remaining); + + if (stateObj.state === "active") { + const now = new Date().getTime(); + const madeActive = new Date(stateObj.last_changed).getTime(); + timeRemaining = Math.max(timeRemaining - (now - madeActive) / 1000, 0); + } + + return timeRemaining; +}; + +export const computeDisplayTimer = ( + hass: HomeAssistant, + stateObj: HassEntity, + timeRemaining?: number +): string | null => { + if (!stateObj) { + return null; + } + + if (stateObj.state === "idle" || timeRemaining === 0) { + return computeStateDisplay(hass.localize, stateObj, hass.locale); + } + + let display = secondsToDuration(timeRemaining || 0); + + if (stateObj.state === "paused") { + display = `${display} (${computeStateDisplay( + hass.localize, + stateObj, + hass.locale + )})`; + } + + return display; +}; diff --git a/src/panels/lovelace/entity-rows/hui-timer-entity-row.ts b/src/panels/lovelace/entity-rows/hui-timer-entity-row.ts index 4ebc4ca14b..a06eeb9905 100644 --- a/src/panels/lovelace/entity-rows/hui-timer-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-timer-entity-row.ts @@ -1,8 +1,7 @@ import { HassEntity } from "home-assistant-js-websocket"; import { html, LitElement, PropertyValues, TemplateResult } from "lit"; import { customElement, property, state } from "lit/decorators"; -import secondsToDuration from "../../../common/datetime/seconds_to_duration"; -import { timerTimeRemaining } from "../../../common/entity/timer_time_remaining"; +import { computeDisplayTimer, timerTimeRemaining } from "../../../data/timer"; import { HomeAssistant } from "../../../types"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import "../components/hui-generic-entity-row"; @@ -58,7 +57,9 @@ class HuiTimerEntityRow extends LitElement { return html` -
${this._computeDisplay(stateObj)}
+
+ ${computeDisplayTimer(this.hass, stateObj, this._timeRemaining)} +
`; } @@ -111,26 +112,6 @@ class HuiTimerEntityRow extends LitElement { private _calculateRemaining(stateObj: HassEntity): void { this._timeRemaining = timerTimeRemaining(stateObj); } - - private _computeDisplay(stateObj: HassEntity): string | null { - if (!stateObj) { - return null; - } - - if (stateObj.state === "idle" || this._timeRemaining === 0) { - return ( - this.hass!.localize(`state.timer.${stateObj.state}`) || stateObj.state - ); - } - - let display = secondsToDuration(this._timeRemaining || 0); - - if (stateObj.state === "paused") { - display += ` (${this.hass!.localize("state.timer.paused")})`; - } - - return display; - } } declare global { diff --git a/src/state-summary/state-card-timer.js b/src/state-summary/state-card-timer.js index 17d39fde5c..a517a52da1 100644 --- a/src/state-summary/state-card-timer.js +++ b/src/state-summary/state-card-timer.js @@ -2,9 +2,8 @@ import "@polymer/iron-flex-layout/iron-flex-layout-classes"; import { html } from "@polymer/polymer/lib/utils/html-tag"; /* eslint-plugin-disable lit */ import { PolymerElement } from "@polymer/polymer/polymer-element"; -import secondsToDuration from "../common/datetime/seconds_to_duration"; -import { timerTimeRemaining } from "../common/entity/timer_time_remaining"; import "../components/entity/state-info"; +import { computeDisplayTimer, timerTimeRemaining } from "../data/timer"; class StateCardTimer extends PolymerElement { static get template() { @@ -18,6 +17,7 @@ class StateCardTimer extends PolymerElement { margin-left: 16px; text-align: right; line-height: 40px; + white-space: nowrap; } @@ -90,10 +90,8 @@ class StateCardTimer extends PolymerElement { this.timeRemaining = timerTimeRemaining(stateObj); } - _displayState(time, stateObj) { - return time - ? secondsToDuration(time) - : this.hass.localize(`state.timer.${stateObj.state}`) || stateObj.state; + _displayState(timeRemaining, stateObj) { + return computeDisplayTimer(this.hass, stateObj, timeRemaining); } } customElements.define("state-card-timer", StateCardTimer); diff --git a/test-mocha/common/entity/timer_time_remaining_test.ts b/test-mocha/common/entity/timer_time_remaining_test.ts index 3fb7632869..8c1ce07e00 100644 --- a/test-mocha/common/entity/timer_time_remaining_test.ts +++ b/test-mocha/common/entity/timer_time_remaining_test.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import * as sinon from "sinon"; -import { timerTimeRemaining } from "../../../src/common/entity/timer_time_remaining"; +import { timerTimeRemaining } from "../../../src/data/timer"; describe("timerTimeRemaining", () => { it("works with idle timers", () => {