Use HA native OAuth2 flow for google assistant components (#16848)

* Use HA native OAuth2 flow for google assistant components

* Lint

* Force breaking changes

* Fix CONFIG_SCHEMA
This commit is contained in:
Jason Hu authored and Paulus Schoutsen committed 2018-09-26 08:57:55 +02:00
1 parent 3cba2e695c
commit 92a5068977
6 files changed
+43 -162

No files matched your search

@@ -6,7 +6,6 @@ https://home-assistant.io/components/google_assistant/
"""
import logging
from aiohttp.hdrs import AUTHORIZATION
from aiohttp.web import Request, Response
# Typing imports
@@ -15,10 +14,8 @@ from homeassistant.core import callback
from .const import (
GOOGLE_ASSISTANT_API_ENDPOINT,
CONF_ACCESS_TOKEN,
CONF_EXPOSE_BY_DEFAULT,
CONF_EXPOSED_DOMAINS,
CONF_AGENT_USER_ID,
CONF_ENTITY_CONFIG,
CONF_EXPOSE,
)
@@ -31,10 +28,8 @@ _LOGGER = logging.getLogger(__name__)
@callback
def async_register_http(hass, cfg):
"""Register HTTP views for Google Assistant."""
access_token = cfg.get(CONF_ACCESS_TOKEN)
expose_by_default = cfg.get(CONF_EXPOSE_BY_DEFAULT)
exposed_domains = cfg.get(CONF_EXPOSED_DOMAINS)
agent_user_id = cfg.get(CONF_AGENT_USER_ID)
entity_config = cfg.get(CONF_ENTITY_CONFIG) or {}
def is_exposed(entity) -> bool:
@@ -57,9 +52,8 @@ def async_register_http(hass, cfg):
return is_default_exposed or explicit_expose
gass_config = Config(is_exposed, agent_user_id, entity_config)
hass.http.register_view(
GoogleAssistantView(access_token, gass_config))
GoogleAssistantView(is_exposed, entity_config))
class GoogleAssistantView(HomeAssistantView):
@@ -67,20 +61,19 @@ class GoogleAssistantView(HomeAssistantView):
url = GOOGLE_ASSISTANT_API_ENDPOINT
name = 'api:google_assistant'
requires_auth = False # Uses access token from oauth flow
requires_auth = True
def __init__(self, access_token, gass_config):
def __init__(self, is_exposed, entity_config):
"""Initialize the Google Assistant request handler."""
self.access_token = access_token
self.gass_config = gass_config
self.is_exposed = is_exposed
self.entity_config = entity_config
async def post(self, request: Request) -> Response:
"""Handle Google Assistant requests."""
auth = request.headers.get(AUTHORIZATION, None)
if 'Bearer {}'.format(self.access_token) != auth:
return self.json_message("missing authorization", status_code=401)
message = await request.json() # type: dict
config = Config(self.is_exposed,
request['hass_user'].id,
self.entity_config)
result = await async_handle_message(
request.app['hass'], self.gass_config, message)
request.app['hass'], config, message)
return self.json(result)