Skip to content

Commit 4ed42ef

Browse files
committed
Setup
1 parent 1f467b9 commit 4ed42ef

File tree

6 files changed

+102
-34
lines changed

6 files changed

+102
-34
lines changed

src/common/datetime/check_time.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@ import { TZDate } from "@date-fns/tz";
22
import { isBefore, isAfter, isWithinInterval } from "date-fns";
33
import type { HomeAssistant } from "../../types";
44
import { TimeZone } from "../../data/translation";
5-
import type { WeekdayShort } from "./weekday";
6-
7-
const WEEKDAY_MAP: Record<number, WeekdayShort> = {
8-
0: "sun",
9-
1: "mon",
10-
2: "tue",
11-
3: "wed",
12-
4: "thu",
13-
5: "fri",
14-
6: "sat",
15-
};
5+
import { WEEKDAY_MAP, type WeekdayShort } from "./weekday";
166

177
/**
188
* Parse a time string (HH:MM or HH:MM:SS) and set it on today's date in the given timezone
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import { getWeekStartByLocale } from "weekstart";
22
import type { FrontendLocaleData } from "../../data/translation";
33
import { FirstWeekday } from "../../data/translation";
4-
import type { WeekdayLong } from "./weekday";
5-
6-
export const weekdays: WeekdayLong[] = [
7-
"sunday",
8-
"monday",
9-
"tuesday",
10-
"wednesday",
11-
"thursday",
12-
"friday",
13-
"saturday",
14-
] as const;
15-
16-
type WeekdayIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
4+
import { WEEKDAYS_LONG, type WeekdayIndex } from "./weekday";
175

186
export const firstWeekdayIndex = (locale: FrontendLocaleData): WeekdayIndex => {
197
if (locale.first_weekday === FirstWeekday.language) {
@@ -24,12 +12,12 @@ export const firstWeekdayIndex = (locale: FrontendLocaleData): WeekdayIndex => {
2412
}
2513
return (getWeekStartByLocale(locale.language) % 7) as WeekdayIndex;
2614
}
27-
return weekdays.includes(locale.first_weekday)
28-
? (weekdays.indexOf(locale.first_weekday) as WeekdayIndex)
15+
return WEEKDAYS_LONG.includes(locale.first_weekday)
16+
? (WEEKDAYS_LONG.indexOf(locale.first_weekday) as WeekdayIndex)
2917
: 1;
3018
};
3119

3220
export const firstWeekday = (locale: FrontendLocaleData) => {
3321
const index = firstWeekdayIndex(locale);
34-
return weekdays[index];
22+
return WEEKDAYS_LONG[index];
3523
};

src/common/datetime/weekday.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type WeekdayIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
2+
13
export type WeekdayShort =
24
| "sun"
35
| "mon"
@@ -15,3 +17,33 @@ export type WeekdayLong =
1517
| "thursday"
1618
| "friday"
1719
| "saturday";
20+
21+
export const WEEKDAYS_SHORT: WeekdayShort[] = [
22+
"sun",
23+
"mon",
24+
"tue",
25+
"wed",
26+
"thu",
27+
"fri",
28+
"sat",
29+
] as const;
30+
31+
export const WEEKDAYS_LONG: WeekdayLong[] = [
32+
"sunday",
33+
"monday",
34+
"tuesday",
35+
"wednesday",
36+
"thursday",
37+
"friday",
38+
"saturday",
39+
] as const;
40+
41+
export const WEEKDAY_MAP: Record<WeekdayIndex, WeekdayShort> = {
42+
0: "sun",
43+
1: "mon",
44+
2: "tue",
45+
3: "wed",
46+
4: "thu",
47+
5: "fri",
48+
6: "sat",
49+
};

src/panels/lovelace/common/validate-condition.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface TimeCondition extends BaseCondition {
5757
condition: "time";
5858
after?: string;
5959
before?: string;
60-
weekday?: WeekdayShort[];
60+
weekdays?: WeekdayShort[];
6161
}
6262

6363
export interface UserCondition extends BaseCondition {
@@ -165,7 +165,7 @@ function checkTimeCondition(condition: TimeCondition, hass: HomeAssistant) {
165165
hass,
166166
condition.after,
167167
condition.before,
168-
condition.weekday
168+
condition.weekdays
169169
);
170170
}
171171

@@ -292,8 +292,11 @@ function validateScreenCondition(condition: ScreenCondition) {
292292
return condition.media_query != null;
293293
}
294294

295-
function validateTimeCondition(_condition: TimeCondition) {
296-
return true;
295+
function validateTimeCondition(condition: TimeCondition) {
296+
return (
297+
(condition.after != null || condition.before != null) &&
298+
condition.weekdays?.every((w: WeekdayShort) => WEEKDAYS_SHORT.includes(w))
299+
);
297300
}
298301

299302
function validateUserCondition(condition: UserCondition) {

src/panels/lovelace/editor/conditions/types/ha-card-condition-time.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1-
import { LitElement, nothing } from "lit";
1+
import { html, LitElement } from "lit";
22
import { customElement, property } from "lit/decorators";
3+
import {
4+
literal,
5+
array,
6+
object,
7+
optional,
8+
string,
9+
assert,
10+
enums,
11+
} from "superstruct";
312
import type { HomeAssistant } from "../../../../../types";
413
import type { TimeCondition } from "../../../common/validate-condition";
14+
import { WEEKDAYS_SHORT } from "../../../../../common/datetime/weekday";
15+
import type { HaFormSchema } from "../../../../../components/ha-form/types";
16+
import { fireEvent } from "../../../../../common/dom/fire_event";
17+
18+
const timeConditionStruct = object({
19+
condition: literal("time"),
20+
after: optional(string()),
21+
before: optional(string()),
22+
weekdays: optional(array(enums(WEEKDAYS_SHORT))),
23+
});
24+
25+
const SCHEMA = [
26+
{ name: "after", selector: { time: {} } },
27+
{ name: "before", selector: { time: {} } },
28+
{
29+
name: "weekdays",
30+
selector: {
31+
select: {
32+
mode: "list",
33+
multiple: true,
34+
options: WEEKDAYS_SHORT,
35+
},
36+
},
37+
},
38+
] as const satisfies HaFormSchema[];
539

640
@customElement("ha-card-condition-time")
741
export class HaCardConditionTime extends LitElement {
@@ -15,8 +49,26 @@ export class HaCardConditionTime extends LitElement {
1549
return { condition: "time" };
1650
}
1751

52+
protected static validateUIConfig(condition: TimeCondition) {
53+
return assert(condition, timeConditionStruct);
54+
}
55+
1856
protected render() {
19-
return nothing;
57+
return html`
58+
<ha-form
59+
.hass=${this.hass}
60+
.data=${this.condition}
61+
.schema=${SCHEMA}
62+
.disabled=${this.disabled}
63+
@value-changed=${this._valueChanged}
64+
></ha-form>
65+
`;
66+
}
67+
68+
private _valueChanged(ev: CustomEvent) {
69+
ev.stopPropagation();
70+
const data = ev.detail.value as TimeCondition;
71+
fireEvent(this, "value-changed", { value: data });
2072
}
2173
}
2274

src/translations/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7565,7 +7565,10 @@
75657565
"state_not_equal": "State is not equal to"
75667566
},
75677567
"time": {
7568-
"label": "Time"
7568+
"label": "Time",
7569+
"after": "After",
7570+
"before": "Before",
7571+
"weekdays": "Weekdays"
75697572
},
75707573
"location": {
75717574
"label": "Location",

0 commit comments

Comments
 (0)