From e74c711ef381ef3f95fe4f66cfe759260c2762b7 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Wed, 8 Jun 2022 07:09:32 -0700 Subject: [PATCH] Add application credentials description strings (#73014) --- .../application_credentials/__init__.py | 24 ++++++++++++++++--- .../google/application_credentials.py | 9 +++++++ homeassistant/components/google/strings.json | 3 +++ .../components/google/translations/en.json | 3 +++ script/hassfest/translations.py | 3 +++ .../application_credentials/test_init.py | 6 ++++- 6 files changed, 44 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/application_credentials/__init__.py b/homeassistant/components/application_credentials/__init__.py index 1a128c5c378..14ae049cfca 100644 --- a/homeassistant/components/application_credentials/__init__.py +++ b/homeassistant/components/application_credentials/__init__.py @@ -253,6 +253,11 @@ class ApplicationCredentialsProtocol(Protocol): ) -> config_entry_oauth2_flow.AbstractOAuth2Implementation: """Return a custom auth implementation.""" + async def async_get_description_placeholders( + self, hass: HomeAssistant + ) -> dict[str, str]: + """Return description placeholders for the credentials dialog.""" + async def _get_platform( hass: HomeAssistant, integration_domain: str @@ -282,6 +287,14 @@ async def _get_platform( return platform +async def _async_integration_config(hass: HomeAssistant, domain: str) -> dict[str, Any]: + platform = await _get_platform(hass, domain) + if platform and hasattr(platform, "async_get_description_placeholders"): + placeholders = await platform.async_get_description_placeholders(hass) + return {"description_placeholders": placeholders} + return {} + + @websocket_api.websocket_command( {vol.Required("type"): "application_credentials/config"} ) @@ -290,6 +303,11 @@ async def handle_integration_list( hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] ) -> None: """Handle integrations command.""" - connection.send_result( - msg["id"], {"domains": await async_get_application_credentials(hass)} - ) + domains = await async_get_application_credentials(hass) + result = { + "domains": domains, + "integrations": { + domain: await _async_integration_config(hass, domain) for domain in domains + }, + } + connection.send_result(msg["id"], result) diff --git a/homeassistant/components/google/application_credentials.py b/homeassistant/components/google/application_credentials.py index 2f1fcba8084..3d557630b05 100644 --- a/homeassistant/components/google/application_credentials.py +++ b/homeassistant/components/google/application_credentials.py @@ -21,3 +21,12 @@ async def async_get_auth_implementation( ) -> config_entry_oauth2_flow.AbstractOAuth2Implementation: """Return auth implementation.""" return DeviceAuth(hass, auth_domain, credential, AUTHORIZATION_SERVER) + + +async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, str]: + """Return description placeholders for the credentials dialog.""" + return { + "oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent", + "more_info_url": "https://www.home-assistant.io/integrations/google/", + "oauth_creds_url": "https://console.cloud.google.com/apis/credentials", + } diff --git a/homeassistant/components/google/strings.json b/homeassistant/components/google/strings.json index e32223627be..6652806cd0f 100644 --- a/homeassistant/components/google/strings.json +++ b/homeassistant/components/google/strings.json @@ -36,5 +36,8 @@ } } } + }, + "application_credentials": { + "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Calendar. You also need to create Application Credentials linked to your Calendar:\n1. Go to [Credentials]({oauth_creds_url}) and click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **TV and Limited Input devices** for the Application Type.\n\n" } } diff --git a/homeassistant/components/google/translations/en.json b/homeassistant/components/google/translations/en.json index 58c89834ca5..54936b0d81c 100644 --- a/homeassistant/components/google/translations/en.json +++ b/homeassistant/components/google/translations/en.json @@ -1,4 +1,7 @@ { + "application_credentials": { + "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Calendar. You also need to create Application Credentials linked to your Calendar:\n1. Go to [Credentials]({oauth_creds_url}) page and click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **TV and Limited Input devices** for the Application Type.\n\n" + }, "config": { "abort": { "already_configured": "Account is already configured", diff --git a/script/hassfest/translations.py b/script/hassfest/translations.py index 0dcdbc133a6..a1f520808f6 100644 --- a/script/hassfest/translations.py +++ b/script/hassfest/translations.py @@ -224,6 +224,9 @@ def gen_strings_schema(config: Config, integration: Integration): ), slug_validator=vol.Any("_", cv.slug), ), + vol.Optional("application_credentials"): { + vol.Optional("description"): cv.string_with_no_html, + }, } ) diff --git a/tests/components/application_credentials/test_init.py b/tests/components/application_credentials/test_init.py index b89a60f42e4..dd5995a8f4f 100644 --- a/tests/components/application_credentials/test_init.py +++ b/tests/components/application_credentials/test_init.py @@ -701,7 +701,11 @@ async def test_websocket_integration_list(ws_client: ClientFixture): "homeassistant.loader.APPLICATION_CREDENTIALS", ["example1", "example2"] ): assert await client.cmd_result("config") == { - "domains": ["example1", "example2"] + "domains": ["example1", "example2"], + "integrations": { + "example1": {}, + "example2": {}, + }, }