Add redirect URL in Google Assistant SDK setup (#148076)

This commit is contained in:
tronikos 2025-07-04 07:17:10 -07:00 committed by GitHub
parent 40fcc3b75b
commit 40ec51c0a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 7 deletions

View File

@ -2,6 +2,10 @@
from homeassistant.components.application_credentials import AuthorizationServer from homeassistant.components.application_credentials import AuthorizationServer
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.config_entry_oauth2_flow import (
AUTH_CALLBACK_PATH,
MY_AUTH_CALLBACK_PATH,
)
async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer:
@ -14,12 +18,14 @@ async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationSe
async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, str]: async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, str]:
"""Return description placeholders for the credentials dialog.""" """Return description placeholders for the credentials dialog."""
if "my" in hass.config.components:
redirect_url = MY_AUTH_CALLBACK_PATH
else:
ha_host = hass.config.external_url or "https://YOUR_DOMAIN:PORT"
redirect_url = f"{ha_host}{AUTH_CALLBACK_PATH}"
return { return {
"oauth_consent_url": ( "oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent",
"https://console.cloud.google.com/apis/credentials/consent" "more_info_url": "https://www.home-assistant.io/integrations/google_assistant_sdk/",
),
"more_info_url": (
"https://www.home-assistant.io/integrations/google_assistant_sdk/"
),
"oauth_creds_url": "https://console.cloud.google.com/apis/credentials", "oauth_creds_url": "https://console.cloud.google.com/apis/credentials",
"redirect_url": redirect_url,
} }

View File

@ -46,7 +46,7 @@
} }
}, },
"application_credentials": { "application_credentials": {
"description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Assistant SDK. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type." "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Assistant SDK. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n1. Add `{redirect_url}` under *Authorized redirect URI*."
}, },
"services": { "services": {
"send_text_command": { "send_text_command": {

View File

@ -0,0 +1,36 @@
"""Test the Google Assistant SDK application_credentials."""
import pytest
from homeassistant import setup
from homeassistant.components.google_assistant_sdk.application_credentials import (
async_get_description_placeholders,
)
from homeassistant.core import HomeAssistant
@pytest.mark.parametrize(
("additional_components", "external_url", "expected_redirect_uri"),
[
([], "https://example.com", "https://example.com/auth/external/callback"),
([], None, "https://YOUR_DOMAIN:PORT/auth/external/callback"),
(["my"], "https://example.com", "https://my.home-assistant.io/redirect/oauth"),
],
)
async def test_description_placeholders(
hass: HomeAssistant,
additional_components: list[str],
external_url: str | None,
expected_redirect_uri: str,
) -> None:
"""Test description placeholders."""
for component in additional_components:
assert await setup.async_setup_component(hass, component, {})
hass.config.external_url = external_url
placeholders = await async_get_description_placeholders(hass)
assert placeholders == {
"oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent",
"more_info_url": "https://www.home-assistant.io/integrations/google_assistant_sdk/",
"oauth_creds_url": "https://console.cloud.google.com/apis/credentials",
"redirect_url": expected_redirect_uri,
}