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.
This commit is contained in:
Joakim Plate 2024-06-03 19:57:17 +00:00
parent 3cf627eedc
commit b664c15977

View File

@ -2,6 +2,9 @@ import datetime
import pytz import pytz
# TODO : Use system timezone
# Preload timezone to avoid loading in event loop later
local_tz = pytz.timezone('Europe/Amsterdam')
def timestamp(value): def timestamp(value):
try: try:
@ -20,8 +23,6 @@ def timestamp(value):
else: else:
is_dst = False is_dst = False
# TODO : Use system timezone
local_tz = pytz.timezone('Europe/Amsterdam')
localized_datetime = local_tz.localize(naive_datetime, is_dst=is_dst) localized_datetime = local_tz.localize(naive_datetime, is_dst=is_dst)
return localized_datetime.astimezone(pytz.utc) return localized_datetime.astimezone(pytz.utc)