2017-10-18 05:00:59 +00:00
|
|
|
"""
|
|
|
|
Support for Google Actions Smart Home Control.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/google_assistant/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2018-06-25 17:05:07 +00:00
|
|
|
from aiohttp.web import Request, Response
|
2017-11-04 19:04:05 +00:00
|
|
|
|
2017-10-18 05:00:59 +00:00
|
|
|
# Typing imports
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2018-07-01 15:57:01 +00:00
|
|
|
from homeassistant.core import callback
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
from .const import (
|
2017-11-13 16:32:23 +00:00
|
|
|
GOOGLE_ASSISTANT_API_ENDPOINT,
|
|
|
|
CONF_EXPOSE_BY_DEFAULT,
|
|
|
|
CONF_EXPOSED_DOMAINS,
|
2018-01-09 23:14:56 +00:00
|
|
|
CONF_ENTITY_CONFIG,
|
|
|
|
CONF_EXPOSE,
|
2017-11-13 16:32:23 +00:00
|
|
|
)
|
2018-03-08 22:39:10 +00:00
|
|
|
from .smart_home import async_handle_message
|
|
|
|
from .helpers import Config
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-12-31 23:04:49 +00:00
|
|
|
@callback
|
|
|
|
def async_register_http(hass, cfg):
|
|
|
|
"""Register HTTP views for Google Assistant."""
|
|
|
|
expose_by_default = cfg.get(CONF_EXPOSE_BY_DEFAULT)
|
|
|
|
exposed_domains = cfg.get(CONF_EXPOSED_DOMAINS)
|
2018-01-10 03:47:24 +00:00
|
|
|
entity_config = cfg.get(CONF_ENTITY_CONFIG) or {}
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2017-12-31 23:04:49 +00:00
|
|
|
def is_exposed(entity) -> bool:
|
2017-10-18 05:00:59 +00:00
|
|
|
"""Determine if an entity should be exposed to Google Assistant."""
|
|
|
|
if entity.attributes.get('view') is not None:
|
|
|
|
# Ignore entities that are views
|
|
|
|
return False
|
|
|
|
|
2018-01-09 23:14:56 +00:00
|
|
|
explicit_expose = \
|
|
|
|
entity_config.get(entity.entity_id, {}).get(CONF_EXPOSE)
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
domain_exposed_by_default = \
|
2018-01-09 23:14:56 +00:00
|
|
|
expose_by_default and entity.domain in exposed_domains
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
# Expose an entity if the entity's domain is exposed by default and
|
|
|
|
# the configuration doesn't explicitly exclude it from being
|
|
|
|
# exposed, or if the entity is explicitly exposed
|
|
|
|
is_default_exposed = \
|
|
|
|
domain_exposed_by_default and explicit_expose is not False
|
|
|
|
|
|
|
|
return is_default_exposed or explicit_expose
|
|
|
|
|
2017-12-31 23:04:49 +00:00
|
|
|
hass.http.register_view(
|
2018-09-26 06:57:55 +00:00
|
|
|
GoogleAssistantView(is_exposed, entity_config))
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
|
2017-12-31 23:04:49 +00:00
|
|
|
class GoogleAssistantView(HomeAssistantView):
|
|
|
|
"""Handle Google Assistant requests."""
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2017-12-31 23:04:49 +00:00
|
|
|
url = GOOGLE_ASSISTANT_API_ENDPOINT
|
|
|
|
name = 'api:google_assistant'
|
2018-09-26 06:57:55 +00:00
|
|
|
requires_auth = True
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2018-09-26 06:57:55 +00:00
|
|
|
def __init__(self, is_exposed, entity_config):
|
2017-12-31 23:04:49 +00:00
|
|
|
"""Initialize the Google Assistant request handler."""
|
2018-09-26 06:57:55 +00:00
|
|
|
self.is_exposed = is_exposed
|
|
|
|
self.entity_config = entity_config
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def post(self, request: Request) -> Response:
|
2017-10-18 05:00:59 +00:00
|
|
|
"""Handle Google Assistant requests."""
|
2018-04-28 23:26:20 +00:00
|
|
|
message = await request.json() # type: dict
|
2018-09-26 06:57:55 +00:00
|
|
|
config = Config(self.is_exposed,
|
|
|
|
request['hass_user'].id,
|
|
|
|
self.entity_config)
|
2018-04-28 23:26:20 +00:00
|
|
|
result = await async_handle_message(
|
2018-09-26 06:57:55 +00:00
|
|
|
request.app['hass'], config, message)
|
2017-12-31 23:04:49 +00:00
|
|
|
return self.json(result)
|