Skip to content

Commit

Permalink
Avoid loading timezone at runtime (#157)
Browse files Browse the repository at this point in the history
* 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.

* Update value_types.py
  • Loading branch information
elupus authored Jun 4, 2024
1 parent 3cf627e commit b42ceb6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dsmr_parser/value_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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:
Expand All @@ -20,8 +24,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)

0 comments on commit b42ceb6

Please sign in to comment.