2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Google Actions Smart Home Control."""
|
2019-10-19 05:37:44 +00:00
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
2017-10-18 05:00:59 +00:00
|
|
|
import logging
|
2019-10-19 05:37:44 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
import jwt
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2019-10-19 05:37:44 +00:00
|
|
|
from aiohttp import ClientResponseError, ClientError
|
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
|
Add support for locks in google assistant component (#18233)
* Add support for locks in google assistant component
This is supported by the smarthome API, but there is no documentation
for it. This work is based on an article I found with screenshots of
documentation that was erroneously uploaded:
https://www.androidpolice.com/2018/01/17/google-assistant-home-can-now-natively-control-smart-locks-august-vivint-first-supported/
Google Assistant now supports unlocking certain locks - Nest and August
come to mind - via this API, and this commit allows Home Assistant to
do so as well.
Notably, I've added a config option `allow_unlock` that controls
whether we actually honor requests to unlock a lock via the google
assistant. It defaults to false.
Additionally, we add the functionNotSupported error, which makes a
little more sense when we're unable to execute the desired state
transition.
https://developers.google.com/actions/reference/smarthome/errors-exceptions#exception_list
* Fix linter warnings
* Ensure that certain groups are never exposed to cloud entities
For example, the group.all_locks entity - we should probably never
expose this to third party cloud integrations. It's risky.
This is not configurable, but can be extended by adding to the
cloud.const.NEVER_EXPOSED_ENTITIES array.
It's implemented in a modestly hacky fashion, because we determine
whether or not a entity should be excluded/included in several ways.
Notably, we define this array in the top level const.py, to avoid
circular import problems between the cloud/alexa components.
2018-11-06 09:39:10 +00:00
|
|
|
from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES
|
2019-10-19 05:37:44 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.util import dt as dt_util
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
from .const import (
|
2017-11-13 16:32:23 +00:00
|
|
|
GOOGLE_ASSISTANT_API_ENDPOINT,
|
2019-10-19 05:37:44 +00:00
|
|
|
CONF_API_KEY,
|
2017-11-13 16:32:23 +00:00
|
|
|
CONF_EXPOSE_BY_DEFAULT,
|
|
|
|
CONF_EXPOSED_DOMAINS,
|
2018-01-09 23:14:56 +00:00
|
|
|
CONF_ENTITY_CONFIG,
|
|
|
|
CONF_EXPOSE,
|
2019-10-19 05:37:44 +00:00
|
|
|
CONF_REPORT_STATE,
|
2019-04-19 21:50:21 +00:00
|
|
|
CONF_SECURE_DEVICES_PIN,
|
2019-10-19 05:37:44 +00:00
|
|
|
CONF_SERVICE_ACCOUNT,
|
|
|
|
CONF_CLIENT_EMAIL,
|
|
|
|
CONF_PRIVATE_KEY,
|
|
|
|
HOMEGRAPH_TOKEN_URL,
|
|
|
|
HOMEGRAPH_SCOPE,
|
|
|
|
REPORT_STATE_BASE_URL,
|
|
|
|
REQUEST_SYNC_BASE_URL,
|
2019-04-19 21:50:21 +00:00
|
|
|
)
|
2018-03-08 22:39:10 +00:00
|
|
|
from .smart_home import async_handle_message
|
2019-06-21 09:17:21 +00:00
|
|
|
from .helpers import AbstractConfig
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-19 05:37:44 +00:00
|
|
|
def _get_homegraph_jwt(time, iss, key):
|
|
|
|
now = int(time.timestamp())
|
|
|
|
|
|
|
|
jwt_raw = {
|
|
|
|
"iss": iss,
|
|
|
|
"scope": HOMEGRAPH_SCOPE,
|
|
|
|
"aud": HOMEGRAPH_TOKEN_URL,
|
|
|
|
"iat": now,
|
|
|
|
"exp": now + 3600,
|
|
|
|
}
|
|
|
|
return jwt.encode(jwt_raw, key, algorithm="RS256").decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
async def _get_homegraph_token(hass, jwt_signed):
|
|
|
|
headers = {
|
|
|
|
"Authorization": "Bearer {}".format(jwt_signed),
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
|
|
"assertion": jwt_signed,
|
|
|
|
}
|
|
|
|
|
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
async with session.post(HOMEGRAPH_TOKEN_URL, headers=headers, data=data) as res:
|
|
|
|
res.raise_for_status()
|
|
|
|
return await res.json()
|
|
|
|
|
|
|
|
|
2019-06-21 09:17:21 +00:00
|
|
|
class GoogleConfig(AbstractConfig):
|
|
|
|
"""Config for manual setup of Google."""
|
|
|
|
|
2019-10-03 11:02:38 +00:00
|
|
|
def __init__(self, hass, config):
|
2019-06-21 09:17:21 +00:00
|
|
|
"""Initialize the config."""
|
2019-10-03 11:02:38 +00:00
|
|
|
super().__init__(hass)
|
2019-06-21 09:17:21 +00:00
|
|
|
self._config = config
|
2019-10-19 05:37:44 +00:00
|
|
|
self._access_token = None
|
|
|
|
self._access_token_renew = None
|
2019-06-21 09:17:21 +00:00
|
|
|
|
2019-10-03 11:02:38 +00:00
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
"""Return if Google is enabled."""
|
|
|
|
return True
|
|
|
|
|
2019-06-21 09:17:21 +00:00
|
|
|
@property
|
|
|
|
def agent_user_id(self):
|
|
|
|
"""Return Agent User Id to use for query responses."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_config(self):
|
|
|
|
"""Return entity config."""
|
2019-06-27 19:17:42 +00:00
|
|
|
return self._config.get(CONF_ENTITY_CONFIG) or {}
|
2019-06-21 09:17:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def secure_devices_pin(self):
|
|
|
|
"""Return entity config."""
|
|
|
|
return self._config.get(CONF_SECURE_DEVICES_PIN)
|
|
|
|
|
2019-10-19 05:37:44 +00:00
|
|
|
@property
|
|
|
|
def should_report_state(self):
|
|
|
|
"""Return if states should be proactively reported."""
|
|
|
|
return self._config.get(CONF_REPORT_STATE)
|
|
|
|
|
2019-06-21 09:17:21 +00:00
|
|
|
def should_expose(self, state) -> bool:
|
|
|
|
"""Return if entity should be exposed."""
|
|
|
|
expose_by_default = self._config.get(CONF_EXPOSE_BY_DEFAULT)
|
|
|
|
exposed_domains = self._config.get(CONF_EXPOSED_DOMAINS)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if state.attributes.get("view") is not None:
|
2017-10-18 05:00:59 +00:00
|
|
|
# Ignore entities that are views
|
|
|
|
return False
|
|
|
|
|
2019-06-21 09:17:21 +00:00
|
|
|
if state.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES:
|
Add support for locks in google assistant component (#18233)
* Add support for locks in google assistant component
This is supported by the smarthome API, but there is no documentation
for it. This work is based on an article I found with screenshots of
documentation that was erroneously uploaded:
https://www.androidpolice.com/2018/01/17/google-assistant-home-can-now-natively-control-smart-locks-august-vivint-first-supported/
Google Assistant now supports unlocking certain locks - Nest and August
come to mind - via this API, and this commit allows Home Assistant to
do so as well.
Notably, I've added a config option `allow_unlock` that controls
whether we actually honor requests to unlock a lock via the google
assistant. It defaults to false.
Additionally, we add the functionNotSupported error, which makes a
little more sense when we're unable to execute the desired state
transition.
https://developers.google.com/actions/reference/smarthome/errors-exceptions#exception_list
* Fix linter warnings
* Ensure that certain groups are never exposed to cloud entities
For example, the group.all_locks entity - we should probably never
expose this to third party cloud integrations. It's risky.
This is not configurable, but can be extended by adding to the
cloud.const.NEVER_EXPOSED_ENTITIES array.
It's implemented in a modestly hacky fashion, because we determine
whether or not a entity should be excluded/included in several ways.
Notably, we define this array in the top level const.py, to avoid
circular import problems between the cloud/alexa components.
2018-11-06 09:39:10 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
explicit_expose = self.entity_config.get(state.entity_id, {}).get(CONF_EXPOSE)
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
domain_exposed_by_default = (
|
2019-06-21 09:17:21 +00:00
|
|
|
expose_by_default and state.domain in exposed_domains
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
is_default_exposed = domain_exposed_by_default and explicit_expose is not False
|
2017-10-18 05:00:59 +00:00
|
|
|
|
|
|
|
return is_default_exposed or explicit_expose
|
|
|
|
|
2019-06-21 09:17:21 +00:00
|
|
|
def should_2fa(self, state):
|
|
|
|
"""If an entity should have 2FA checked."""
|
|
|
|
return True
|
2019-04-19 21:50:21 +00:00
|
|
|
|
2019-11-26 21:47:13 +00:00
|
|
|
async def _async_request_sync_devices(self, agent_user_id: str):
|
|
|
|
if CONF_API_KEY in self._config:
|
|
|
|
await self.async_call_homegraph_api_key(
|
|
|
|
REQUEST_SYNC_BASE_URL, {"agentUserId": agent_user_id}
|
|
|
|
)
|
|
|
|
elif CONF_SERVICE_ACCOUNT in self._config:
|
|
|
|
await self.async_call_homegraph_api(
|
|
|
|
REQUEST_SYNC_BASE_URL, {"agentUserId": agent_user_id}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
_LOGGER.error("No configuration for request_sync available")
|
|
|
|
|
2019-10-19 05:37:44 +00:00
|
|
|
async def _async_update_token(self, force=False):
|
|
|
|
if CONF_SERVICE_ACCOUNT not in self._config:
|
|
|
|
_LOGGER.error("Trying to get homegraph api token without service account")
|
|
|
|
return
|
|
|
|
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
if not self._access_token or now > self._access_token_renew or force:
|
|
|
|
token = await _get_homegraph_token(
|
|
|
|
self.hass,
|
|
|
|
_get_homegraph_jwt(
|
|
|
|
now,
|
|
|
|
self._config[CONF_SERVICE_ACCOUNT][CONF_CLIENT_EMAIL],
|
|
|
|
self._config[CONF_SERVICE_ACCOUNT][CONF_PRIVATE_KEY],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self._access_token = token["access_token"]
|
|
|
|
self._access_token_renew = now + timedelta(seconds=token["expires_in"])
|
|
|
|
|
2019-11-26 21:47:13 +00:00
|
|
|
async def async_call_homegraph_api_key(self, url, data):
|
|
|
|
"""Call a homegraph api with api key authentication."""
|
|
|
|
websession = async_get_clientsession(self.hass)
|
|
|
|
try:
|
|
|
|
res = await websession.post(
|
|
|
|
url, params={"key": self._config.get(CONF_API_KEY)}, json=data
|
|
|
|
)
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Response on %s with data %s was %s", url, data, await res.text()
|
|
|
|
)
|
|
|
|
res.raise_for_status()
|
|
|
|
return res.status
|
|
|
|
except ClientResponseError as error:
|
|
|
|
_LOGGER.error("Request for %s failed: %d", url, error.status)
|
|
|
|
return error.status
|
|
|
|
except (asyncio.TimeoutError, ClientError):
|
|
|
|
_LOGGER.error("Could not contact %s", url)
|
|
|
|
return 500
|
|
|
|
|
2019-10-19 05:37:44 +00:00
|
|
|
async def async_call_homegraph_api(self, url, data):
|
|
|
|
"""Call a homegraph api with authenticaiton."""
|
|
|
|
session = async_get_clientsession(self.hass)
|
|
|
|
|
|
|
|
async def _call():
|
|
|
|
headers = {
|
|
|
|
"Authorization": "Bearer {}".format(self._access_token),
|
|
|
|
"X-GFE-SSL": "yes",
|
|
|
|
}
|
|
|
|
async with session.post(url, headers=headers, json=data) as res:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Response on %s with data %s was %s", url, data, await res.text()
|
|
|
|
)
|
|
|
|
res.raise_for_status()
|
2019-11-26 21:47:13 +00:00
|
|
|
return res.status
|
2019-10-19 05:37:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await self._async_update_token()
|
|
|
|
try:
|
2019-11-26 21:47:13 +00:00
|
|
|
return await _call()
|
2019-10-19 05:37:44 +00:00
|
|
|
except ClientResponseError as error:
|
|
|
|
if error.status == 401:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Request for %s unauthorized, renewing token and retrying", url
|
|
|
|
)
|
|
|
|
await self._async_update_token(True)
|
2019-11-26 21:47:13 +00:00
|
|
|
return await _call()
|
|
|
|
raise
|
2019-10-19 05:37:44 +00:00
|
|
|
except ClientResponseError as error:
|
|
|
|
_LOGGER.error("Request for %s failed: %d", url, error.status)
|
2019-11-26 21:47:13 +00:00
|
|
|
return error.status
|
2019-10-19 05:37:44 +00:00
|
|
|
except (asyncio.TimeoutError, ClientError):
|
|
|
|
_LOGGER.error("Could not contact %s", url)
|
2019-11-26 21:47:13 +00:00
|
|
|
return 500
|
2019-10-19 05:37:44 +00:00
|
|
|
|
|
|
|
async def async_report_state(self, message):
|
|
|
|
"""Send a state report to Google."""
|
|
|
|
data = {
|
|
|
|
"requestId": uuid4().hex,
|
|
|
|
"agentUserId": (await self.hass.auth.async_get_owner()).id,
|
|
|
|
"payload": message,
|
|
|
|
}
|
|
|
|
await self.async_call_homegraph_api(REPORT_STATE_BASE_URL, data)
|
|
|
|
|
2019-06-21 09:17:21 +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
|
2019-07-31 19:25:30 +00:00
|
|
|
name = "api:google_assistant"
|
2018-09-26 06:57:55 +00:00
|
|
|
requires_auth = True
|
2017-10-18 05:00:59 +00:00
|
|
|
|
2019-04-19 21:50:21 +00:00
|
|
|
def __init__(self, config):
|
2017-12-31 23:04:49 +00:00
|
|
|
"""Initialize the Google Assistant request handler."""
|
2019-04-19 21:50:21 +00:00
|
|
|
self.config = 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."""
|
2019-09-07 06:48:58 +00:00
|
|
|
message: dict = await request.json()
|
2018-04-28 23:26:20 +00:00
|
|
|
result = await async_handle_message(
|
2019-07-31 19:25:30 +00:00
|
|
|
request.app["hass"], self.config, request["hass_user"].id, message
|
|
|
|
)
|
2017-12-31 23:04:49 +00:00
|
|
|
return self.json(result)
|