Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Validation utility functions for ecobee services."""
2 
3 from datetime import date, datetime, timedelta
4 
5 import voluptuous as vol
6 
7 
8 def ecobee_date(date_string):
9  """Validate a date_string as valid for the ecobee API."""
10  try:
11  datetime.strptime(date_string, "%Y-%m-%d")
12  except ValueError as err:
13  raise vol.Invalid("Date does not match ecobee date format YYYY-MM-DD") from err
14  return date_string
15 
16 
17 def ecobee_time(time_string):
18  """Validate a time_string as valid for the ecobee API."""
19  try:
20  datetime.strptime(time_string, "%H:%M:%S")
21  except ValueError as err:
22  raise vol.Invalid(
23  "Time does not match ecobee 24-hour time format HH:MM:SS"
24  ) from err
25  return time_string
26 
27 
28 def is_indefinite_hold(start_date_string: str, end_date_string: str) -> bool:
29  """Determine if the given start and end dates from the ecobee API represent an indefinite hold.
30 
31  This is not documented in the API, so a rough heuristic is used where a hold over 1 year is considered indefinite.
32  """
33  return date.fromisoformat(end_date_string) - date.fromisoformat(
34  start_date_string
35  ) > timedelta(days=365)
bool is_indefinite_hold(str start_date_string, str end_date_string)
Definition: util.py:28
def ecobee_time(time_string)
Definition: util.py:17
def ecobee_date(date_string)
Definition: util.py:8