Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Set up of Tesla component."""
from teslajsonpy import Controller as teslaAPI, TeslaException
config = base_config.get(DOMAIN)
email = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
update_interval = config.get(CONF_SCAN_INTERVAL)
if hass.data.get(DOMAIN) is None:
try:
hass.data[DOMAIN] = {
'controller': teslaAPI(email, password, update_interval),
'devices': defaultdict(list)
}
_LOGGER.debug("Connected to the Tesla API.")
except TeslaException 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.message),
title=NOTIFICATION_TITLE,
notification_id=NOTIFICATION_ID)
_LOGGER.error("Unable to communicate with Tesla API: %s",
ex.message)
websession = aiohttp_client.async_get_clientsession(hass)
email = config_entry.title
if email in hass.data[DOMAIN] and CONF_SCAN_INTERVAL in hass.data[DOMAIN][email]:
scan_interval = hass.data[DOMAIN][email][CONF_SCAN_INTERVAL]
hass.config_entries.async_update_entry(
config_entry, options={CONF_SCAN_INTERVAL: scan_interval}
)
hass.data[DOMAIN].pop(email)
try:
controller = TeslaAPI(
websession,
refresh_token=config[CONF_TOKEN],
update_interval=config_entry.options.get(CONF_SCAN_INTERVAL, 300),
)
(refresh_token, access_token) = await controller.connect()
except TeslaException as ex:
_LOGGER.error("Unable to communicate with Tesla API: %s", ex.message)
return False
_async_save_tokens(hass, config_entry, access_token, refresh_token)
entry_data = hass.data[DOMAIN][config_entry.entry_id] = {
"controller": controller,
"devices": defaultdict(list),
DATA_LISTENER: [config_entry.add_update_listener(update_listener)],
}
_LOGGER.debug("Connected to the Tesla API.")
all_devices = entry_data["controller"].get_homeassistant_components()
if not all_devices:
return False
for device in all_devices:
entry_data["devices"][device.hass_type].append(device)