@@ -2,18 +2,21 @@ import type { PropertyValues } from "lit";
22import { html , LitElement , nothing } from "lit" ;
33import { customElement , property , state } from "lit/decorators" ;
44import memoizeOne from "memoize-one" ;
5+ import { firstWeekdayIndex } from "../../../../../common/datetime/first_weekday" ;
56import { fireEvent } from "../../../../../common/dom/fire_event" ;
67import type { LocalizeFunc } from "../../../../../common/translations/localize" ;
78import "../../../../../components/ha-form/ha-form" ;
89import type { SchemaUnion } from "../../../../../components/ha-form/types" ;
910import type { TimeTrigger } from "../../../../../data/automation" ;
11+ import type { FrontendLocaleData } from "../../../../../data/translation" ;
1012import type { HomeAssistant } from "../../../../../types" ;
1113import type { TriggerElement } from "../ha-automation-trigger-row" ;
1214import { computeDomain } from "../../../../../common/entity/compute_domain" ;
1315
1416const MODE_TIME = "time" ;
1517const MODE_ENTITY = "entity" ;
1618const VALID_DOMAINS = [ "sensor" , "input_datetime" ] ;
19+ const DAYS = [ "sun" , "mon" , "tue" , "wed" , "thu" , "fri" , "sat" ] as const ;
1720
1821@customElement ( "ha-automation-trigger-time" )
1922export class HaTimeTrigger extends LitElement implements TriggerElement {
@@ -35,9 +38,14 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
3538 private _schema = memoizeOne (
3639 (
3740 localize : LocalizeFunc ,
41+ locale : FrontendLocaleData ,
3842 inputMode : typeof MODE_TIME | typeof MODE_ENTITY
39- ) =>
40- [
43+ ) => {
44+ const dayIndex = firstWeekdayIndex ( locale ) ;
45+ const sortedDays = DAYS . slice ( dayIndex , DAYS . length ) . concat (
46+ DAYS . slice ( 0 , dayIndex )
47+ ) ;
48+ return [
4149 {
4250 name : "mode" ,
4351 type : "select" ,
@@ -73,7 +81,21 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
7381 } ,
7482 { name : "offset" , selector : { text : { } } } ,
7583 ] as const ) ) ,
76- ] as const
84+ {
85+ type : "multi_select" ,
86+ name : "weekday" ,
87+ options : sortedDays . map (
88+ ( day ) =>
89+ [
90+ day ,
91+ localize (
92+ `ui.panel.config.automation.editor.triggers.type.time.weekdays.${ day } `
93+ ) ,
94+ ] as const
95+ ) ,
96+ } ,
97+ ] as const ;
98+ }
7799 ) ;
78100
79101 public willUpdate ( changedProperties : PropertyValues ) {
@@ -95,12 +117,14 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
95117 inputMode : undefined | typeof MODE_ENTITY | typeof MODE_TIME ,
96118 at :
97119 | string
98- | { entity_id : string | undefined ; offset ?: string | undefined }
120+ | { entity_id : string | undefined ; offset ?: string | undefined } ,
121+ weekday : string | string [ ] | undefined
99122 ) : {
100123 mode : typeof MODE_TIME | typeof MODE_ENTITY ;
101124 entity : string | undefined ;
102125 time : string | undefined ;
103126 offset : string | undefined ;
127+ weekday : string | string [ ] | undefined ;
104128 } => {
105129 const entity =
106130 typeof at === "object"
@@ -116,6 +140,7 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
116140 entity,
117141 time,
118142 offset,
143+ weekday,
119144 } ;
120145 }
121146 ) ;
@@ -127,8 +152,12 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
127152 return nothing ;
128153 }
129154
130- const data = this . _data ( this . _inputMode , at ) ;
131- const schema = this . _schema ( this . hass . localize , data . mode ) ;
155+ const data = this . _data ( this . _inputMode , at , this . trigger . weekday ) ;
156+ const schema = this . _schema (
157+ this . hass . localize ,
158+ this . hass . locale ,
159+ data . mode
160+ ) ;
132161
133162 return html `
134163 < ha-form
@@ -146,22 +175,36 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
146175 ev . stopPropagation ( ) ;
147176 const newValue = { ...ev . detail . value } ;
148177 this . _inputMode = newValue . mode ;
178+
179+ const weekday = newValue . weekday ;
180+ delete newValue . weekday ;
181+
149182 if ( newValue . mode === MODE_TIME ) {
150183 delete newValue . entity ;
151184 delete newValue . offset ;
152185 } else {
153186 delete newValue . time ;
154187 }
188+
189+ const triggerUpdate : TimeTrigger = {
190+ ...this . trigger ,
191+ at : newValue . offset
192+ ? {
193+ entity_id : newValue . entity ,
194+ offset : newValue . offset ,
195+ }
196+ : newValue . entity || newValue . time ,
197+ } ;
198+
199+ // Only include weekday if it has a value
200+ if ( weekday && weekday . length > 0 ) {
201+ triggerUpdate . weekday = weekday ;
202+ } else {
203+ delete triggerUpdate . weekday ;
204+ }
205+
155206 fireEvent ( this , "value-changed" , {
156- value : {
157- ...this . trigger ,
158- at : newValue . offset
159- ? {
160- entity_id : newValue . entity ,
161- offset : newValue . offset ,
162- }
163- : newValue . entity || newValue . time ,
164- } ,
207+ value : triggerUpdate ,
165208 } ) ;
166209 }
167210
@@ -173,6 +216,10 @@ export class HaTimeTrigger extends LitElement implements TriggerElement {
173216 return this . hass . localize (
174217 `ui.panel.config.automation.editor.triggers.type.time.at`
175218 ) ;
219+ case "weekday" :
220+ return this . hass . localize (
221+ `ui.panel.config.automation.editor.triggers.type.time.weekday`
222+ ) ;
176223 }
177224 return this . hass . localize (
178225 `ui.panel.config.automation.editor.triggers.type.time.${ schema . name } `
0 commit comments