def async_setup_scanner(hass, config, async_see, discovery_info=None): """Validate the configuration and return an Automatic scanner.""" hass.http.register_view(AutomaticAuthCallbackView()) scope = FULL_SCOPE if config.get(CONF_CURRENT_LOCATION) else DEFAULT_SCOPE client = aioautomatic.Client(client_id=config[CONF_CLIENT_ID], client_secret=config[CONF_SECRET], client_session=async_get_clientsession(hass), request_kwargs={'timeout': DEFAULT_TIMEOUT}) filename = AUTOMATIC_CONFIG_FILE.format(config[CONF_CLIENT_ID], config[CONF_NAME]) refresh_token = yield from hass.async_add_job(_get_refresh_token_from_file, hass, filename) if refresh_token is not None: try: session = yield from client.create_session_from_refresh_token( refresh_token) account = AutomaticAccount(config[CONF_NAME], hass, None, filename, client, session, async_see, config.get(CONF_DEVICES)) yield from account.initialize_data() return True except AutomaticError as err: _LOGGER.error(str(err)) configurator = hass.components.configurator request_id = configurator.async_request_config( "Automatic" + (" " + config.get(CONF_NAME)) if CONF_NAME in config else ".", description=("Authorization required for Automatic device tracker" + (" " + config.get(CONF_NAME)) if CONF_NAME in config else "."), link_name=("Click here to authorize Home Assistant" + (" " + config.get(CONF_NAME)) if CONF_NAME in config else "."), link_url=client.generate_oauth_url(scope), entity_picture="/static/images/logo_automatic.png", ) if DATA_CONFIGURING not in hass.data: hass.data[DATA_CONFIGURING] = {} account = AutomaticAccount(config[CONF_NAME], hass, configurator, filename, client, None, async_see, config.get(CONF_DEVICES), request_id) hass.data[DATA_CONFIGURING][client.state] = account return True
def async_setup_scanner(hass, config, async_see, discovery_info=None): """Validate the configuration and return an Automatic scanner.""" import aioautomatic client = aioautomatic.Client(client_id=config[CONF_CLIENT_ID], client_secret=config[CONF_SECRET], client_session=async_get_clientsession(hass), request_kwargs={'timeout': DEFAULT_TIMEOUT}) try: try: session = yield from client.create_session_from_password( FULL_SCOPE, config[CONF_USERNAME], config[CONF_PASSWORD]) except aioautomatic.exceptions.ForbiddenError as exc: if not str(exc).startswith("invalid_scope"): raise exc _LOGGER.info("Client not authorized for current_location scope. " "location:updated events will not be received.") session = yield from client.create_session_from_password( DEFAULT_SCOPE, config[CONF_USERNAME], config[CONF_PASSWORD]) data = AutomaticData(hass, client, session, config[CONF_DEVICES], async_see) # Load the initial vehicle data vehicles = yield from session.get_vehicles() for vehicle in vehicles: hass.async_add_job(data.load_vehicle(vehicle)) except aioautomatic.exceptions.AutomaticError as err: _LOGGER.error(str(err)) return False @callback def ws_connect(event): """Open the websocket connection.""" hass.async_add_job(data.ws_connect()) @callback def ws_close(event): """Close the websocket connection.""" hass.async_add_job(data.ws_close()) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, ws_connect) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, ws_close) return True
def async_setup_scanner(hass, config, async_see, discovery_info=None): """Validate the configuration and return an Automatic scanner.""" import aioautomatic client = aioautomatic.Client(client_id=config[CONF_CLIENT_ID], client_secret=config[CONF_SECRET], client_session=async_get_clientsession(hass), request_kwargs={'timeout': DEFAULT_TIMEOUT}) try: session = yield from client.create_session_from_password( config[CONF_USERNAME], config[CONF_PASSWORD]) data = AutomaticData(hass, session, config[CONF_DEVICES], async_see) except aioautomatic.exceptions.AutomaticError as err: _LOGGER.error(str(err)) return False yield from data.update() return True
def async_setup_scanner(hass, config, async_see, discovery_info=None): """Validate the configuration and return an Automatic scanner.""" hass.http.register_view(AutomaticAuthCallbackView()) scope = FULL_SCOPE if config.get(CONF_CURRENT_LOCATION) else DEFAULT_SCOPE client = aioautomatic.Client( client_id=config[CONF_CLIENT_ID], client_secret=config[CONF_SECRET], client_session=async_get_clientsession(hass), request_kwargs={"timeout": DEFAULT_TIMEOUT}, ) filename = f".automatic/session-{config[CONF_CLIENT_ID]}.json" refresh_token = yield from hass.async_add_job( _get_refresh_token_from_file, hass, filename ) @asyncio.coroutine def initialize_data(session): """Initialize the AutomaticData object from the created session.""" hass.async_add_job( _write_refresh_token_to_file, hass, filename, session.refresh_token ) data = AutomaticData(hass, client, session, config.get(CONF_DEVICES), async_see) # Load the initial vehicle data vehicles = yield from session.get_vehicles() for vehicle in vehicles: hass.async_create_task(data.load_vehicle(vehicle)) # Create a task instead of adding a tracking job, since this task will # run until the websocket connection is closed. hass.loop.create_task(data.ws_connect()) if refresh_token is not None: try: session = yield from client.create_session_from_refresh_token(refresh_token) yield from initialize_data(session) return True except aioautomatic.exceptions.AutomaticError as err: _LOGGER.error(str(err)) configurator = hass.components.configurator request_id = configurator.async_request_config( "Automatic", description=("Authorization required for Automatic device tracker."), link_name="Click here to authorize Home Assistant.", link_url=client.generate_oauth_url(scope), entity_picture="/static/images/logo_automatic.png", ) @asyncio.coroutine def initialize_callback(code, state): """Call after OAuth2 response is returned.""" try: session = yield from client.create_session_from_oauth_code(code, state) yield from initialize_data(session) configurator.async_request_done(request_id) except aioautomatic.exceptions.AutomaticError as err: _LOGGER.error(str(err)) configurator.async_notify_errors(request_id, str(err)) return False if DATA_CONFIGURING not in hass.data: hass.data[DATA_CONFIGURING] = {} hass.data[DATA_CONFIGURING][client.state] = initialize_callback return True