diff --git a/hassio/core.py b/hassio/core.py index d85605418..28cd4b7f6 100644 --- a/hassio/core.py +++ b/hassio/core.py @@ -20,7 +20,7 @@ from .dock.supervisor import DockerSupervisor from .tasks import ( hassio_update, homeassistant_watchdog, homeassistant_setup, api_sessions_cleanup) -from .tools import get_arch_from_image, get_local_ip +from .tools import get_arch_from_image, get_local_ip, fetch_timezone _LOGGER = logging.getLogger(__name__) @@ -60,13 +60,17 @@ class HassIO(object): # set api endpoint self.config.api_endpoint = await get_local_ip(self.loop) + # update timezone + if self.config.timezone == 'UTC': + self.config.timezone = await fetch_timezone(self.websession) + # hostcontrol await self.host_control.load() # schedule update info tasks self.scheduler.register_task( - self.host_control.load, RUN_UPDATE_INFO_TASKS) + self.host_control.load, RUN_UPDATE_INFO_TASKS) # rest api views self.api.register_host(self.host_control) self.api.register_network(self.host_control) diff --git a/hassio/tools.py b/hassio/tools.py index 6eedce39a..01f15fad8 100644 --- a/hassio/tools.py +++ b/hassio/tools.py @@ -1,5 +1,6 @@ """Tools file for HassIO.""" import asyncio +from contextlib import suppress import json import logging import re @@ -14,6 +15,8 @@ from .const import URL_HASSIO_VERSION, URL_HASSIO_VERSION_BETA _LOGGER = logging.getLogger(__name__) +FREEGEOIP_URL = "https://freegeoip.io/json/" + _RE_VERSION = re.compile(r"VERSION=(.*)") _IMAGE_ARCH = re.compile(r".*/([a-z0-9]*)-hassio-supervisor") @@ -105,3 +108,15 @@ def validate_timezone(timezone): from None return timezone + + +async def fetch_timezone(websession): + """Read timezone from freegeoip.""" + data = {} + with suppress(aiohttp.ClientError, asyncio.TimeoutError, + json.JSONDecodeError, KeyError): + with async_timeout.timeout(10, loop=websession.loop): + async with websession.get(FREEGEOIP_URL) as request: + data = await request.json() + + return data.get('time_zone', 'UTC')