1 """Config flow for Google Sheets integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from google.oauth2.credentials
import Credentials
10 from gspread
import Client, GSpreadException
16 from .const
import DEFAULT_ACCESS, DEFAULT_NAME, DOMAIN
18 _LOGGER = logging.getLogger(__name__)
22 config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
24 """Config flow to handle Google Sheets OAuth2 authentication."""
31 return logging.getLogger(__name__)
35 """Extra data that needs to be appended to the authorize url."""
37 "scope": DEFAULT_ACCESS,
39 "access_type":
"offline",
44 self, entry_data: Mapping[str, Any]
45 ) -> ConfigFlowResult:
46 """Perform reauth upon an API authentication error."""
50 self, user_input: dict[str, Any] |
None =
None
51 ) -> ConfigFlowResult:
52 """Confirm reauth dialog."""
53 if user_input
is None:
54 return self.async_show_form(step_id=
"reauth_confirm")
55 return await self.async_step_user()
58 """Create an entry for the flow, or update existing entry."""
60 Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN])
63 if self.
sourcesource == SOURCE_REAUTH:
64 reauth_entry = self._get_reauth_entry()
65 _LOGGER.debug(
"service.open_by_key")
67 await self.hass.async_add_executor_job(
69 reauth_entry.unique_id,
71 except GSpreadException
as err:
73 "Could not find spreadsheet '%s': %s",
74 reauth_entry.unique_id,
77 return self.async_abort(reason=
"open_spreadsheet_failure")
79 return self.async_update_reload_and_abort(reauth_entry, data=data)
82 doc = await self.hass.async_add_executor_job(
83 service.create,
"Home Assistant"
85 except GSpreadException
as err:
86 _LOGGER.error(
"Error creating spreadsheet: %s",
str(err))
87 return self.async_abort(reason=
"create_spreadsheet_failure")
89 await self.async_set_unique_id(doc.id)
90 self._abort_if_unique_id_configured()
91 return self.async_create_entry(
92 title=DEFAULT_NAME, data=data, description_placeholders={
"url": doc.url}
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
logging.Logger logger(self)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
dict[str, Any] extra_authorize_data(self)