Prepare google calendar integration for Application Credentials (#71748)

* Prepare google calendar integration for Application Credentials

Update google calendar integration to have fewer dependencies on
yaml configuration data to prepare for supporting application
credentials, which means setup can happen without configuration.yaml
at all. This pre-factoring will allow the following PR adding
application credentials support to be more focused.

* Add test coverage for device auth checks
This commit is contained in:
Allen Porter 2022-05-12 19:33:52 -07:00 committed by GitHub
parent 7ab4960b1e
commit 32e4046435
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 25 deletions

View file

@ -9,7 +9,14 @@ from oauth2client.client import Credentials
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_entry_oauth2_flow
from .api import DeviceFlow, OAuthError, async_create_device_flow
from .api import (
DEVICE_AUTH_CREDS,
DeviceAuth,
DeviceFlow,
OAuthError,
async_create_device_flow,
get_feature_access,
)
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -67,15 +74,28 @@ class OAuth2FlowHandler(
if not self._device_flow:
_LOGGER.debug("Creating DeviceAuth flow")
if not isinstance(self.flow_impl, DeviceAuth):
_LOGGER.error(
"Unexpected OAuth implementation does not support device auth: %s",
self.flow_impl,
)
return self.async_abort(reason="oauth_error")
try:
device_flow = await async_create_device_flow(self.hass)
device_flow = await async_create_device_flow(
self.hass,
self.flow_impl.client_id,
self.flow_impl.client_secret,
get_feature_access(self.hass),
)
except OAuthError as err:
_LOGGER.error("Error initializing device flow: %s", str(err))
return self.async_abort(reason="oauth_error")
self._device_flow = device_flow
async def _exchange_finished(creds: Credentials | None) -> None:
self.external_data = {"creds": creds} # is None on timeout/expiration
self.external_data = {
DEVICE_AUTH_CREDS: creds
} # is None on timeout/expiration
self.hass.async_create_task(
self.hass.config_entries.flow.async_configure(
flow_id=self.flow_id, user_input={}
@ -97,7 +117,7 @@ class OAuth2FlowHandler(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle external yaml configuration."""
if self.external_data.get("creds") is None:
if self.external_data.get(DEVICE_AUTH_CREDS) is None:
return self.async_abort(reason="code_expired")
return await super().async_step_creation(user_input)