Skip to content

Commit b49b8e3

Browse files
frenckNoRi2909silamon
authored andcommitted
Add weekdays to time trigger (#25908)
* Add weekdays to time trigger * Update src/translations/en.json Co-authored-by: Norbert Rittel <[email protected]> * Localization changes --------- Co-authored-by: Norbert Rittel <[email protected]> Co-authored-by: Simon Lamon <[email protected]>
1 parent c013c5e commit b49b8e3

File tree

4 files changed

+89
-16
lines changed

4 files changed

+89
-16
lines changed

src/data/automation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export interface TagTrigger extends BaseTrigger {
169169
export interface TimeTrigger extends BaseTrigger {
170170
trigger: "time";
171171
at: string | { entity_id: string; offset?: string };
172+
weekday?: string | string[];
172173
}
173174

174175
export interface TemplateTrigger extends BaseTrigger {

src/data/automation_i18n.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,23 @@ const tryDescribeTrigger = (
400400
return `${entityStr}${offsetStr}`;
401401
});
402402

403+
// Handle weekday information if present
404+
let weekdays: string[] = [];
405+
if (trigger.weekday) {
406+
const weekdayArray = ensureArray(trigger.weekday);
407+
if (weekdayArray.length > 0) {
408+
weekdays = weekdayArray.map((day) =>
409+
hass.localize(
410+
`ui.panel.config.automation.editor.triggers.type.time.weekdays.${day}` as any
411+
)
412+
);
413+
}
414+
}
415+
403416
return hass.localize(`${triggerTranslationBaseKey}.time.description.full`, {
404417
time: formatListWithOrs(hass.locale, result),
418+
hasWeekdays: weekdays.length > 0 ? "true" : "false",
419+
weekdays: formatListWithOrs(hass.locale, weekdays),
405420
});
406421
}
407422

src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import type { PropertyValues } from "lit";
22
import { html, LitElement, nothing } from "lit";
33
import { customElement, property, state } from "lit/decorators";
44
import memoizeOne from "memoize-one";
5+
import { firstWeekdayIndex } from "../../../../../common/datetime/first_weekday";
56
import { fireEvent } from "../../../../../common/dom/fire_event";
67
import type { LocalizeFunc } from "../../../../../common/translations/localize";
78
import "../../../../../components/ha-form/ha-form";
89
import type { SchemaUnion } from "../../../../../components/ha-form/types";
910
import type { TimeTrigger } from "../../../../../data/automation";
11+
import type { FrontendLocaleData } from "../../../../../data/translation";
1012
import type { HomeAssistant } from "../../../../../types";
1113
import type { TriggerElement } from "../ha-automation-trigger-row";
1214
import { computeDomain } from "../../../../../common/entity/compute_domain";
1315

1416
const MODE_TIME = "time";
1517
const MODE_ENTITY = "entity";
1618
const VALID_DOMAINS = ["sensor", "input_datetime"];
19+
const DAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] as const;
1720

1821
@customElement("ha-automation-trigger-time")
1922
export 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}`

src/translations/en.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4017,9 +4017,19 @@
40174017
"entity": "Entity with timestamp",
40184018
"offset_by": "offset by {offset}",
40194019
"mode": "Mode",
4020+
"weekday": "Weekdays",
4021+
"weekdays": {
4022+
"mon": "[%key:ui::weekdays::monday%]",
4023+
"tue": "[%key:ui::weekdays::tuesday%]",
4024+
"wed": "[%key:ui::weekdays::wednesday%]",
4025+
"thu": "[%key:ui::weekdays::thursday%]",
4026+
"fri": "[%key:ui::weekdays::friday%]",
4027+
"sat": "[%key:ui::weekdays::saturday%]",
4028+
"sun": "[%key:ui::weekdays::sunday%]"
4029+
},
40204030
"description": {
40214031
"picker": "At a specific time, or on a specific date.",
4022-
"full": "When the time is equal to {time}"
4032+
"full": "When the time is equal to {time}{hasWeekdays, select, \n true { on {weekdays}} \n other {}\n}"
40234033
}
40244034
},
40254035
"time_pattern": {

0 commit comments

Comments
 (0)