Skip to content

Commit dfb2a71

Browse files
authored
Add weekday to time contidion editor (#6848)
1 parent e9141d8 commit dfb2a71

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

src/data/automation.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ export interface ZoneCondition {
168168

169169
export interface TimeCondition {
170170
condition: "time";
171-
after: string;
172-
before: string;
171+
after?: string;
172+
before?: string;
173+
weekday?: string | string[];
173174
}
174175

175176
export interface TemplateCondition {

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

+82-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
internalProperty,
77
LitElement,
88
property,
9+
CSSResult,
10+
css,
911
} from "lit-element";
1012
import "../../../../../components/ha-formfield";
1113
import "../../../../../components/ha-radio";
@@ -15,14 +17,31 @@ import {
1517
ConditionElement,
1618
handleChangeEvent,
1719
} 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";
1823

1924
const includeDomains = ["input_datetime"];
2025

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+
2140
@customElement("ha-automation-condition-time")
2241
export class HaTimeCondition extends LitElement implements ConditionElement {
2342
@property({ attribute: false }) public hass!: HomeAssistant;
2443

25-
@property() public condition!: TimeCondition;
44+
@property({ attribute: false }) public condition!: TimeCondition;
2645

2746
@internalProperty() private _inputModeBefore?: boolean;
2847

@@ -33,7 +52,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
3352
}
3453

3554
protected render() {
36-
const { after, before } = this.condition;
55+
const { after, before, weekday } = this.condition;
3756

3857
const inputModeBefore =
3958
this._inputModeBefore ?? before?.startsWith("input_datetime.");
@@ -128,6 +147,26 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
128147
.value=${before?.startsWith("input_datetime.") ? "" : before}
129148
@value-changed=${this._valueChanged}
130149
></paper-input>`}
150+
${Object.keys(DAYS).map(
151+
(day) => html`
152+
<ha-formfield
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 || weekday === day || weekday.includes(day)}
164+
@change=${this._dayValueChanged}
165+
>
166+
</ha-switch>
167+
</ha-formfield>
168+
`
169+
)}
131170
`;
132171
}
133172

@@ -143,4 +182,45 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
143182
private _valueChanged(ev: CustomEvent): void {
144183
handleChangeEvent(this, ev);
145184
}
185+
186+
private _dayValueChanged(ev: CustomEvent): void {
187+
const daySwitch = ev.currentTarget as WeekdayHaSwitch;
188+
189+
let days: string[];
190+
191+
if (!this.condition.weekday) {
192+
days = Object.keys(DAYS);
193+
} else {
194+
days = !Array.isArray(this.condition.weekday)
195+
? [this.condition.weekday]
196+
: this.condition.weekday;
197+
}
198+
199+
if (daySwitch.checked) {
200+
days.push(daySwitch.day);
201+
} else {
202+
days = days.filter((d) => d !== daySwitch.day);
203+
}
204+
205+
days.sort((a: string, b: string) => DAYS[a] - DAYS[b]);
206+
207+
fireEvent(this, "value-changed", {
208+
value: { ...this.condition, weekday: days },
209+
});
210+
}
211+
212+
static get styles(): CSSResult {
213+
return css`
214+
.weekday-toggle {
215+
display: flex;
216+
height: 40px;
217+
}
218+
`;
219+
}
220+
}
221+
222+
declare global {
223+
interface HTMLElementTagNameMap {
224+
"ha-automation-condition-time": HaTimeCondition;
225+
}
146226
}

src/translations/en.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,16 @@
11391139
"type_input": "[%key:ui::panel::config::automation::editor::triggers::type::time::type_input%]",
11401140
"label": "[%key:ui::panel::config::automation::editor::triggers::type::time::label%]",
11411141
"after": "After",
1142-
"before": "Before"
1142+
"before": "Before",
1143+
"weekdays": {
1144+
"mon": "Monday",
1145+
"tue": "Tuesday",
1146+
"wed": "Wednesday",
1147+
"thu": "Thursday",
1148+
"fri": "Friday",
1149+
"sat": "Saturday",
1150+
"sun": "Sunday"
1151+
}
11431152
},
11441153
"zone": {
11451154
"label": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",

0 commit comments

Comments
 (0)