async def async_setup(hass, base_config): """Set up of Tesla component.""" 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: websession = aiohttp_client.async_get_clientsession(hass) controller = teslaAPI( websession, email=email, password=password, update_interval=update_interval, ) await controller.connect(test_login=False) hass.data[DOMAIN] = { "controller": controller, "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) return False all_devices = controller.get_homeassistant_components() if not all_devices: return False for device in all_devices: hass.data[DOMAIN]["devices"][device.hass_type].append(device) for component in TESLA_COMPONENTS: hass.async_create_task( discovery.async_load_platform(hass, component, DOMAIN, {}, base_config)) return True
def setup(hass, base_config): """Set up of Tesla platform.""" 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) return False all_devices = hass.data[DOMAIN]['controller'].list_vehicles() if not all_devices: return False for device in all_devices: hass.data[DOMAIN]['devices'][device.hass_type].append(device) for component in TESLA_COMPONENTS: discovery.load_platform(hass, component, DOMAIN, {}, base_config) return True
def setup(hass, base_config): """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) return False all_devices = hass.data[DOMAIN]["controller"].list_vehicles() if not all_devices: return False for device in all_devices: hass.data[DOMAIN]["devices"][device.hass_type].append(device) for component in TESLA_COMPONENTS: discovery.load_platform(hass, component, DOMAIN, {}, base_config) return True