From 3b2bf1d567d8debc80284a0c417c476c3acba6af Mon Sep 17 00:00:00 2001 From: Sergey Isachenko Date: Thu, 7 Sep 2017 19:20:27 +0300 Subject: [PATCH] Fix for potential issue with tesla initialization (#9307) Fix for potential issue with tesla initialization --- homeassistant/components/tesla.py | 36 +++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/tesla.py b/homeassistant/components/tesla.py index e48d805abab..08006310dc7 100644 --- a/homeassistant/components/tesla.py +++ b/homeassistant/components/tesla.py @@ -5,7 +5,9 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/tesla/ """ from collections import defaultdict +import logging +from urllib.error import HTTPError import voluptuous as vol from homeassistant.const import ( @@ -19,6 +21,8 @@ REQUIREMENTS = ['teslajsonpy==0.0.11'] DOMAIN = 'tesla' +_LOGGER = logging.getLogger(__name__) + TESLA_ID_FORMAT = '{}_{}' TESLA_ID_LIST_SCHEMA = vol.Schema([int]) @@ -31,6 +35,9 @@ CONFIG_SCHEMA = vol.Schema({ }), }, extra=vol.ALLOW_EXTRA) +NOTIFICATION_ID = 'tesla_integration_notification' +NOTIFICATION_TITLE = 'Tesla integration setup' + TESLA_COMPONENTS = [ 'sensor', 'lock', 'climate', 'binary_sensor', 'device_tracker' ] @@ -46,10 +53,31 @@ def setup(hass, base_config): password = config.get(CONF_PASSWORD) update_interval = config.get(CONF_SCAN_INTERVAL) if hass.data.get(DOMAIN) is None: - hass.data[DOMAIN] = { - 'controller': teslaApi(email, password, update_interval), - 'devices': defaultdict(list) - } + try: + hass.data[DOMAIN] = { + 'controller': teslaApi(email, password, update_interval), + 'devices': defaultdict(list) + } + _LOGGER.debug("Connected to the Tesla API.") + except HTTPError as ex: + if ex.code == 401: + hass.components.persistent_notification.create( + "Error:
Please check username and password." + "You will need to restart Home Assistant after fixing.", + title=NOTIFICATION_TITLE, + notification_id=NOTIFICATION_ID) + else: + hass.components.persistent_notification.create( + "Error:
Can't communicate with Tesla API.
" + "Error code: {} Reason: {}" + "You will need to restart Home Assistant after fixing." + "".format(ex.code, ex.reason), + title=NOTIFICATION_TITLE, + notification_id=NOTIFICATION_ID) + _LOGGER.error("Unable to communicate with Tesla API: %s", + ex.reason) + + return False all_devices = hass.data[DOMAIN]['controller'].list_vehicles()