-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustom-timer-card.js
65 lines (56 loc) · 1.74 KB
/
Custom-timer-card.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class TimerCard extends HTMLElement {
set hass(hass) {
this._hass = hass;
}
setConfig(config) {
this._config = config;
}
startTimer() {
this._timeRemaining = this._durationInput.value;
this._interval = setInterval(() => {
this._timeRemaining--;
this._timeRemainingElement.innerHTML = this._timeRemaining;
if (this._timeRemaining <= 0) {
this._hass.callService('light', 'turn_off', {
entity_id: this._config.entity,
});
clearInterval(this._interval);
}
}, 60000);
}
stopTimer() {
clearInterval(this._interval);
}
// Returns an object describing the properites of this element.
static get properties() {
return { hass: Object, config: Object };
}
// Use the updated() callback to handle changes to element properties.
updated(changedProperties) {
if (changedProperties.has('config')) {
this.stopTimer();
this.startTimer();
}
}
// Use the render() callback to create and return the component's template.
render() {
this._durationInput = document.createElement('input');
this._durationInput.type = 'number';
this._durationInput.min = 1;
this._durationInput.value = this._config.duration;
this._durationInput.addEventListener('change', () => this.startTimer());
this._timeRemainingElement = document.createElement('div');
this._timeRemainingElement.innerHTML = this._durationInput.value;
return html`
<style>
/* voeg hier de stijl voor je kaart toe */
</style>
<div>
<!-- voeg hier de HTML-markup voor je kaart toe -->
${this._durationInput}
${this._timeRemainingElement}
</div>
`;
}
}
customElements.define('custom-timer-card', TimerCard);