mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 23:48:11 +01:00
Fix timezone detection (#586)
* Fix timezone detection * Update __init__.py
This commit is contained in:
parent
e5899ff717
commit
075fcb77a8
1 changed files with 12 additions and 2 deletions
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue