From 85195436c18d774805bb4436d92031709c5c6705 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 3 Jul 2019 17:13:40 +0200 Subject: [PATCH] Work around pytz tzname bug Fixes https://github.com/esphome/issues/issues/445 --- esphome/components/time/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index 634de26f00..2097be3a26 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -2,6 +2,7 @@ import bisect import datetime import logging import math +import string import pytz import tzlocal @@ -52,8 +53,18 @@ def _tz_dst_str(dt): _tz_timedelta(td)) -def _non_dst_tz(tz, dt): +def _safe_tzname(tz, dt): tzname = tz.tzname(dt) + # pytz does not always return valid tznames + # For example: 'Europe/Saratov' returns '+04' + # Work around it by using a generic name for the timezone + if not all(c in string.ascii_letters for c in tzname): + return 'TZ' + return tzname + + +def _non_dst_tz(tz, dt): + tzname = _safe_tzname(tz, dt) utcoffset = tz.utcoffset(dt) _LOGGER.info("Detected timezone '%s' with UTC offset %s", tzname, _tz_timedelta(utcoffset))