async def _async_create_session(self, code): """Create point session and entries.""" from pypoint import PointSession flow = self.hass.data[DATA_FLOW_IMPL][DOMAIN] client_id = flow[CLIENT_ID] client_secret = flow[CLIENT_SECRET] point_session = PointSession( client_id, client_secret=client_secret, ) token = await self.hass.async_add_executor_job( point_session.get_access_token, code) _LOGGER.debug("Got new token") if not point_session.is_authorized: _LOGGER.error('Authentication Error') return self.async_abort(reason='auth_error') _LOGGER.info('Successfully authenticated Point') user_email = point_session.user().get('email') or "" return self.async_create_entry( title=user_email, data={ 'token': token, 'refresh_args': { 'client_id': client_id, 'client_secret': client_secret } }, )
async def _async_create_session(self, code): """Create point session and entries.""" flow = self.hass.data[DATA_FLOW_IMPL][DOMAIN] client_id = flow[CLIENT_ID] client_secret = flow[CLIENT_SECRET] point_session = PointSession(client_id, client_secret=client_secret) token = await self.hass.async_add_executor_job( point_session.get_access_token, code) _LOGGER.debug("Got new token") if not point_session.is_authorized: _LOGGER.error("Authentication Error") return self.async_abort(reason="auth_error") _LOGGER.info("Successfully authenticated Point") user_email = point_session.user().get("email") or "" return self.async_create_entry( title=user_email, data={ "token": token, "refresh_args": { "client_id": client_id, "client_secret": client_secret, }, }, )
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Set up Point from a config entry.""" def token_saver(token): _LOGGER.debug("Saving updated token") hass.config_entries.async_update_entry( entry, data={**entry.data, CONF_TOKEN: token} ) # Force token update. entry.data[CONF_TOKEN]["expires_in"] = -1 session = PointSession( entry.data["refresh_args"]["client_id"], token=entry.data[CONF_TOKEN], auto_refresh_kwargs=entry.data["refresh_args"], token_saver=token_saver, ) if not session.is_authorized: _LOGGER.error("Authentication Error") return False hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() await async_setup_webhook(hass, entry, session) client = MinutPointClient(hass, entry, session) hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: client}) hass.async_create_task(client.update()) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Point from a config entry.""" async def token_saver(token, **kwargs): _LOGGER.debug("Saving updated token %s", token) hass.config_entries.async_update_entry( entry, data={**entry.data, CONF_TOKEN: token} ) session = PointSession( hass.helpers.aiohttp_client.async_get_clientsession(), entry.data["refresh_args"][CONF_CLIENT_ID], entry.data["refresh_args"][CONF_CLIENT_SECRET], token=entry.data[CONF_TOKEN], token_saver=token_saver, ) try: await session.ensure_active_token() except Exception: # pylint: disable=broad-except _LOGGER.error("Authentication Error") return False hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() await async_setup_webhook(hass, entry, session) client = MinutPointClient(hass, entry, session) hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: client}) hass.async_create_task(client.update()) return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Set up Point from a config entry.""" from pypoint import PointSession def token_saver(token): _LOGGER.debug('Saving updated token') entry.data[CONF_TOKEN] = token hass.config_entries.async_update_entry(entry, data={**entry.data}) # Force token update. entry.data[CONF_TOKEN]['expires_in'] = -1 session = PointSession( entry.data['refresh_args']['client_id'], token=entry.data[CONF_TOKEN], auto_refresh_kwargs=entry.data['refresh_args'], token_saver=token_saver, ) if not session.is_authorized: _LOGGER.error('Authentication Error') return False hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() await async_setup_webhook(hass, entry, session) client = MinutPointClient(hass, entry, session) hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: client}) await client.update() return True
async def _async_create_session(self, code): """Create point session and entries.""" flow = self.opp.data[DATA_FLOW_IMPL][DOMAIN] client_id = flow[CONF_CLIENT_ID] client_secret = flow[CONF_CLIENT_SECRET] point_session = PointSession( self.opp.helpers.aiohttp_client.async_get_clientsession(), client_id, client_secret, ) token = await point_session.get_access_token(code) _LOGGER.debug("Got new token") if not point_session.is_authorized: _LOGGER.error("Authentication Error") return self.async_abort(reason="auth_error") _LOGGER.info("Successfully authenticated Point") user_email = (await point_session.user()).get("email") or "" return self.async_create_entry( title=user_email, data={ "token": token, "refresh_args": { CONF_CLIENT_ID: client_id, CONF_CLIENT_SECRET: client_secret, }, }, )
async def _get_authorization_url(self): """Create Minut Point session and get authorization url.""" from pypoint import PointSession flow = self.hass.data[DATA_FLOW_IMPL][self.flow_impl] client_id = flow[CLIENT_ID] client_secret = flow[CLIENT_SECRET] point_session = PointSession(client_id, client_secret=client_secret) self.hass.http.register_view(MinutAuthCallbackView()) return point_session.get_authorization_url
async def _get_authorization_url(self): """Create Minut Point session and get authorization url.""" flow = self.hass.data[DATA_FLOW_IMPL][self.flow_impl] client_id = flow[CONF_CLIENT_ID] client_secret = flow[CONF_CLIENT_SECRET] point_session = PointSession( self.hass.helpers.aiohttp_client.async_get_clientsession(), client_id, client_secret, ) self.hass.http.register_view(MinutAuthCallbackView()) return point_session.get_authorization_url
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Point from a config entry.""" async def token_saver(token, **kwargs): _LOGGER.debug("Saving updated token %s", token) hass.config_entries.async_update_entry(entry, data={ **entry.data, CONF_TOKEN: token }) session = PointSession( async_get_clientsession(hass), entry.data["refresh_args"][CONF_CLIENT_ID], entry.data["refresh_args"][CONF_CLIENT_SECRET], token=entry.data[CONF_TOKEN], token_saver=token_saver, ) try: # pylint: disable-next=fixme # TODO Remove authlib constraint when refactoring this code await session.ensure_active_token() except ConnectTimeout as err: _LOGGER.debug("Connection Timeout") raise ConfigEntryNotReady from err except Exception: # pylint: disable=broad-except _LOGGER.error("Authentication Error") return False hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() await async_setup_webhook(hass, entry, session) client = MinutPointClient(hass, entry, session) hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: client}) hass.async_create_task(client.update()) return True