From b664c15977bfebac69bb2c43e9e5758bbc316356 Mon Sep 17 00:00:00 2001 From: Joakim Plate <elupus@ecce.se> Date: Mon, 3 Jun 2024 19:57:17 +0000 Subject: [PATCH] Avoid loading timezone at runtime Pytz will load timezone info from files in a lazy fashion on first access. This triggers warnings in HA due to it blocking the event loop. Pre-load the needed timezone info at module import instead, which will run in executor in HA. --- dsmr_parser/value_types.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dsmr_parser/value_types.py b/dsmr_parser/value_types.py index 487e98c..f915918 100644 --- a/dsmr_parser/value_types.py +++ b/dsmr_parser/value_types.py @@ -2,6 +2,9 @@ import datetime import pytz +# TODO : Use system timezone +# Preload timezone to avoid loading in event loop later +local_tz = pytz.timezone('Europe/Amsterdam') def timestamp(value): try: @@ -20,8 +23,6 @@ def timestamp(value): else: is_dst = False - # TODO : Use system timezone - local_tz = pytz.timezone('Europe/Amsterdam') localized_datetime = local_tz.localize(naive_datetime, is_dst=is_dst) return localized_datetime.astimezone(pytz.utc)