mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 02:49:40 +00:00
* Add Google Photos integration * Mark credentials typing * Add code review suggestions to simpilfy google_photos * Update tests/components/google_photos/conftest.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Fix comment typo * Update test fixtures from review feedback * Remove unnecessary test for services * Remove keyword argument --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""Config flow for Google Photos."""
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant.config_entries import ConfigFlowResult
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
|
|
from homeassistant.helpers import config_entry_oauth2_flow
|
|
|
|
from . import api
|
|
from .const import DOMAIN, OAUTH2_SCOPES
|
|
from .exceptions import GooglePhotosApiError
|
|
|
|
|
|
class OAuth2FlowHandler(
|
|
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
|
|
):
|
|
"""Config flow to handle Google Photos OAuth2 authentication."""
|
|
|
|
DOMAIN = DOMAIN
|
|
|
|
@property
|
|
def logger(self) -> logging.Logger:
|
|
"""Return logger."""
|
|
return logging.getLogger(__name__)
|
|
|
|
@property
|
|
def extra_authorize_data(self) -> dict[str, Any]:
|
|
"""Extra data that needs to be appended to the authorize url."""
|
|
return {
|
|
"scope": " ".join(OAUTH2_SCOPES),
|
|
# Add params to ensure we get back a refresh token
|
|
"access_type": "offline",
|
|
"prompt": "consent",
|
|
}
|
|
|
|
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
|
|
"""Create an entry for the flow."""
|
|
client = api.AsyncConfigFlowAuth(self.hass, data[CONF_TOKEN][CONF_ACCESS_TOKEN])
|
|
try:
|
|
user_resource_info = await client.get_user_info()
|
|
await client.list_media_items()
|
|
except GooglePhotosApiError as ex:
|
|
return self.async_abort(
|
|
reason="access_not_configured",
|
|
description_placeholders={"message": str(ex)},
|
|
)
|
|
except Exception:
|
|
self.logger.exception("Unknown error occurred")
|
|
return self.async_abort(reason="unknown")
|
|
user_id = user_resource_info["id"]
|
|
await self.async_set_unique_id(user_id)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(title=user_resource_info["name"], data=data)
|