From 141a9af1c3d3ae4f4dbe13bd8e9221fc4066807d Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Mon, 20 Nov 2023 16:47:54 -0500 Subject: [PATCH] Make datetime tz aware --- custom_components/variable/helpers.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/custom_components/variable/helpers.py b/custom_components/variable/helpers.py index e60bb45..60c6e6d 100644 --- a/custom_components/variable/helpers.py +++ b/custom_components/variable/helpers.py @@ -3,6 +3,8 @@ import datetime import logging +import homeassistant.util.dt as dt_util + _LOGGER = logging.getLogger(__name__) @@ -61,6 +63,11 @@ def value_to_type(init_val, dest_type): # noqa: C901 _LOGGER.debug( f"[value_to_type] return value: {value_datetime}, type: {type(value_datetime)}" ) + if ( + value_datetime.tzinfo is None + or value_datetime.tzinfo.utcoffset(value_datetime) is None + ): + return value_datetime.replace(tzinfo=dt_util.UTC) return value_datetime elif dest_type == "number": @@ -113,6 +120,11 @@ def value_to_type(init_val, dest_type): # noqa: C901 _LOGGER.debug( f"[value_to_type] return value: {value_datetime}, type: {type(value_datetime)}" ) + if ( + value_datetime.tzinfo is None + or value_datetime.tzinfo.utcoffset(value_datetime) is None + ): + return value_datetime.replace(tzinfo=dt_util.UTC) return value_datetime elif dest_type == "number": _LOGGER.debug(