Skip to content

Commit f95bb1d

Browse files
committed
Reduce
1 parent c99f95a commit f95bb1d

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

src/common/condition/extract.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { Condition } from "../../panels/lovelace/common/validate-condition";
1+
import type {
2+
Condition,
3+
TimeCondition,
4+
} from "../../panels/lovelace/common/validate-condition";
25

36
/**
47
* Extract media queries from conditions recursively
@@ -20,19 +23,13 @@ export function extractMediaQueries(conditions: Condition[]): string[] {
2023
*/
2124
export function extractTimeConditions(
2225
conditions: Condition[]
23-
): { after?: string; before?: string; weekdays?: string[] }[] {
24-
return conditions.reduce<
25-
{ after?: string; before?: string; weekdays?: string[] }[]
26-
>((array, c) => {
26+
): TimeCondition[] {
27+
return conditions.reduce<TimeCondition[]>((array, c) => {
2728
if ("conditions" in c && c.conditions) {
2829
array.push(...extractTimeConditions(c.conditions));
2930
}
3031
if (c.condition === "time") {
31-
array.push({
32-
after: c.after,
33-
before: c.before,
34-
weekdays: c.weekdays,
35-
});
32+
array.push(c);
3633
}
3734
return array;
3835
}, []);

src/common/condition/listeners.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ export function setupTimeListeners(
5252

5353
timeConditions.forEach((timeCondition) => {
5454
const scheduleUpdate = () => {
55-
const delay = calculateNextTimeUpdate(
56-
hass,
57-
timeCondition.after,
58-
timeCondition.before,
59-
timeCondition.weekdays
60-
);
55+
const delay = calculateNextTimeUpdate(hass, timeCondition);
6156

6257
if (delay === undefined) return;
6358

src/common/condition/time-calculator.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ import {
88
import type { HomeAssistant } from "../../types";
99
import { TimeZone } from "../../data/translation";
1010
import { parseTimeString } from "../datetime/check_time";
11+
import type { TimeCondition } from "../../panels/lovelace/common/validate-condition";
1112

1213
/**
1314
* Calculate milliseconds until next time boundary for a time condition
1415
* @param hass Home Assistant object
15-
* @param after Optional time string (HH:MM) for after condition
16-
* @param before Optional time string (HH:MM) for before condition
17-
* @param weekdays Optional array of weekdays to match
16+
* @param timeCondition Time condition to calculate next update for
1817
* @returns Milliseconds until next boundary, or undefined if no boundaries
1918
*/
2019
export function calculateNextTimeUpdate(
2120
hass: HomeAssistant,
22-
after?: string,
23-
before?: string,
24-
weekdays?: string[]
21+
{ after, before, weekdays }: TimeCondition
2522
): number | undefined {
2623
const timezone =
2724
hass.locale.time_zone === TimeZone.server

0 commit comments

Comments
 (0)