-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Google Assistant SDK integration #82328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Support for Google Assistant SDK.""" | ||
| from __future__ import annotations | ||
|
|
||
| import aiohttp | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry, ConfigEntryState | ||
| from homeassistant.const import CONF_NAME, Platform | ||
| from homeassistant.core import HomeAssistant, ServiceCall | ||
| from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady | ||
| from homeassistant.helpers import discovery | ||
| from homeassistant.helpers.config_entry_oauth2_flow import ( | ||
| OAuth2Session, | ||
| async_get_config_entry_implementation, | ||
| ) | ||
| from homeassistant.helpers.typing import ConfigType | ||
|
|
||
| from .const import DOMAIN | ||
| from .helpers import async_send_text_commands | ||
|
|
||
| SERVICE_SEND_TEXT_COMMAND = "send_text_command" | ||
| SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND = "command" | ||
| SERVICE_SEND_TEXT_COMMAND_SCHEMA = vol.All( | ||
| { | ||
| vol.Required(SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND): vol.All( | ||
| str, vol.Length(min=1) | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: | ||
| """Set up Google Assistant SDK component.""" | ||
| hass.async_create_task( | ||
| discovery.async_load_platform( | ||
| hass, Platform.NOTIFY, DOMAIN, {CONF_NAME: DOMAIN}, config | ||
| ) | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Set up Google Assistant SDK from a config entry.""" | ||
| implementation = await async_get_config_entry_implementation(hass, entry) | ||
| session = OAuth2Session(hass, entry, implementation) | ||
| try: | ||
| await session.async_ensure_token_valid() | ||
| except aiohttp.ClientResponseError as err: | ||
| if 400 <= err.status < 500: | ||
| raise ConfigEntryAuthFailed( | ||
| "OAuth session is not valid, reauth required" | ||
| ) from err | ||
| raise ConfigEntryNotReady from err | ||
| except aiohttp.ClientError as err: | ||
| raise ConfigEntryNotReady from err | ||
| hass.data.setdefault(DOMAIN, {})[entry.entry_id] = session | ||
|
|
||
| await async_setup_service(hass) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Unload a config entry.""" | ||
| hass.data[DOMAIN].pop(entry.entry_id) | ||
| loaded_entries = [ | ||
| entry | ||
| for entry in hass.config_entries.async_entries(DOMAIN) | ||
| if entry.state == ConfigEntryState.LOADED | ||
| ] | ||
| if len(loaded_entries) == 1: | ||
| for service_name in hass.services.async_services()[DOMAIN]: | ||
| hass.services.async_remove(DOMAIN, service_name) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def async_setup_service(hass: HomeAssistant) -> None: | ||
| """Add the services for Google Assistant SDK.""" | ||
|
|
||
| async def send_text_command(call: ServiceCall) -> None: | ||
| """Send a text command to Google Assistant SDK.""" | ||
| command: str = call.data[SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND] | ||
| await async_send_text_commands([command], hass) | ||
|
|
||
| hass.services.async_register( | ||
| DOMAIN, | ||
| SERVICE_SEND_TEXT_COMMAND, | ||
| send_text_command, | ||
| schema=SERVICE_SEND_TEXT_COMMAND_SCHEMA, | ||
| ) |
22 changes: 22 additions & 0 deletions
22
homeassistant/components/google_assistant_sdk/application_credentials.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """application_credentials platform for Google Assistant SDK.""" | ||
| import oauth2client | ||
|
|
||
| from homeassistant.components.application_credentials import AuthorizationServer | ||
| from homeassistant.core import HomeAssistant | ||
|
|
||
|
|
||
| async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: | ||
| """Return authorization server.""" | ||
| return AuthorizationServer( | ||
| oauth2client.GOOGLE_AUTH_URI, | ||
| oauth2client.GOOGLE_TOKEN_URI, | ||
| ) | ||
|
|
||
|
|
||
| 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_assistant_sdk/", | ||
| "oauth_creds_url": "https://console.cloud.google.com/apis/credentials", | ||
| } |
67 changes: 67 additions & 0 deletions
67
homeassistant/components/google_assistant_sdk/config_flow.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Config flow for Google Assistant SDK integration.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.data_entry_flow import FlowResult | ||
| from homeassistant.helpers import config_entry_oauth2_flow | ||
|
|
||
| from .const import DEFAULT_NAME, DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class OAuth2FlowHandler( | ||
| config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN | ||
| ): | ||
| """Config flow to handle Google Assistant SDK OAuth2 authentication.""" | ||
|
|
||
| DOMAIN = DOMAIN | ||
|
|
||
| reauth_entry: ConfigEntry | None = None | ||
|
|
||
| @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": "https://www.googleapis.com/auth/assistant-sdk-prototype", | ||
| # Add params to ensure we get back a refresh token | ||
| "access_type": "offline", | ||
| "prompt": "consent", | ||
| } | ||
|
|
||
| async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult: | ||
| """Perform reauth upon an API authentication error.""" | ||
| self.reauth_entry = self.hass.config_entries.async_get_entry( | ||
| self.context["entry_id"] | ||
| ) | ||
| return await self.async_step_reauth_confirm() | ||
|
|
||
| async def async_step_reauth_confirm( | ||
| self, user_input: dict[str, Any] | None = None | ||
| ) -> FlowResult: | ||
| """Confirm reauth dialog.""" | ||
| if user_input is None: | ||
| return self.async_show_form(step_id="reauth_confirm") | ||
| return await self.async_step_user() | ||
|
|
||
| async def async_oauth_create_entry(self, data: dict[str, Any]) -> FlowResult: | ||
| """Create an entry for the flow, or update existing entry.""" | ||
| if self.reauth_entry: | ||
| self.hass.config_entries.async_update_entry(self.reauth_entry, data=data) | ||
| await self.hass.config_entries.async_reload(self.reauth_entry.entry_id) | ||
| return self.async_abort(reason="reauth_successful") | ||
|
|
||
| if self._async_current_entries(): | ||
| # Config entry already exists, only one allowed. | ||
| return self.async_abort(reason="single_instance_allowed") | ||
|
|
||
| return self.async_create_entry(title=DEFAULT_NAME, data=data) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| """Constants for Google Assistant SDK integration.""" | ||
| from __future__ import annotations | ||
|
|
||
| DOMAIN = "google_assistant_sdk" | ||
|
|
||
| DEFAULT_NAME = "Google Assistant SDK" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Helper classes for Google Assistant SDK integration.""" | ||
| from __future__ import annotations | ||
|
|
||
| import aiohttp | ||
| from gassist_text import TextAssistant | ||
| from google.oauth2.credentials import Credentials | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_ACCESS_TOKEN | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
|
|
||
| async def async_send_text_commands(commands: list[str], hass: HomeAssistant) -> None: | ||
| """Send text commands to Google Assistant Service.""" | ||
| # There can only be 1 entry (config_flow has single_instance_allowed) | ||
| entry: ConfigEntry = hass.config_entries.async_entries(DOMAIN)[0] | ||
|
|
||
| session: OAuth2Session = hass.data[DOMAIN].get(entry.entry_id) | ||
| try: | ||
| await session.async_ensure_token_valid() | ||
| except aiohttp.ClientResponseError as err: | ||
| if 400 <= err.status < 500: | ||
| entry.async_start_reauth(hass) | ||
| raise err | ||
|
|
||
| credentials = Credentials(session.token[CONF_ACCESS_TOKEN]) | ||
| with TextAssistant(credentials) as assistant: | ||
| for command in commands: | ||
| assistant.assist(command) |
11 changes: 11 additions & 0 deletions
11
homeassistant/components/google_assistant_sdk/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "domain": "google_assistant_sdk", | ||
| "name": "Google Assistant SDK", | ||
| "config_flow": true, | ||
| "dependencies": ["application_credentials"], | ||
| "documentation": "https://www.home-assistant.io/integrations/google_assistant_sdk/", | ||
| "requirements": ["gassist-text==0.0.4"], | ||
| "codeowners": ["@tronikos"], | ||
| "iot_class": "cloud_polling", | ||
| "integration_type": "service" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Support for Google Assistant SDK broadcast notifications.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType | ||
|
|
||
| from .helpers import async_send_text_commands | ||
|
|
||
|
|
||
| async def async_get_service( | ||
| hass: HomeAssistant, | ||
| config: ConfigType, | ||
| discovery_info: DiscoveryInfoType | None = None, | ||
| ) -> BaseNotificationService: | ||
| """Get the broadcast notification service.""" | ||
| return BroadcastNotificationService(hass) | ||
|
|
||
|
|
||
| class BroadcastNotificationService(BaseNotificationService): | ||
| """Implement broadcast notification service.""" | ||
|
|
||
| def __init__(self, hass: HomeAssistant) -> None: | ||
| """Initialize the service.""" | ||
| self.hass = hass | ||
|
|
||
| async def async_send_message(self, message: str = "", **kwargs: Any) -> None: | ||
| """Send a message.""" | ||
| if not message: | ||
| return | ||
|
|
||
| commands = [] | ||
| targets = kwargs.get(ATTR_TARGET) | ||
| if not targets: | ||
| commands.append(f"broadcast {message}") | ||
| else: | ||
| for target in targets: | ||
| commands.append(f"broadcast to {target} {message}") | ||
| await async_send_text_commands(commands, self.hass) |
10 changes: 10 additions & 0 deletions
10
homeassistant/components/google_assistant_sdk/services.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| send_text_command: | ||
| name: Send text command | ||
| description: Send a command as a text query to Google Assistant. | ||
| fields: | ||
| command: | ||
| name: Command | ||
| description: Command to send to Google Assistant. | ||
| example: turn off kitchen TV | ||
| selector: | ||
| text: |
33 changes: 33 additions & 0 deletions
33
homeassistant/components/google_assistant_sdk/strings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "config": { | ||
| "step": { | ||
| "pick_implementation": { | ||
| "title": "[%key:common::config_flow::title::oauth2_pick_implementation%]" | ||
| }, | ||
| "auth": { | ||
| "title": "Link Google Account" | ||
| }, | ||
| "reauth_confirm": { | ||
| "title": "[%key:common::config_flow::title::reauth%]", | ||
| "description": "The Google Assistant SDK integration needs to re-authenticate your account" | ||
| } | ||
| }, | ||
| "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%]" | ||
| }, | ||
| "create_entry": { | ||
| "default": "[%key:common::config_flow::create_entry::authenticated%]" | ||
| } | ||
| }, | ||
| "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 click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n\n" | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
homeassistant/components/google_assistant_sdk/translations/en.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "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 click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n\n" | ||
| }, | ||
| "config": { | ||
| "abort": { | ||
| "already_configured": "Account is already configured", | ||
| "already_in_progress": "Configuration flow is already in progress", | ||
| "cannot_connect": "Failed to connect", | ||
| "invalid_access_token": "Invalid access token", | ||
| "missing_configuration": "The component is not configured. Please follow the documentation.", | ||
| "oauth_error": "Received invalid token data.", | ||
| "reauth_successful": "Re-authentication was successful", | ||
| "timeout_connect": "Timeout establishing connection", | ||
| "unknown": "Unexpected error" | ||
| }, | ||
| "create_entry": { | ||
| "default": "Successfully authenticated" | ||
| }, | ||
| "step": { | ||
| "auth": { | ||
| "title": "Link Google Account" | ||
| }, | ||
| "pick_implementation": { | ||
| "title": "Pick Authentication Method" | ||
| }, | ||
| "reauth_confirm": { | ||
| "description": "The Google Assistant SDK integration needs to re-authenticate your account", | ||
| "title": "Reauthenticate Integration" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.