hass-core/homeassistant/components/google_assistant_sdk/helpers.py
tronikos 5d31673465
Google Assistant SDK integration (#82328)
* Copy google_sheets to google_assistant_sdk

This is to improve diff of the next commit with the actual implementation.

Commands used:
cp -r homeassistant/components/google_sheets/ homeassistant/components/google_assistant_sdk/
cp -r tests/components/google_sheets/ tests/components/google_assistant_sdk/

find homeassistant/components/google_assistant_sdk/ tests/components/google_assistant_sdk/ -type f | xargs sed -i \
-e 's@google_sheets@google_assistant_sdk@g' \
-e 's@Google Sheets@Google Assistant SDK@g' \
-e 's@tkdrob@tronikos@g'

* Google Assistant SDK integration
Allows sending commands and broadcast messages to Google Assistant.

* Remove unnecessary async_entry_has_scopes check

* Bump gassist-text to fix protobuf dependency
2022-12-09 07:14:43 -08:00

32 lines
1.2 KiB
Python

"""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)