Add optional delay

This commit is contained in:
Georgi Gardev
2022-12-01 15:58:01 +02:00
parent 7a536c4ef6
commit ff2d7a226d
5 changed files with 31 additions and 8 deletions
+3
View File
@@ -94,9 +94,11 @@ layout:
``` ```
### Dark theme, embedded layout ### Dark theme, embedded layout
![Dark theme embedded](https://raw.githubusercontent.com/GeorgeSG/lovelace-time-picker-card/master/examples/dark_embedded.png) ![Dark theme embedded](https://raw.githubusercontent.com/GeorgeSG/lovelace-time-picker-card/master/examples/dark_embedded.png)
### Dark theme, thin layout ### Dark theme, thin layout
![Dark theme thin layout](https://raw.githubusercontent.com/GeorgeSG/lovelace-time-picker-card/master/examples/dark_thin.png) ![Dark theme thin layout](https://raw.githubusercontent.com/GeorgeSG/lovelace-time-picker-card/master/examples/dark_thin.png)
### With a custom lovelace theme ### With a custom lovelace theme
@@ -114,6 +116,7 @@ layout:
| hour_mode | `12` or `24` | **Optional** | Hour format. If `12`, card will show AM/PM picker | `24` | | hour_mode | `12` or `24` | **Optional** | Hour format. If `12`, card will show AM/PM picker | `24` |
| hour_step | number | **Optional** | Hour change when clicking arrows | `1` | | hour_step | number | **Optional** | Hour change when clicking arrows | `1` |
| minute_step | number | **Optional** | Minute change when clicking arrows | `5` | | minute_step | number | **Optional** | Minute change when clicking arrows | `5` |
| delay | number | **Optional** | Delay in ms before updating entity | `0` |
| layout | object | **Optional** | Card Layout configuration | `none` | | layout | object | **Optional** | Card Layout configuration | `none` |
| hide | object | **Optional** | Hide object | `none` | | hide | object | **Optional** | Hide object | `none` |
| tap_action | action | **Optional** | Home assistant action to perform on tap | `more-info` | | tap_action | action | **Optional** | Home assistant action to perform on tap | `more-info` |
+1 -1
View File
@@ -17,7 +17,7 @@ lovelace:
- type: module - type: module
url: /hacsfiles/lovelace-state-switch/state-switch.js url: /hacsfiles/lovelace-state-switch/state-switch.js
- type: module - type: module
url: http://localhost:5000/time-picker-card.js url: http://127.0.0.1:5000/time-picker-card.js
dashboards: dashboards:
lovelace-home: lovelace-home:
mode: yaml mode: yaml
+8
View File
@@ -44,6 +44,14 @@ views:
thin: true thin: true
hide: hide:
name: true name: true
- type: custom:time-picker-card
name: delayed
entity: input_datetime.test_date_time
delay: 1000
hour_mode: 12
layout:
hour_mode: single
thin: true
- type: custom:time-picker-card - type: custom:time-picker-card
entity: input_datetime.test_date_time entity: input_datetime.test_date_time
+18 -7
View File
@@ -52,6 +52,8 @@ export class TimePickerCard extends LitElement implements LovelaceCard {
@property() private time!: Time; @property() private time!: Time;
@property() private period!: Period; @property() private period!: Period;
private bounce: number | undefined;
private get entity(): HassEntity | undefined { private get entity(): HassEntity | undefined {
return this.hass.states[this.config.entity]; return this.hass.states[this.config.entity];
} }
@@ -197,20 +199,20 @@ export class TimePickerCard extends LitElement implements LovelaceCard {
<time-unit <time-unit
.unit=${this.time.hour} .unit=${this.time.hour}
@stepChange=${this.onHourStepChange} @stepChange=${this.onHourStepChange}
@update=${this.callHassService} @update=${this.debouncedCallHassService}
></time-unit> ></time-unit>
<div class="time-separator">:</div> <div class="time-separator">:</div>
<time-unit <time-unit
.unit=${this.time.minute} .unit=${this.time.minute}
@stepChange=${this.onMinuteStepChange} @stepChange=${this.onMinuteStepChange}
@update=${this.callHassService} @update=${this.debouncedCallHassService}
></time-unit> ></time-unit>
${this.config.hide?.seconds === false ${this.config.hide?.seconds === false
? html`<div class="time-separator">:</div> ? html`<div class="time-separator">:</div>
<time-unit <time-unit
.unit=${this.time.second} .unit=${this.time.second}
@stepChange=${this.onSecondStepChange} @stepChange=${this.onSecondStepChange}
@update=${this.callHassService} @update=${this.debouncedCallHassService}
></time-unit>` ></time-unit>`
: ''} : ''}
${this.shouldShowPeriod ${this.shouldShowPeriod
@@ -248,22 +250,31 @@ export class TimePickerCard extends LitElement implements LovelaceCard {
private onPeriodToggle(): void { private onPeriodToggle(): void {
this.time.hour.togglePeriod(); this.time.hour.togglePeriod();
this.callHassService(); this.debouncedCallHassService();
} }
private onHourStepChange(event: CustomEvent): void { private onHourStepChange(event: CustomEvent): void {
this.time.hourStep(event.detail.direction); this.time.hourStep(event.detail.direction);
this.callHassService(); this.debouncedCallHassService();
} }
private onMinuteStepChange(event: CustomEvent): void { private onMinuteStepChange(event: CustomEvent): void {
this.time.minuteStep(event.detail.direction); this.time.minuteStep(event.detail.direction);
this.callHassService(); this.debouncedCallHassService();
} }
private onSecondStepChange(event: CustomEvent): void { private onSecondStepChange(event: CustomEvent): void {
this.time.secondStep(event.detail.direction); this.time.secondStep(event.detail.direction);
this.callHassService(); this.debouncedCallHassService();
}
private debouncedCallHassService(): void {
if (this.config.delay) {
clearTimeout(this.bounce);
this.bounce = setTimeout(() => this.callHassService(), this.config.delay, this);
} else {
this.callHassService();
}
} }
private callHassService(): Promise<void> { private callHassService(): Promise<void> {
+1
View File
@@ -8,6 +8,7 @@ export interface TimePickerCardConfig extends LovelaceCardConfig {
hour_step?: number; hour_step?: number;
minute_step?: number; minute_step?: number;
second_step?: number; second_step?: number;
delay?: number;
layout?: TimePickerLayoutConfig; layout?: TimePickerLayoutConfig;
hide?: TimePickerHideConfig; hide?: TimePickerHideConfig;
tap_action?: ActionConfig; tap_action?: ActionConfig;