mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""Config flow for Dropbox."""
|
|
|
|
from collections.abc import Mapping
|
|
import logging
|
|
from typing import Any
|
|
|
|
from python_dropbox_api import DropboxAPIClient
|
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler
|
|
|
|
from .auth import DropboxConfigFlowAuth
|
|
from .const import DOMAIN
|
|
|
|
|
|
class DropboxConfigFlow(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|
"""Config flow to handle Dropbox OAuth2 authentication."""
|
|
|
|
DOMAIN = DOMAIN
|
|
|
|
@property
|
|
def logger(self) -> logging.Logger:
|
|
"""Return logger."""
|
|
return logging.getLogger(__name__)
|
|
|
|
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
|
|
"""Create an entry for the flow, or update existing entry."""
|
|
access_token = data[CONF_TOKEN][CONF_ACCESS_TOKEN]
|
|
|
|
auth = DropboxConfigFlowAuth(async_get_clientsession(self.hass), access_token)
|
|
|
|
client = DropboxAPIClient(auth)
|
|
account_info = await client.get_account_info()
|
|
|
|
await self.async_set_unique_id(account_info.account_id)
|
|
if self.source == SOURCE_REAUTH:
|
|
self._abort_if_unique_id_mismatch(reason="wrong_account")
|
|
return self.async_update_reload_and_abort(
|
|
self._get_reauth_entry(), data=data
|
|
)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(title=account_info.email, data=data)
|
|
|
|
async def async_step_reauth(
|
|
self, entry_data: Mapping[str, Any]
|
|
) -> ConfigFlowResult:
|
|
"""Perform reauth upon an API authentication error."""
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
async def async_step_reauth_confirm(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Dialog that informs the user that reauth is required."""
|
|
if user_input is None:
|
|
return self.async_show_form(step_id="reauth_confirm")
|
|
return await self.async_step_user()
|