-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display live remaining time for timer on tile card (#21290)
Display timer by default on tile card
- Loading branch information
Showing
5 changed files
with
101 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { HassEntity } from "home-assistant-js-websocket"; | ||
import { PropertyValues, ReactiveElement } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { computeDisplayTimer, timerTimeRemaining } from "../data/timer"; | ||
import type { HomeAssistant } from "../types"; | ||
|
||
@customElement("state-display-timer") | ||
class StateDisplayTimer extends ReactiveElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public stateObj!: HassEntity; | ||
|
||
@state() private timeRemaining?: number; | ||
|
||
private _updateRemaining: any; | ||
|
||
protected createRenderRoot() { | ||
return this; | ||
} | ||
|
||
protected update(changedProps: PropertyValues) { | ||
super.update(changedProps); | ||
this.innerHTML = | ||
computeDisplayTimer(this.hass, this.stateObj, this.timeRemaining) ?? "-"; | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
if (this.stateObj) { | ||
this._startInterval(this.stateObj); | ||
} | ||
} | ||
|
||
disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
this._clearInterval(); | ||
} | ||
|
||
protected willUpdate(changedProp: PropertyValues): void { | ||
super.willUpdate(changedProp); | ||
if (changedProp.has("stateObj")) { | ||
this._startInterval(this.stateObj); | ||
} | ||
} | ||
|
||
private _clearInterval() { | ||
if (this._updateRemaining) { | ||
clearInterval(this._updateRemaining); | ||
this._updateRemaining = null; | ||
} | ||
} | ||
|
||
private _startInterval(stateObj: HassEntity) { | ||
this._clearInterval(); | ||
this._calculateRemaining(stateObj); | ||
|
||
if (stateObj.state === "active") { | ||
this._updateRemaining = setInterval( | ||
() => this._calculateRemaining(this.stateObj), | ||
1000 | ||
); | ||
} | ||
} | ||
|
||
private _calculateRemaining(stateObj: HassEntity) { | ||
this.timeRemaining = timerTimeRemaining(stateObj); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"state-display-timer": StateDisplayTimer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters