From 1e196bac985e44a9121c937c60b1ada4e54772a8 Mon Sep 17 00:00:00 2001 From: RFDarter Date: Tue, 7 May 2024 02:47:07 +0200 Subject: [PATCH] fix date_time validation (#6688) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/datetime/__init__.py | 8 +- .../components/template/datetime/__init__.py | 10 ++- esphome/config_validation.py | 75 +++++++++---------- tests/components/template/common.yaml | 3 + 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/esphome/components/datetime/__init__.py b/esphome/components/datetime/__init__.py index 639a03515..3d08e4a6d 100644 --- a/esphome/components/datetime/__init__.py +++ b/esphome/components/datetime/__init__.py @@ -169,7 +169,7 @@ async def to_code(config): { cv.Required(CONF_ID): cv.use_id(DateEntity), cv.Required(CONF_DATE): cv.Any( - cv.returning_lambda, cv.date_time(allowed_time=False) + cv.returning_lambda, cv.date_time(date=True, time=False) ), } ), @@ -200,7 +200,7 @@ async def datetime_date_set_to_code(config, action_id, template_arg, args): { cv.Required(CONF_ID): cv.use_id(TimeEntity), cv.Required(CONF_TIME): cv.Any( - cv.returning_lambda, cv.date_time(allowed_date=False) + cv.returning_lambda, cv.date_time(date=False, time=True) ), } ), @@ -230,7 +230,9 @@ async def datetime_time_set_to_code(config, action_id, template_arg, args): cv.Schema( { cv.Required(CONF_ID): cv.use_id(DateTimeEntity), - cv.Required(CONF_DATETIME): cv.Any(cv.returning_lambda, cv.date_time()), + cv.Required(CONF_DATETIME): cv.Any( + cv.returning_lambda, cv.date_time(date=True, time=True) + ), }, ), ) diff --git a/esphome/components/template/datetime/__init__.py b/esphome/components/template/datetime/__init__.py index bf7154ef7..0c9447116 100644 --- a/esphome/components/template/datetime/__init__.py +++ b/esphome/components/template/datetime/__init__.py @@ -72,21 +72,25 @@ CONFIG_SCHEMA = cv.All( .extend(_BASE_SCHEMA) .extend( { - cv.Optional(CONF_INITIAL_VALUE): cv.date_time(allowed_time=False), + cv.Optional(CONF_INITIAL_VALUE): cv.date_time( + date=True, time=False + ), } ), "TIME": datetime.time_schema(TemplateTime) .extend(_BASE_SCHEMA) .extend( { - cv.Optional(CONF_INITIAL_VALUE): cv.date_time(allowed_date=False), + cv.Optional(CONF_INITIAL_VALUE): cv.date_time( + date=False, time=True + ), } ), "DATETIME": datetime.datetime_schema(TemplateDateTime) .extend(_BASE_SCHEMA) .extend( { - cv.Optional(CONF_INITIAL_VALUE): cv.date_time(), + cv.Optional(CONF_INITIAL_VALUE): cv.date_time(date=True, time=True), } ), }, diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 3554f0383..512f1d8f6 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -821,57 +821,50 @@ positive_not_null_time_period = All( def time_of_day(value): - return date_time(allowed_date=False, allowed_time=True)(value) + return date_time(date=False, time=True)(value) -def date_time(allowed_date: bool = True, allowed_time: bool = True): +def date_time(date: bool, time: bool): pattern_str = r"^" # Start of string - if allowed_date: + if date: + pattern_str += r"\d{4}-\d{1,2}-\d{1,2}" + if time: + pattern_str += r" " + if time: pattern_str += ( - r"(" # 1. Optional Date group - r"\d{4}-\d{1,2}-\d{1,2}" # Date - r"(?:\s(?=.+))?" # Space after date only if time is following - r")?" # End optional Date group - ) - if allowed_time: - pattern_str += ( - r"(" # 2. Optional Time group - r"(\d{1,2}:\d{2})" # 3. Hour/Minute - r"(:\d{2})?" # 4. Seconds - r"(" # 5. Optional AM/PM group - r"(\s)?" # 6. Optional Space + r"\d{1,2}:\d{2}" # Hour/Minute + r"(:\d{2})?" # 1. Seconds + r"(" # 2. Optional AM/PM group + r"(\s)?" # 3. Optional Space r"(?:AM|PM|am|pm)" # AM/PM string matching r")?" # End optional AM/PM group - r")?" # End optional Time group ) pattern_str += r"$" # End of string pattern = re.compile(pattern_str) exc_message = "" - if allowed_date: + if date: exc_message += "date" - if allowed_time: - exc_message += "/" - if allowed_time: + if time: exc_message += "time" schema = Schema({}) - if allowed_date: + if date: schema = schema.extend( { - Optional(CONF_YEAR): int_range(min=1970, max=3000), - Optional(CONF_MONTH): int_range(min=1, max=12), - Optional(CONF_DAY): int_range(min=1, max=31), + Required(CONF_YEAR): int_range(min=1970, max=3000), + Required(CONF_MONTH): int_range(min=1, max=12), + Required(CONF_DAY): int_range(min=1, max=31), } ) - if allowed_time: + if time: schema = schema.extend( { - Optional(CONF_HOUR): int_range(min=0, max=23), - Optional(CONF_MINUTE): int_range(min=0, max=59), - Optional(CONF_SECOND): int_range(min=0, max=59), + Required(CONF_HOUR): int_range(min=0, max=23), + Required(CONF_MINUTE): int_range(min=0, max=59), + Required(CONF_SECOND): int_range(min=0, max=59), } ) @@ -885,21 +878,21 @@ def date_time(allowed_date: bool = True, allowed_time: bool = True): # pylint: disable=raise-missing-from raise Invalid(f"Invalid {exc_message}: {value}") - if allowed_date: - has_date = match[1] is not None - if allowed_time: - has_time = match[2] is not None - has_seconds = match[3] is not None - has_ampm = match[4] is not None - has_ampm_space = match[5] is not None + if time: + has_seconds = match[1] is not None + has_ampm = match[2] is not None + has_ampm_space = match[3] is not None format = "" - if allowed_date and has_date: + if date: format += "%Y-%m-%d" - if allowed_time and has_time: + if time: format += " " - if allowed_time and has_time: - format += "%H:%M" + if time: + if has_ampm: + format += "%I:%M" + else: + format += "%H:%M" if has_seconds: format += ":%S" if has_ampm_space: @@ -914,12 +907,12 @@ def date_time(allowed_date: bool = True, allowed_time: bool = True): raise Invalid(f"Invalid {exc_message}: {err}") return_value = {} - if allowed_date and has_date: + if date: return_value[CONF_YEAR] = date_obj.year return_value[CONF_MONTH] = date_obj.month return_value[CONF_DAY] = date_obj.day - if allowed_time and has_time: + if time: return_value[CONF_HOUR] = date_obj.hour return_value[CONF_MINUTE] = date_obj.minute return_value[CONF_SECOND] = date_obj.second if has_seconds else 0 diff --git a/tests/components/template/common.yaml b/tests/components/template/common.yaml index 2b91225b5..9e89424d8 100644 --- a/tests/components/template/common.yaml +++ b/tests/components/template/common.yaml @@ -172,6 +172,7 @@ datetime: name: Date id: test_date type: date + initial_value: "2000-1-2" set_action: - logger.log: "set_value" on_value: @@ -185,6 +186,7 @@ datetime: name: Time id: test_time type: time + initial_value: "12:34:56am" set_action: - logger.log: "set_value" on_value: @@ -198,6 +200,7 @@ datetime: name: DateTime id: test_datetime type: datetime + initial_value: "2000-1-2 12:34:56" set_action: - logger.log: "set_value" on_value: