6
6
internalProperty ,
7
7
LitElement ,
8
8
property ,
9
+ CSSResult ,
10
+ css ,
9
11
} from "lit-element" ;
10
12
import "../../../../../components/ha-formfield" ;
11
13
import "../../../../../components/ha-radio" ;
@@ -15,14 +17,31 @@ import {
15
17
ConditionElement ,
16
18
handleChangeEvent ,
17
19
} from "../ha-automation-condition-row" ;
20
+ import { HaSwitch } from "../../../../../components/ha-switch" ;
21
+ import { computeRTLDirection } from "../../../../../common/util/compute_rtl" ;
22
+ import { fireEvent } from "../../../../../common/dom/fire_event" ;
18
23
19
24
const includeDomains = [ "input_datetime" ] ;
20
25
26
+ const DAYS = {
27
+ mon : 1 ,
28
+ tue : 2 ,
29
+ wed : 3 ,
30
+ thu : 4 ,
31
+ fri : 5 ,
32
+ sat : 6 ,
33
+ sun : 7 ,
34
+ } ;
35
+
36
+ interface WeekdayHaSwitch extends HaSwitch {
37
+ day : string ;
38
+ }
39
+
21
40
@customElement ( "ha-automation-condition-time" )
22
41
export class HaTimeCondition extends LitElement implements ConditionElement {
23
42
@property ( { attribute : false } ) public hass ! : HomeAssistant ;
24
43
25
- @property ( ) public condition ! : TimeCondition ;
44
+ @property ( { attribute : false } ) public condition ! : TimeCondition ;
26
45
27
46
@internalProperty ( ) private _inputModeBefore ?: boolean ;
28
47
@@ -33,7 +52,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
33
52
}
34
53
35
54
protected render ( ) {
36
- const { after, before } = this . condition ;
55
+ const { after, before, weekday } = this . condition ;
37
56
38
57
const inputModeBefore =
39
58
this . _inputModeBefore ?? before ?. startsWith ( "input_datetime." ) ;
@@ -128,6 +147,26 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
128
147
.value = ${ before ?. startsWith ( "input_datetime." ) ? "" : before }
129
148
@value-changed = ${ this . _valueChanged }
130
149
> </ paper- input> ` }
150
+ ${ Object . keys ( DAYS ) . map (
151
+ ( day ) => html `
152
+ <ha- for mfield
153
+ alignEnd
154
+ spaceBetween
155
+ class= "weekday-toggle"
156
+ .label = ${ this . hass ! . localize (
157
+ `ui.panel.config.automation.editor.conditions.type.time.weekdays.${ day } `
158
+ ) }
159
+ .dir = ${ computeRTLDirection ( this . hass ! ) }
160
+ >
161
+ <ha- switch
162
+ .day = ${ day }
163
+ .checked = ${ weekday ?. includes ( day ) }
164
+ @change = ${ this . _dayValueChanged }
165
+ >
166
+ </ ha- switch>
167
+ </ ha- for mfield>
168
+ `
169
+ ) }
131
170
` ;
132
171
}
133
172
@@ -143,4 +182,41 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
143
182
private _valueChanged ( ev : CustomEvent ) : void {
144
183
handleChangeEvent ( this , ev ) ;
145
184
}
185
+
186
+ private _dayValueChanged ( ev : CustomEvent ) : void {
187
+ ev . stopPropagation ( ) ;
188
+
189
+ const daySwitch = ev . currentTarget as WeekdayHaSwitch ;
190
+
191
+ let days = this . condition . weekday || [ ] ;
192
+
193
+ if ( daySwitch . checked ) {
194
+ days . push ( daySwitch . day ) ;
195
+ } else {
196
+ days = days . filter ( ( d ) => d !== daySwitch . day ) ;
197
+ }
198
+
199
+ days . sort ( ( a : string , b : string ) => DAYS [ a ] - DAYS [ b ] ) ;
200
+
201
+ this . condition . weekday = days ;
202
+
203
+ fireEvent ( this , "value-changed" , {
204
+ value : this . condition ,
205
+ } ) ;
206
+ }
207
+
208
+ static get styles ( ) : CSSResult {
209
+ return css `
210
+ .weekday-toggle {
211
+ display : flex;
212
+ height : 40px ;
213
+ }
214
+ ` ;
215
+ }
216
+ }
217
+
218
+ declare global {
219
+ interface HTMLElementTagNameMap {
220
+ "ha-automation-condition-time" : HaTimeCondition ;
221
+ }
146
222
}
0 commit comments