Skip to content

Commit dc00839

Browse files
committed
add weekday to time condition editor
1 parent d7448ec commit dc00839

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

src/data/automation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export interface TimeCondition {
170170
condition: "time";
171171
after: string;
172172
before: string;
173+
weekday: string[];
173174
}
174175

175176
export interface TemplateCondition {

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

+78-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?.includes(day)}
164+
@change=${this._dayValueChanged}
165+
>
166+
</ha-switch>
167+
</ha-formfield>
168+
`
169+
)}
131170
`;
132171
}
133172

@@ -143,4 +182,41 @@ 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+
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+
}
146222
}

src/translations/en.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,16 @@
11111111
"type_input": "[%key:ui::panel::config::automation::editor::triggers::type::time::type_input%]",
11121112
"label": "[%key:ui::panel::config::automation::editor::triggers::type::time::label%]",
11131113
"after": "After",
1114-
"before": "Before"
1114+
"before": "Before",
1115+
"weekdays": {
1116+
"mon": "Monday",
1117+
"tue": "Tuesday",
1118+
"wed": "Wednesday",
1119+
"thu": "Thursday",
1120+
"fri": "Friday",
1121+
"sat": "Saturday",
1122+
"sun": "Sunday"
1123+
}
11151124
},
11161125
"zone": {
11171126
"label": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",

0 commit comments

Comments
 (0)