From 075fcb77a885e0491f5e2f99b2ac23e84bf3458e Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 1 Jun 2019 12:03:43 +0200 Subject: [PATCH] Fix timezone detection (#586) * Fix timezone detection * Update __init__.py --- esphome/components/time/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index a81255a254..8c85a3d62f 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -70,6 +70,14 @@ def convert_tz(pytz_obj): transition_times = tz._utc_transition_times transition_info = tz._transition_info idx = max(0, bisect.bisect_right(transition_times, now)) + if idx >= len(transition_times): + tzname = tz.tzname(now) + utcoffset = tz.utcoffset(now) + _LOGGER.info("Detected timezone '%s' with UTC offset %s", + tzname, _tz_timedelta(utcoffset)) + tzbase = '{}{}'.format(tzname, _tz_timedelta(-1 * utcoffset)) + return tzbase + idx1, idx2 = idx, idx + 1 dstoffset1 = transition_info[idx1][1] if dstoffset1 == datetime.timedelta(seconds=0): @@ -244,10 +252,12 @@ def validate_tz(value): value = cv.string_strict(value) try: - return convert_tz(pytz.timezone(value)) - except Exception: # pylint: disable=broad-except + pytz_obj = pytz.timezone(value) + except pytz.UnknownTimeZoneError: # pylint: disable=broad-except return value + return convert_tz(pytz_obj) + TIME_SCHEMA = cv.Schema({ cv.Optional(CONF_TIMEZONE, default=detect_tz): validate_tz,