Cloud connection via aiohttp (#9860)

* Cloud: connect to cloud

* Fix tests in py34

* Update warrant to 0.5.0

* Differentiate errors between unknown handler vs exception

* Lint

* Respond to cloud message to logout

* Refresh token exception handling

* Swap out bare exception for RuntimeError

* Add more tests

* Fix tests py34
This commit is contained in:
Paulus Schoutsen 2017-10-14 19:43:14 -07:00 committed by GitHub
parent 26cb67dec2
commit 0362a76cd6
12 changed files with 930 additions and 429 deletions

View file

@ -10,7 +10,7 @@ from homeassistant.components.http import (
HomeAssistantView, RequestDataValidator)
from . import auth_api
from .const import REQUEST_TIMEOUT
from .const import DOMAIN, REQUEST_TIMEOUT
_LOGGER = logging.getLogger(__name__)
@ -74,13 +74,14 @@ class CloudLoginView(HomeAssistantView):
def post(self, request, data):
"""Handle login request."""
hass = request.app['hass']
auth = hass.data['cloud']['auth']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(auth.login, data['email'],
yield from hass.async_add_job(auth_api.login, cloud, data['email'],
data['password'])
hass.async_add_job(cloud.iot.connect)
return self.json(_auth_data(auth))
return self.json(_account_data(cloud))
class CloudLogoutView(HomeAssistantView):
@ -94,10 +95,10 @@ class CloudLogoutView(HomeAssistantView):
def post(self, request):
"""Handle logout request."""
hass = request.app['hass']
auth = hass.data['cloud']['auth']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(auth.logout)
yield from cloud.logout()
return self.json_message('ok')
@ -112,12 +113,12 @@ class CloudAccountView(HomeAssistantView):
def get(self, request):
"""Get account info."""
hass = request.app['hass']
auth = hass.data['cloud']['auth']
cloud = hass.data[DOMAIN]
if not auth.is_logged_in:
if not cloud.is_logged_in:
return self.json_message('Not logged in', 400)
return self.json(_auth_data(auth))
return self.json(_account_data(cloud))
class CloudRegisterView(HomeAssistantView):
@ -135,10 +136,11 @@ class CloudRegisterView(HomeAssistantView):
def post(self, request, data):
"""Handle registration request."""
hass = request.app['hass']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(
auth_api.register, hass, data['email'], data['password'])
auth_api.register, cloud, data['email'], data['password'])
return self.json_message('ok')
@ -158,10 +160,11 @@ class CloudConfirmRegisterView(HomeAssistantView):
def post(self, request, data):
"""Handle registration confirmation request."""
hass = request.app['hass']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(
auth_api.confirm_register, hass, data['confirmation_code'],
auth_api.confirm_register, cloud, data['confirmation_code'],
data['email'])
return self.json_message('ok')
@ -181,10 +184,11 @@ class CloudForgotPasswordView(HomeAssistantView):
def post(self, request, data):
"""Handle forgot password request."""
hass = request.app['hass']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(
auth_api.forgot_password, hass, data['email'])
auth_api.forgot_password, cloud, data['email'])
return self.json_message('ok')
@ -205,18 +209,19 @@ class CloudConfirmForgotPasswordView(HomeAssistantView):
def post(self, request, data):
"""Handle forgot password confirm request."""
hass = request.app['hass']
cloud = hass.data[DOMAIN]
with async_timeout.timeout(REQUEST_TIMEOUT, loop=hass.loop):
yield from hass.async_add_job(
auth_api.confirm_forgot_password, hass,
auth_api.confirm_forgot_password, cloud,
data['confirmation_code'], data['email'],
data['new_password'])
return self.json_message('ok')
def _auth_data(auth):
def _account_data(cloud):
"""Generate the auth data JSON response."""
return {
'email': auth.account.email
'email': cloud.email
}