2017-08-29 20:40:08 +00:00
|
|
|
"""The HTTP api to control the cloud integration."""
|
|
|
|
import asyncio
|
2017-09-12 16:47:04 +00:00
|
|
|
from functools import wraps
|
2017-08-29 20:40:08 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import async_timeout
|
2018-01-21 06:35:38 +00:00
|
|
|
import voluptuous as vol
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2018-09-20 12:53:13 +00:00
|
|
|
from homeassistant.core import callback
|
2018-02-14 20:06:03 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
|
|
|
from homeassistant.components.http.data_validator import (
|
|
|
|
RequestDataValidator)
|
2018-09-20 12:53:13 +00:00
|
|
|
from homeassistant.components import websocket_api
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
from . import auth_api
|
2017-10-15 02:43:14 +00:00
|
|
|
from .const import DOMAIN, REQUEST_TIMEOUT
|
2018-09-20 12:53:13 +00:00
|
|
|
from .iot import STATE_DISCONNECTED
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-09-20 12:53:13 +00:00
|
|
|
WS_TYPE_STATUS = 'cloud/status'
|
|
|
|
SCHEMA_WS_STATUS = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
|
|
|
vol.Required('type'): WS_TYPE_STATUS,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
WS_TYPE_SUBSCRIPTION = 'cloud/subscription'
|
|
|
|
SCHEMA_WS_SUBSCRIPTION = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
|
|
|
vol.Required('type'): WS_TYPE_SUBSCRIPTION,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-03-23 19:13:52 +00:00
|
|
|
async def async_setup(hass):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the HTTP API."""
|
2018-09-20 12:53:13 +00:00
|
|
|
hass.components.websocket_api.async_register_command(
|
|
|
|
WS_TYPE_STATUS, websocket_cloud_status,
|
|
|
|
SCHEMA_WS_STATUS
|
|
|
|
)
|
|
|
|
hass.components.websocket_api.async_register_command(
|
|
|
|
WS_TYPE_SUBSCRIPTION, websocket_subscription,
|
|
|
|
SCHEMA_WS_SUBSCRIPTION
|
|
|
|
)
|
2018-03-23 19:13:52 +00:00
|
|
|
hass.http.register_view(GoogleActionsSyncView)
|
2017-08-29 20:40:08 +00:00
|
|
|
hass.http.register_view(CloudLoginView)
|
|
|
|
hass.http.register_view(CloudLogoutView)
|
2017-09-12 16:47:04 +00:00
|
|
|
hass.http.register_view(CloudRegisterView)
|
2017-12-29 13:46:10 +00:00
|
|
|
hass.http.register_view(CloudResendConfirmView)
|
2017-09-12 16:47:04 +00:00
|
|
|
hass.http.register_view(CloudForgotPasswordView)
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
_CLOUD_ERRORS = {
|
|
|
|
auth_api.UserNotFound: (400, "User does not exist."),
|
|
|
|
auth_api.UserNotConfirmed: (400, 'Email not confirmed.'),
|
|
|
|
auth_api.Unauthenticated: (401, 'Authentication failed.'),
|
|
|
|
auth_api.PasswordChangeRequired: (400, 'Password change required.'),
|
|
|
|
asyncio.TimeoutError: (502, 'Unable to reach the Home Assistant cloud.')
|
|
|
|
}
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
def _handle_cloud_errors(handler):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Handle auth errors."""
|
2017-09-12 16:47:04 +00:00
|
|
|
@wraps(handler)
|
2018-03-23 19:13:52 +00:00
|
|
|
async def error_handler(view, request, *args, **kwargs):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Handle exceptions that raise from the wrapped request handler."""
|
2017-08-29 20:40:08 +00:00
|
|
|
try:
|
2018-03-23 19:13:52 +00:00
|
|
|
result = await handler(view, request, *args, **kwargs)
|
2017-09-12 16:47:04 +00:00
|
|
|
return result
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
except (auth_api.CloudError, asyncio.TimeoutError) as err:
|
|
|
|
err_info = _CLOUD_ERRORS.get(err.__class__)
|
|
|
|
if err_info is None:
|
|
|
|
err_info = (502, 'Unexpected error: {}'.format(err))
|
|
|
|
status, msg = err_info
|
|
|
|
return view.json_message(msg, status_code=status,
|
|
|
|
message_code=err.__class__.__name__)
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
return error_handler
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
2018-03-23 19:13:52 +00:00
|
|
|
class GoogleActionsSyncView(HomeAssistantView):
|
|
|
|
"""Trigger a Google Actions Smart Home Sync."""
|
|
|
|
|
|
|
|
url = '/api/cloud/google_actions/sync'
|
|
|
|
name = 'api:cloud:google_actions/sync'
|
|
|
|
|
|
|
|
@_handle_cloud_errors
|
|
|
|
async def post(self, request):
|
|
|
|
"""Trigger a Google Actions sync."""
|
|
|
|
hass = request.app['hass']
|
|
|
|
cloud = hass.data[DOMAIN]
|
|
|
|
websession = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
|
|
|
await hass.async_add_job(auth_api.check_token, cloud)
|
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
|
|
|
req = await websession.post(
|
|
|
|
cloud.google_actions_sync_url, headers={
|
|
|
|
'authorization': cloud.id_token
|
|
|
|
})
|
|
|
|
|
|
|
|
return self.json({}, status_code=req.status)
|
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
class CloudLoginView(HomeAssistantView):
|
|
|
|
"""Login to Home Assistant cloud."""
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
url = '/api/cloud/login'
|
|
|
|
name = 'api:cloud:login'
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
@_handle_cloud_errors
|
|
|
|
@RequestDataValidator(vol.Schema({
|
|
|
|
vol.Required('email'): str,
|
|
|
|
vol.Required('password'): str,
|
|
|
|
}))
|
2018-03-23 19:13:52 +00:00
|
|
|
async def post(self, request, data):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Handle login request."""
|
|
|
|
hass = request.app['hass']
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data[DOMAIN]
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
2018-03-23 19:13:52 +00:00
|
|
|
await hass.async_add_job(auth_api.login, cloud, data['email'],
|
|
|
|
data['password'])
|
2017-09-12 16:47:04 +00:00
|
|
|
|
2017-11-15 07:16:19 +00:00
|
|
|
hass.async_add_job(cloud.iot.connect)
|
2018-09-20 12:53:13 +00:00
|
|
|
return self.json({'success': True})
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CloudLogoutView(HomeAssistantView):
|
|
|
|
"""Log out of the Home Assistant cloud."""
|
|
|
|
|
|
|
|
url = '/api/cloud/logout'
|
|
|
|
name = 'api:cloud:logout'
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
@_handle_cloud_errors
|
2018-03-23 19:13:52 +00:00
|
|
|
async def post(self, request):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Handle logout request."""
|
2017-08-29 20:40:08 +00:00
|
|
|
hass = request.app['hass']
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data[DOMAIN]
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
2018-03-23 19:13:52 +00:00
|
|
|
await cloud.logout()
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
return self.json_message('ok')
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
class CloudRegisterView(HomeAssistantView):
|
|
|
|
"""Register on the Home Assistant cloud."""
|
|
|
|
|
|
|
|
url = '/api/cloud/register'
|
|
|
|
name = 'api:cloud:register'
|
|
|
|
|
|
|
|
@_handle_cloud_errors
|
|
|
|
@RequestDataValidator(vol.Schema({
|
|
|
|
vol.Required('email'): str,
|
|
|
|
vol.Required('password'): vol.All(str, vol.Length(min=6)),
|
|
|
|
}))
|
2018-03-23 19:13:52 +00:00
|
|
|
async def post(self, request, data):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Handle registration request."""
|
|
|
|
hass = request.app['hass']
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data[DOMAIN]
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
2018-03-23 19:13:52 +00:00
|
|
|
await hass.async_add_job(
|
2017-10-15 02:43:14 +00:00
|
|
|
auth_api.register, cloud, data['email'], data['password'])
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
return self.json_message('ok')
|
|
|
|
|
|
|
|
|
2017-12-29 13:46:10 +00:00
|
|
|
class CloudResendConfirmView(HomeAssistantView):
|
|
|
|
"""Resend email confirmation code."""
|
|
|
|
|
|
|
|
url = '/api/cloud/resend_confirm'
|
|
|
|
name = 'api:cloud:resend_confirm'
|
|
|
|
|
|
|
|
@_handle_cloud_errors
|
|
|
|
@RequestDataValidator(vol.Schema({
|
|
|
|
vol.Required('email'): str,
|
|
|
|
}))
|
2018-03-23 19:13:52 +00:00
|
|
|
async def post(self, request, data):
|
2017-12-29 13:46:10 +00:00
|
|
|
"""Handle resending confirm email code request."""
|
|
|
|
hass = request.app['hass']
|
|
|
|
cloud = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
2018-03-23 19:13:52 +00:00
|
|
|
await hass.async_add_job(
|
2017-12-29 13:46:10 +00:00
|
|
|
auth_api.resend_email_confirm, cloud, data['email'])
|
|
|
|
|
|
|
|
return self.json_message('ok')
|
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
class CloudForgotPasswordView(HomeAssistantView):
|
|
|
|
"""View to start Forgot Password flow.."""
|
|
|
|
|
|
|
|
url = '/api/cloud/forgot_password'
|
|
|
|
name = 'api:cloud:forgot_password'
|
|
|
|
|
|
|
|
@_handle_cloud_errors
|
|
|
|
@RequestDataValidator(vol.Schema({
|
|
|
|
vol.Required('email'): str,
|
|
|
|
}))
|
2018-03-23 19:13:52 +00:00
|
|
|
async def post(self, request, data):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Handle forgot password request."""
|
|
|
|
hass = request.app['hass']
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data[DOMAIN]
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
2018-03-23 19:13:52 +00:00
|
|
|
await hass.async_add_job(
|
2017-10-15 02:43:14 +00:00
|
|
|
auth_api.forgot_password, cloud, data['email'])
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
return self.json_message('ok')
|
|
|
|
|
|
|
|
|
2018-09-20 12:53:13 +00:00
|
|
|
@callback
|
|
|
|
def websocket_cloud_status(hass, connection, msg):
|
|
|
|
"""Handle request for account info.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
cloud = hass.data[DOMAIN]
|
|
|
|
connection.to_write.put_nowait(
|
|
|
|
websocket_api.result_message(msg['id'], _account_data(cloud)))
|
|
|
|
|
|
|
|
|
|
|
|
@websocket_api.async_response
|
|
|
|
async def websocket_subscription(hass, connection, msg):
|
|
|
|
"""Handle request for account info."""
|
|
|
|
cloud = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
if not cloud.is_logged_in:
|
|
|
|
connection.to_write.put_nowait(websocket_api.error_message(
|
|
|
|
msg['id'], 'not_logged_in',
|
|
|
|
'You need to be logged in to the cloud.'))
|
|
|
|
return
|
|
|
|
|
|
|
|
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
|
|
|
|
response = await cloud.fetch_subscription_info()
|
|
|
|
|
|
|
|
if response.status == 200:
|
|
|
|
connection.send_message_outside(websocket_api.result_message(
|
|
|
|
msg['id'], await response.json()))
|
|
|
|
else:
|
|
|
|
connection.send_message_outside(websocket_api.error_message(
|
|
|
|
msg['id'], 'request_failed', 'Failed to request subscription'))
|
|
|
|
|
|
|
|
|
2017-10-15 02:43:14 +00:00
|
|
|
def _account_data(cloud):
|
2017-09-12 16:47:04 +00:00
|
|
|
"""Generate the auth data JSON response."""
|
2018-09-20 12:53:13 +00:00
|
|
|
if not cloud.is_logged_in:
|
|
|
|
return {
|
|
|
|
'logged_in': False,
|
|
|
|
'cloud': STATE_DISCONNECTED,
|
|
|
|
}
|
|
|
|
|
2017-11-15 07:16:19 +00:00
|
|
|
claims = cloud.claims
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
return {
|
2018-09-20 12:53:13 +00:00
|
|
|
'logged_in': True,
|
2017-11-15 07:16:19 +00:00
|
|
|
'email': claims['email'],
|
|
|
|
'cloud': cloud.iot.state,
|
2017-09-12 16:47:04 +00:00
|
|
|
}
|