|
| 1 | +import { TZDate } from "@date-fns/tz"; |
| 2 | +import { isBefore, isAfter, isWithinInterval } from "date-fns"; |
| 3 | +import type { HomeAssistant } from "../../types"; |
| 4 | +import { TimeZone } from "../../data/translation"; |
| 5 | +import { WEEKDAY_MAP } from "./weekday"; |
| 6 | +import type { TimeCondition } from "../../panels/lovelace/common/validate-condition"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Parse a time string (HH:MM or HH:MM:SS) and set it on today's date in the given timezone |
| 10 | + * @param timeString The time string to parse |
| 11 | + * @param timezone The timezone to use |
| 12 | + * @returns The Date object |
| 13 | + */ |
| 14 | +export const parseTimeString = (timeString: string, timezone: string): Date => { |
| 15 | + const parts = timeString.split(":"); |
| 16 | + |
| 17 | + if (parts.length < 2 || parts.length > 3) { |
| 18 | + throw new Error( |
| 19 | + `Invalid time format: ${timeString}. Expected HH:MM or HH:MM:SS` |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + const hours = parseInt(parts[0], 10); |
| 24 | + const minutes = parseInt(parts[1], 10); |
| 25 | + const seconds = parts.length === 3 ? parseInt(parts[2], 10) : 0; |
| 26 | + |
| 27 | + if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) { |
| 28 | + throw new Error(`Invalid time values in: ${timeString}`); |
| 29 | + } |
| 30 | + |
| 31 | + // Add range validation |
| 32 | + if (hours < 0 || hours > 23) { |
| 33 | + throw new Error(`Invalid hours in: ${timeString}. Must be 0-23`); |
| 34 | + } |
| 35 | + if (minutes < 0 || minutes > 59) { |
| 36 | + throw new Error(`Invalid minutes in: ${timeString}. Must be 0-59`); |
| 37 | + } |
| 38 | + if (seconds < 0 || seconds > 59) { |
| 39 | + throw new Error(`Invalid seconds in: ${timeString}. Must be 0-59`); |
| 40 | + } |
| 41 | + |
| 42 | + const now = new TZDate(new Date(), timezone); |
| 43 | + const dateWithTime = new TZDate( |
| 44 | + now.getFullYear(), |
| 45 | + now.getMonth(), |
| 46 | + now.getDate(), |
| 47 | + hours, |
| 48 | + minutes, |
| 49 | + seconds, |
| 50 | + 0, |
| 51 | + timezone |
| 52 | + ); |
| 53 | + |
| 54 | + return new Date(dateWithTime.getTime()); |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Check if the current time matches the time condition (after/before/weekday) |
| 59 | + * @param hass Home Assistant object |
| 60 | + * @param timeCondition Time condition to check |
| 61 | + * @returns true if current time matches the condition |
| 62 | + */ |
| 63 | +export const checkTimeInRange = ( |
| 64 | + hass: HomeAssistant, |
| 65 | + { after, before, weekdays }: Omit<TimeCondition, "condition"> |
| 66 | +): boolean => { |
| 67 | + const timezone = |
| 68 | + hass.locale.time_zone === TimeZone.server |
| 69 | + ? hass.config.time_zone |
| 70 | + : Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 71 | + |
| 72 | + const now = new TZDate(new Date(), timezone); |
| 73 | + |
| 74 | + // Check weekday condition |
| 75 | + if (weekdays && weekdays.length > 0) { |
| 76 | + const currentWeekday = WEEKDAY_MAP[now.getDay()]; |
| 77 | + if (!weekdays.includes(currentWeekday)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Check time conditions |
| 83 | + if (!after && !before) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + const afterDate = after ? parseTimeString(after, timezone) : undefined; |
| 88 | + const beforeDate = before ? parseTimeString(before, timezone) : undefined; |
| 89 | + |
| 90 | + if (afterDate && beforeDate) { |
| 91 | + if (isBefore(beforeDate, afterDate)) { |
| 92 | + // Crosses midnight (e.g., 22:00 to 06:00) |
| 93 | + return isAfter(now, afterDate) || isBefore(now, beforeDate); |
| 94 | + } |
| 95 | + return isWithinInterval(now, { start: afterDate, end: beforeDate }); |
| 96 | + } |
| 97 | + |
| 98 | + if (afterDate) { |
| 99 | + return !isBefore(now, afterDate); |
| 100 | + } |
| 101 | + |
| 102 | + if (beforeDate) { |
| 103 | + return !isAfter(now, beforeDate); |
| 104 | + } |
| 105 | + |
| 106 | + return true; |
| 107 | +}; |
0 commit comments