Fix timezone detection (#586)

* Fix timezone detection

* Update __init__.py
This commit is contained in:
Otto Winter 2019-06-01 12:03:43 +02:00 committed by GitHub
parent 420c8b49e2
commit 672f8d1719
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,6 +70,14 @@ def convert_tz(pytz_obj):
transition_times = tz._utc_transition_times transition_times = tz._utc_transition_times
transition_info = tz._transition_info transition_info = tz._transition_info
idx = max(0, bisect.bisect_right(transition_times, now)) 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 idx1, idx2 = idx, idx + 1
dstoffset1 = transition_info[idx1][1] dstoffset1 = transition_info[idx1][1]
if dstoffset1 == datetime.timedelta(seconds=0): if dstoffset1 == datetime.timedelta(seconds=0):
@ -244,10 +252,12 @@ def validate_tz(value):
value = cv.string_strict(value) value = cv.string_strict(value)
try: try:
return convert_tz(pytz.timezone(value)) pytz_obj = pytz.timezone(value)
except Exception: # pylint: disable=broad-except except pytz.UnknownTimeZoneError: # pylint: disable=broad-except
return value return value
return convert_tz(pytz_obj)
TIME_SCHEMA = cv.Schema({ TIME_SCHEMA = cv.Schema({
cv.Optional(CONF_TIMEZONE, default=detect_tz): validate_tz, cv.Optional(CONF_TIMEZONE, default=detect_tz): validate_tz,