Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -5,16 +5,25 @@ from typing import Dict
|
||||
from aiohttp.web import Response, Request
|
||||
|
||||
from homeassistant.auth.util import generate_secret
|
||||
from homeassistant.components.cloud import (async_create_cloudhook,
|
||||
async_remote_ui_url,
|
||||
CloudNotAvailable)
|
||||
from homeassistant.components.cloud import (
|
||||
async_create_cloudhook,
|
||||
async_remote_ui_url,
|
||||
CloudNotAvailable,
|
||||
)
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.http.data_validator import RequestDataValidator
|
||||
from homeassistant.const import (HTTP_CREATED, CONF_WEBHOOK_ID)
|
||||
from homeassistant.const import HTTP_CREATED, CONF_WEBHOOK_ID
|
||||
|
||||
from .const import (ATTR_DEVICE_ID, ATTR_SUPPORTS_ENCRYPTION,
|
||||
CONF_CLOUDHOOK_URL, CONF_REMOTE_UI_URL, CONF_SECRET,
|
||||
CONF_USER_ID, DOMAIN, REGISTRATION_SCHEMA)
|
||||
from .const import (
|
||||
ATTR_DEVICE_ID,
|
||||
ATTR_SUPPORTS_ENCRYPTION,
|
||||
CONF_CLOUDHOOK_URL,
|
||||
CONF_REMOTE_UI_URL,
|
||||
CONF_SECRET,
|
||||
CONF_USER_ID,
|
||||
DOMAIN,
|
||||
REGISTRATION_SCHEMA,
|
||||
)
|
||||
|
||||
from .helpers import supports_encryption
|
||||
|
||||
@@ -22,19 +31,18 @@ from .helpers import supports_encryption
|
||||
class RegistrationsView(HomeAssistantView):
|
||||
"""A view that accepts registration requests."""
|
||||
|
||||
url = '/api/mobile_app/registrations'
|
||||
name = 'api:mobile_app:register'
|
||||
url = "/api/mobile_app/registrations"
|
||||
name = "api:mobile_app:register"
|
||||
|
||||
@RequestDataValidator(REGISTRATION_SCHEMA)
|
||||
async def post(self, request: Request, data: Dict) -> Response:
|
||||
"""Handle the POST request for registration."""
|
||||
hass = request.app['hass']
|
||||
hass = request.app["hass"]
|
||||
|
||||
webhook_id = generate_secret()
|
||||
|
||||
if hass.components.cloud.async_active_subscription():
|
||||
data[CONF_CLOUDHOOK_URL] = \
|
||||
await async_create_cloudhook(hass, webhook_id)
|
||||
data[CONF_CLOUDHOOK_URL] = await async_create_cloudhook(hass, webhook_id)
|
||||
|
||||
data[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "")
|
||||
|
||||
@@ -45,12 +53,12 @@ class RegistrationsView(HomeAssistantView):
|
||||
|
||||
data[CONF_SECRET] = generate_secret(SecretBox.KEY_SIZE)
|
||||
|
||||
data[CONF_USER_ID] = request['hass_user'].id
|
||||
data[CONF_USER_ID] = request["hass_user"].id
|
||||
|
||||
ctx = {'source': 'registration'}
|
||||
ctx = {"source": "registration"}
|
||||
await hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(DOMAIN, context=ctx,
|
||||
data=data))
|
||||
hass.config_entries.flow.async_init(DOMAIN, context=ctx, data=data)
|
||||
)
|
||||
|
||||
remote_ui_url = None
|
||||
try:
|
||||
@@ -58,9 +66,12 @@ class RegistrationsView(HomeAssistantView):
|
||||
except CloudNotAvailable:
|
||||
pass
|
||||
|
||||
return self.json({
|
||||
CONF_CLOUDHOOK_URL: data.get(CONF_CLOUDHOOK_URL),
|
||||
CONF_REMOTE_UI_URL: remote_ui_url,
|
||||
CONF_SECRET: data.get(CONF_SECRET),
|
||||
CONF_WEBHOOK_ID: data[CONF_WEBHOOK_ID],
|
||||
}, status_code=HTTP_CREATED)
|
||||
return self.json(
|
||||
{
|
||||
CONF_CLOUDHOOK_URL: data.get(CONF_CLOUDHOOK_URL),
|
||||
CONF_REMOTE_UI_URL: remote_ui_url,
|
||||
CONF_SECRET: data.get(CONF_SECRET),
|
||||
CONF_WEBHOOK_ID: data[CONF_WEBHOOK_ID],
|
||||
},
|
||||
status_code=HTTP_CREATED,
|
||||
)
|
||||
Reference in new issue
Block a user