Skip to content

Commit

Permalink
Merge pull request #10 from Daanoz/feature/exception_detail_in_config…
Browse files Browse the repository at this point in the history
…_flow

feat: improve error message on setup issues
  • Loading branch information
Daanoz authored Mar 20, 2023
2 parents 366c418 + d70522c commit 1181ed1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
21 changes: 20 additions & 1 deletion custom_components/google_photos/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError as GoogleApiError

from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -82,7 +83,25 @@ def _get_profile() -> dict[str, Any]:
userinfo = lib.userinfo().get().execute() # pylint: disable=no-member
return userinfo

email = (await self.hass.async_add_executor_job(_get_profile))["email"]
def _check_photoslibrary_access() -> PhotosLibraryService:
lib = build(
"photoslibrary",
"v1",
credentials=credentials,
static_discovery=False,
)
albums = (
lib.albums().list(pageSize=1).execute() # pylint: disable=no-member
)
return albums

try:
(await self.hass.async_add_executor_job(_check_photoslibrary_access))
email = (await self.hass.async_add_executor_job(_get_profile))["email"]
except GoogleApiError as ex:
return self.async_abort(
reason="access_error", description_placeholders={"reason": ex.reason}
)

await self.async_set_unique_id(email)
self._abort_if_unique_id_configured()
Expand Down
103 changes: 52 additions & 51 deletions custom_components/google_photos/strings.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
{
"config": {
"step": {
"pick_implementation": {
"title": "[%key:common::config_flow::title::oauth2_pick_implementation%]"
},
"reauth_confirm": {
"title": "[%key:common::config_flow::title::reauth%]",
"description": "The Google Photos integration needs to re-authenticate your account"
},
"auth": {
"title": "Link Google Account"
}
"config": {
"step": {
"pick_implementation": {
"title": "[%key:common::config_flow::title::oauth2_pick_implementation%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"timeout_connect": "[%key:common::config_flow::error::timeout_connect%]",
"oauth_error": "[%key:common::config_flow::abort::oauth2_error%]",
"missing_configuration": "[%key:common::config_flow::abort::oauth2_missing_configuration%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"invalid_access_token": "[%key:common::config_flow::error::invalid_access_token%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
"reauth_confirm": {
"title": "[%key:common::config_flow::title::reauth%]",
"description": "The Google Photos integration needs to re-authenticate your account"
},
"create_entry": {
"default": "[%key:common::config_flow::create_entry::authenticated%]"
"auth": {
"title": "Link Google Account"
}
},
"options": {
"step": {
"init": {
"menu_options": {
"albumselect": "Select album",
"settings": "Settings"
},
"title": "Adjust Google Photos options"
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"timeout_connect": "[%key:common::config_flow::error::timeout_connect%]",
"oauth_error": "[%key:common::config_flow::abort::oauth2_error%]",
"missing_configuration": "[%key:common::config_flow::abort::oauth2_missing_configuration%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"invalid_access_token": "[%key:common::config_flow::error::invalid_access_token%]",
"unknown": "[%key:common::config_flow::error::unknown%]",
"access_error": "[%key:common::config_flow::error::access_error%]"
},
"create_entry": {
"default": "[%key:common::config_flow::create_entry::authenticated%]"
}
},
"options": {
"step": {
"init": {
"menu_options": {
"albumselect": "Select album",
"settings": "Settings"
},
"albumselect": {
"data": {
"album_id": "Album"
},
"title": "Select album to add",
"description": "Album will be added as a seperate entity after a short period of time."
"title": "Adjust Google Photos options"
},
"albumselect": {
"data": {
"album_id": "Album"
},
"title": "Select album to add",
"description": "Album will be added as a seperate entity after a short period of time."
},
"settings": {
"data": {
"mode": "Selection mode.",
"interval": "Refresh interval",
"attribute_metadata": "Write metadata to attributes"
},
"settings": {
"data": {
"mode": "Selection mode.",
"interval": "Refresh interval",
"attribute_metadata": "Write metadata to attributes"
},
"description": "Choose how and when the next photo is loaded. Warning: a high refresh rate for a lot of albums might lead into google api limits.",
"title": "Adjust Google Photos options."
}
"description": "Choose how and when the next photo is loaded. Warning: a high refresh rate for a lot of albums might lead into google api limits.",
"title": "Adjust Google Photos options."
}
},
"application_credentials": {
"description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Photos. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n\n"
}
}
},
"application_credentials": {
"description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Photos. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n\n"
}
}
23 changes: 12 additions & 11 deletions custom_components/google_photos/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"oauth_error": "Received invalid token data.",
"reauth_successful": "Re-authentication was successful",
"timeout_connect": "Timeout establishing connection",
"unknown": "Unexpected error"
"unknown": "Unexpected error",
"access_error": "Failed to query google api: {reason}"
},
"create_entry": {
"default": "Successfully authenticated"
Expand All @@ -33,18 +34,18 @@
"options": {
"step": {
"init": {
"menu_options": {
"albumselect": "Select album",
"settings": "Settings"
},
"title": "Adjust Google Photos options"
"menu_options": {
"albumselect": "Select album",
"settings": "Settings"
},
"title": "Adjust Google Photos options"
},
"albumselect": {
"data": {
"album_id": "Album"
},
"title": "Select album to add",
"description": "Album will be added as a seperate entity after a short period of time."
"data": {
"album_id": "Album"
},
"title": "Select album to add",
"description": "Album will be added as a seperate entity after a short period of time."
},
"settings": {
"data": {
Expand Down

0 comments on commit 1181ed1

Please sign in to comment.