Skip to content

Commit e868695

Browse files
committed
Remove additional type
1 parent f95bb1d commit e868695

File tree

4 files changed

+77
-42
lines changed

4 files changed

+77
-42
lines changed

src/common/condition/time-calculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { TimeCondition } from "../../panels/lovelace/common/validate-condit
1818
*/
1919
export function calculateNextTimeUpdate(
2020
hass: HomeAssistant,
21-
{ after, before, weekdays }: TimeCondition
21+
{ after, before, weekdays }: Omit<TimeCondition, "condition">
2222
): number | undefined {
2323
const timezone =
2424
hass.locale.time_zone === TimeZone.server

src/common/datetime/check_time.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ 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 { WEEKDAY_MAP, type WeekdayShort } from "./weekday";
5+
import { WEEKDAY_MAP } from "./weekday";
6+
import type { TimeCondition } from "../../panels/lovelace/common/validate-condition";
67

78
/**
89
* Parse a time string (HH:MM or HH:MM:SS) and set it on today's date in the given timezone
@@ -56,16 +57,12 @@ export const parseTimeString = (timeString: string, timezone: string): Date => {
5657
/**
5758
* Check if the current time matches the time condition (after/before/weekday)
5859
* @param hass Home Assistant object
59-
* @param after Optional time string (HH:MM) for after condition
60-
* @param before Optional time string (HH:MM) for before condition
61-
* @param weekdays Optional array of weekdays to match
60+
* @param timeCondition Time condition to check
6261
* @returns true if current time matches the condition
6362
*/
6463
export const checkTimeInRange = (
6564
hass: HomeAssistant,
66-
after?: string,
67-
before?: string,
68-
weekdays?: WeekdayShort[]
65+
{ after, before, weekdays }: Omit<TimeCondition, "condition">
6966
): boolean => {
7067
const timezone =
7168
hass.locale.time_zone === TimeZone.server

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,11 @@ function checkScreenCondition(condition: ScreenCondition, _: HomeAssistant) {
162162
: false;
163163
}
164164

165-
function checkTimeCondition(condition: TimeCondition, hass: HomeAssistant) {
166-
return checkTimeInRange(
167-
hass,
168-
condition.after,
169-
condition.before,
170-
condition.weekdays
171-
);
165+
function checkTimeCondition(
166+
condition: Omit<TimeCondition, "condition">,
167+
hass: HomeAssistant
168+
) {
169+
return checkTimeInRange(hass, condition);
172170
}
173171

174172
function checkLocationCondition(

test/common/datetime/check_time.test.ts

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,27 @@ describe("checkTimeInRange", () => {
3737
// Set time to 10:00 AM
3838
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
3939

40-
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(true);
40+
expect(
41+
checkTimeInRange(mockHass, { after: "08:00", before: "17:00" })
42+
).toBe(true);
4143
});
4244

4345
it("should return false when current time is before range", () => {
4446
// Set time to 7:00 AM
4547
vi.setSystemTime(new Date(2024, 0, 15, 7, 0, 0));
4648

47-
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(false);
49+
expect(
50+
checkTimeInRange(mockHass, { after: "08:00", before: "17:00" })
51+
).toBe(false);
4852
});
4953

5054
it("should return false when current time is after range", () => {
5155
// Set time to 6:00 PM
5256
vi.setSystemTime(new Date(2024, 0, 15, 18, 0, 0));
5357

54-
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(false);
58+
expect(
59+
checkTimeInRange(mockHass, { after: "08:00", before: "17:00" })
60+
).toBe(false);
5561
});
5662
});
5763

@@ -60,45 +66,51 @@ describe("checkTimeInRange", () => {
6066
// Set time to 11:00 PM
6167
vi.setSystemTime(new Date(2024, 0, 15, 23, 0, 0));
6268

63-
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(true);
69+
expect(
70+
checkTimeInRange(mockHass, { after: "22:00", before: "06:00" })
71+
).toBe(true);
6472
});
6573

6674
it("should return true when current time is after midnight", () => {
6775
// Set time to 3:00 AM
6876
vi.setSystemTime(new Date(2024, 0, 15, 3, 0, 0));
6977

70-
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(true);
78+
expect(
79+
checkTimeInRange(mockHass, { after: "22:00", before: "06:00" })
80+
).toBe(true);
7181
});
7282

7383
it("should return false when outside the range", () => {
7484
// Set time to 10:00 AM
7585
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
7686

77-
expect(checkTimeInRange(mockHass, "22:00", "06:00")).toBe(false);
87+
expect(
88+
checkTimeInRange(mockHass, { after: "22:00", before: "06:00" })
89+
).toBe(false);
7890
});
7991
});
8092

8193
describe("only 'after' condition", () => {
8294
it("should return true when after specified time", () => {
8395
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
84-
expect(checkTimeInRange(mockHass, "08:00")).toBe(true);
96+
expect(checkTimeInRange(mockHass, { after: "08:00" })).toBe(true);
8597
});
8698

8799
it("should return false when before specified time", () => {
88100
vi.setSystemTime(new Date(2024, 0, 15, 6, 0, 0));
89-
expect(checkTimeInRange(mockHass, "08:00")).toBe(false);
101+
expect(checkTimeInRange(mockHass, { after: "08:00" })).toBe(false);
90102
});
91103
});
92104

93105
describe("only 'before' condition", () => {
94106
it("should return true when before specified time", () => {
95107
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
96-
expect(checkTimeInRange(mockHass, undefined, "17:00")).toBe(true);
108+
expect(checkTimeInRange(mockHass, { before: "17:00" })).toBe(true);
97109
});
98110

99111
it("should return false when after specified time", () => {
100112
vi.setSystemTime(new Date(2024, 0, 15, 18, 0, 0));
101-
expect(checkTimeInRange(mockHass, undefined, "17:00")).toBe(false);
113+
expect(checkTimeInRange(mockHass, { before: "17:00" })).toBe(false);
102114
});
103115
});
104116

@@ -107,26 +119,22 @@ describe("checkTimeInRange", () => {
107119
// January 15, 2024 is a Monday
108120
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
109121

110-
expect(checkTimeInRange(mockHass, undefined, undefined, ["mon"])).toBe(
111-
true
112-
);
122+
expect(checkTimeInRange(mockHass, { weekdays: ["mon"] })).toBe(true);
113123
});
114124

115125
it("should return false on non-matching weekday", () => {
116126
// January 15, 2024 is a Monday
117127
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
118128

119-
expect(checkTimeInRange(mockHass, undefined, undefined, ["tue"])).toBe(
120-
false
121-
);
129+
expect(checkTimeInRange(mockHass, { weekdays: ["tue"] })).toBe(false);
122130
});
123131

124132
it("should work with multiple weekdays", () => {
125133
// January 15, 2024 is a Monday
126134
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
127135

128136
expect(
129-
checkTimeInRange(mockHass, undefined, undefined, ["mon", "wed", "fri"])
137+
checkTimeInRange(mockHass, { weekdays: ["mon", "wed", "fri"] })
130138
).toBe(true);
131139
});
132140
});
@@ -136,29 +144,49 @@ describe("checkTimeInRange", () => {
136144
// January 15, 2024 is a Monday at 10:00 AM
137145
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
138146

139-
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["mon"])).toBe(true);
147+
expect(
148+
checkTimeInRange(mockHass, {
149+
after: "08:00",
150+
before: "17:00",
151+
weekdays: ["mon"],
152+
})
153+
).toBe(true);
140154
});
141155

142156
it("should return false when time matches but weekday doesn't", () => {
143157
// January 15, 2024 is a Monday at 10:00 AM
144158
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
145159

146-
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["tue"])).toBe(false);
160+
expect(
161+
checkTimeInRange(mockHass, {
162+
after: "08:00",
163+
before: "17:00",
164+
weekdays: ["tue"],
165+
})
166+
).toBe(false);
147167
});
148168

149169
it("should return false when weekday matches but time doesn't", () => {
150170
// January 15, 2024 is a Monday at 6:00 AM
151171
vi.setSystemTime(new Date(2024, 0, 15, 6, 0, 0));
152172

153-
expect(checkTimeInRange(mockHass, "08:00", "17:00", ["mon"])).toBe(false);
173+
expect(
174+
checkTimeInRange(mockHass, {
175+
after: "08:00",
176+
before: "17:00",
177+
weekdays: ["mon"],
178+
})
179+
).toBe(false);
154180
});
155181
});
156182

157183
describe("no conditions", () => {
158184
it("should return true when no conditions specified", () => {
159185
vi.setSystemTime(new Date(2024, 0, 15, 10, 0, 0));
160186

161-
expect(checkTimeInRange(mockHass)).toBe(true);
187+
expect(
188+
checkTimeInRange(mockHass, { after: "08:00", before: "17:00" })
189+
).toBe(true);
162190
});
163191
});
164192

@@ -169,15 +197,19 @@ describe("checkTimeInRange", () => {
169197
vi.setSystemTime(new Date(2024, 2, 10, 1, 30, 0));
170198

171199
// Should be within range that crosses the transition
172-
expect(checkTimeInRange(mockHass, "01:00", "04:00")).toBe(true);
200+
expect(
201+
checkTimeInRange(mockHass, { after: "01:00", before: "04:00" })
202+
).toBe(true);
173203
});
174204

175205
it("should handle spring forward transition after the jump", () => {
176206
// March 10, 2024 at 3:30 AM PDT - after spring forward
177207
vi.setSystemTime(new Date(2024, 2, 10, 3, 30, 0));
178208

179209
// Should still be within range
180-
expect(checkTimeInRange(mockHass, "01:00", "04:00")).toBe(true);
210+
expect(
211+
checkTimeInRange(mockHass, { after: "01:00", before: "04:00" })
212+
).toBe(true);
181213
});
182214

183215
it("should handle fall back transition (gaining an hour)", () => {
@@ -186,24 +218,32 @@ describe("checkTimeInRange", () => {
186218
vi.setSystemTime(new Date(2024, 10, 3, 1, 30, 0));
187219

188220
// Should be within range that crosses the transition
189-
expect(checkTimeInRange(mockHass, "01:00", "03:00")).toBe(true);
221+
expect(
222+
checkTimeInRange(mockHass, { after: "01:00", before: "03:00" })
223+
).toBe(true);
190224
});
191225

192226
it("should handle midnight crossing during DST transition", () => {
193227
// March 10, 2024 at 1:00 AM - during spring forward night
194228
vi.setSystemTime(new Date(2024, 2, 10, 1, 0, 0));
195229

196230
// Range that crosses midnight and DST transition
197-
expect(checkTimeInRange(mockHass, "22:00", "04:00")).toBe(true);
231+
expect(
232+
checkTimeInRange(mockHass, { after: "22:00", before: "04:00" })
233+
).toBe(true);
198234
});
199235

200236
it("should correctly compare times on DST transition day", () => {
201237
// November 3, 2024 at 10:00 AM - after fall back completed
202238
vi.setSystemTime(new Date(2024, 10, 3, 10, 0, 0));
203239

204240
// Normal business hours should work correctly
205-
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(true);
206-
expect(checkTimeInRange(mockHass, "12:00", "17:00")).toBe(false);
241+
expect(
242+
checkTimeInRange(mockHass, { after: "08:00", before: "17:00" })
243+
).toBe(true);
244+
expect(
245+
checkTimeInRange(mockHass, { after: "12:00", before: "17:00" })
246+
).toBe(false);
207247
});
208248
});
209249
});

0 commit comments

Comments
 (0)