mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Move google assistant sdk services to separate module (#146434)
This commit is contained in:
parent
def0384608
commit
6a23ad96ca
@ -2,21 +2,13 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import dataclasses
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from gassist_text import TextAssistant
|
from gassist_text import TextAssistant
|
||||||
from google.oauth2.credentials import Credentials
|
from google.oauth2.credentials import Credentials
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.components import conversation
|
from homeassistant.components import conversation
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, Platform
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, Platform
|
||||||
from homeassistant.core import (
|
from homeassistant.core import HomeAssistant
|
||||||
HomeAssistant,
|
|
||||||
ServiceCall,
|
|
||||||
ServiceResponse,
|
|
||||||
SupportsResponse,
|
|
||||||
)
|
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, discovery, intent
|
from homeassistant.helpers import config_validation as cv, discovery, intent
|
||||||
from homeassistant.helpers.config_entry_oauth2_flow import (
|
from homeassistant.helpers.config_entry_oauth2_flow import (
|
||||||
@ -31,21 +23,9 @@ from .helpers import (
|
|||||||
GoogleAssistantSDKConfigEntry,
|
GoogleAssistantSDKConfigEntry,
|
||||||
GoogleAssistantSDKRuntimeData,
|
GoogleAssistantSDKRuntimeData,
|
||||||
InMemoryStorage,
|
InMemoryStorage,
|
||||||
async_send_text_commands,
|
|
||||||
best_matching_language_code,
|
best_matching_language_code,
|
||||||
)
|
)
|
||||||
|
from .services import async_setup_services
|
||||||
SERVICE_SEND_TEXT_COMMAND = "send_text_command"
|
|
||||||
SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND = "command"
|
|
||||||
SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER = "media_player"
|
|
||||||
SERVICE_SEND_TEXT_COMMAND_SCHEMA = vol.All(
|
|
||||||
{
|
|
||||||
vol.Required(SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND): vol.All(
|
|
||||||
cv.ensure_list, [vol.All(str, vol.Length(min=1))]
|
|
||||||
),
|
|
||||||
vol.Optional(SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER): cv.comp_entity_ids,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||||
|
|
||||||
@ -58,6 +38,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async_setup_services(hass)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -81,8 +63,6 @@ async def async_setup_entry(
|
|||||||
mem_storage = InMemoryStorage(hass)
|
mem_storage = InMemoryStorage(hass)
|
||||||
hass.http.register_view(GoogleAssistantSDKAudioView(mem_storage))
|
hass.http.register_view(GoogleAssistantSDKAudioView(mem_storage))
|
||||||
|
|
||||||
await async_setup_service(hass)
|
|
||||||
|
|
||||||
entry.runtime_data = GoogleAssistantSDKRuntimeData(
|
entry.runtime_data = GoogleAssistantSDKRuntimeData(
|
||||||
session=session, mem_storage=mem_storage
|
session=session, mem_storage=mem_storage
|
||||||
)
|
)
|
||||||
@ -105,36 +85,6 @@ async def async_unload_entry(
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_service(hass: HomeAssistant) -> None:
|
|
||||||
"""Add the services for Google Assistant SDK."""
|
|
||||||
|
|
||||||
async def send_text_command(call: ServiceCall) -> ServiceResponse:
|
|
||||||
"""Send a text command to Google Assistant SDK."""
|
|
||||||
commands: list[str] = call.data[SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND]
|
|
||||||
media_players: list[str] | None = call.data.get(
|
|
||||||
SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER
|
|
||||||
)
|
|
||||||
command_response_list = await async_send_text_commands(
|
|
||||||
hass, commands, media_players
|
|
||||||
)
|
|
||||||
if call.return_response:
|
|
||||||
return {
|
|
||||||
"responses": [
|
|
||||||
dataclasses.asdict(command_response)
|
|
||||||
for command_response in command_response_list
|
|
||||||
]
|
|
||||||
}
|
|
||||||
return None
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN,
|
|
||||||
SERVICE_SEND_TEXT_COMMAND,
|
|
||||||
send_text_command,
|
|
||||||
schema=SERVICE_SEND_TEXT_COMMAND_SCHEMA,
|
|
||||||
supports_response=SupportsResponse.OPTIONAL,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class GoogleAssistantConversationAgent(conversation.AbstractConversationAgent):
|
class GoogleAssistantConversationAgent(conversation.AbstractConversationAgent):
|
||||||
"""Google Assistant SDK conversation agent."""
|
"""Google Assistant SDK conversation agent."""
|
||||||
|
|
||||||
|
61
homeassistant/components/google_assistant_sdk/services.py
Normal file
61
homeassistant/components/google_assistant_sdk/services.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
"""Support for Google Assistant SDK."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.core import (
|
||||||
|
HomeAssistant,
|
||||||
|
ServiceCall,
|
||||||
|
ServiceResponse,
|
||||||
|
SupportsResponse,
|
||||||
|
)
|
||||||
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
|
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_FIELD_MEDIA_PLAYER = "media_player"
|
||||||
|
SERVICE_SEND_TEXT_COMMAND_SCHEMA = vol.All(
|
||||||
|
{
|
||||||
|
vol.Required(SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND): vol.All(
|
||||||
|
cv.ensure_list, [vol.All(str, vol.Length(min=1))]
|
||||||
|
),
|
||||||
|
vol.Optional(SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER): cv.comp_entity_ids,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _send_text_command(call: ServiceCall) -> ServiceResponse:
|
||||||
|
"""Send a text command to Google Assistant SDK."""
|
||||||
|
commands: list[str] = call.data[SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND]
|
||||||
|
media_players: list[str] | None = call.data.get(
|
||||||
|
SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER
|
||||||
|
)
|
||||||
|
command_response_list = await async_send_text_commands(
|
||||||
|
call.hass, commands, media_players
|
||||||
|
)
|
||||||
|
if call.return_response:
|
||||||
|
return {
|
||||||
|
"responses": [
|
||||||
|
dataclasses.asdict(command_response)
|
||||||
|
for command_response in command_response_list
|
||||||
|
]
|
||||||
|
}
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def async_setup_services(hass: HomeAssistant) -> None:
|
||||||
|
"""Add the services for Google Assistant SDK."""
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_SEND_TEXT_COMMAND,
|
||||||
|
_send_text_command,
|
||||||
|
schema=SERVICE_SEND_TEXT_COMMAND_SCHEMA,
|
||||||
|
supports_response=SupportsResponse.OPTIONAL,
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user