Fix for potential issue with tesla initialization (#9307)

Fix for potential issue with tesla initialization
pull/9327/head
Sergey Isachenko 2017-09-07 19:20:27 +03:00 committed by Paulus Schoutsen
parent 7036a7845c
commit d2d876945b
1 changed files with 32 additions and 4 deletions

View File

@ -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:<br />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:<br />Can't communicate with Tesla API.<br />"
"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()