async HTTP component (#3914)
* Migrate WSGI to asyncio * Rename wsgi -> http * Python 3.4 compat * Move linting to Python 3.4 * lint * Lint * Fix Python 3.4 mock_open + binary data * Surpress logging aiohttp.access * Spelling * Sending files is a coroutine * More callback annotations and naming fixes * Fix ios
This commit is contained in:
1 parent
9aa88819a5
commit
519d9f2fd0
45 files changed
+1422
-1009
No files matched your search
@@ -4,6 +4,7 @@ HTML5 Push Messaging notification service.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/notify.html5/
|
||||
"""
|
||||
import asyncio
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
@@ -107,9 +108,9 @@ def get_service(hass, config):
|
||||
if registrations is None:
|
||||
return None
|
||||
|
||||
hass.wsgi.register_view(
|
||||
hass.http.register_view(
|
||||
HTML5PushRegistrationView(hass, registrations, json_path))
|
||||
hass.wsgi.register_view(HTML5PushCallbackView(hass, registrations))
|
||||
hass.http.register_view(HTML5PushCallbackView(hass, registrations))
|
||||
|
||||
gcm_api_key = config.get(ATTR_GCM_API_KEY)
|
||||
gcm_sender_id = config.get(ATTR_GCM_SENDER_ID)
|
||||
@@ -163,12 +164,18 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
||||
self.registrations = registrations
|
||||
self.json_path = json_path
|
||||
|
||||
@asyncio.coroutine
|
||||
def post(self, request):
|
||||
"""Accept the POST request for push registrations from a browser."""
|
||||
try:
|
||||
data = REGISTER_SCHEMA(request.json)
|
||||
data = yield from request.json()
|
||||
except ValueError:
|
||||
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
data = REGISTER_SCHEMA(data)
|
||||
except vol.Invalid as ex:
|
||||
return self.json_message(humanize_error(request.json, ex),
|
||||
return self.json_message(humanize_error(data, ex),
|
||||
HTTP_BAD_REQUEST)
|
||||
|
||||
name = ensure_unique_string('unnamed device',
|
||||
@@ -182,9 +189,15 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
||||
|
||||
return self.json_message('Push notification subscriber registered.')
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete(self, request):
|
||||
"""Delete a registration."""
|
||||
subscription = request.json.get(ATTR_SUBSCRIPTION)
|
||||
try:
|
||||
data = yield from request.json()
|
||||
except ValueError:
|
||||
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)
|
||||
|
||||
subscription = data.get(ATTR_SUBSCRIPTION)
|
||||
|
||||
found = None
|
||||
|
||||
@@ -270,23 +283,29 @@ class HTML5PushCallbackView(HomeAssistantView):
|
||||
status_code=HTTP_UNAUTHORIZED)
|
||||
return payload
|
||||
|
||||
@asyncio.coroutine
|
||||
def post(self, request):
|
||||
"""Accept the POST request for push registrations event callback."""
|
||||
auth_check = self.check_authorization_header(request)
|
||||
if not isinstance(auth_check, dict):
|
||||
return auth_check
|
||||
|
||||
try:
|
||||
data = yield from request.json()
|
||||
except ValueError:
|
||||
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)
|
||||
|
||||
event_payload = {
|
||||
ATTR_TAG: request.json.get(ATTR_TAG),
|
||||
ATTR_TYPE: request.json[ATTR_TYPE],
|
||||
ATTR_TAG: data.get(ATTR_TAG),
|
||||
ATTR_TYPE: data[ATTR_TYPE],
|
||||
ATTR_TARGET: auth_check[ATTR_TARGET],
|
||||
}
|
||||
|
||||
if request.json.get(ATTR_ACTION) is not None:
|
||||
event_payload[ATTR_ACTION] = request.json.get(ATTR_ACTION)
|
||||
if data.get(ATTR_ACTION) is not None:
|
||||
event_payload[ATTR_ACTION] = data.get(ATTR_ACTION)
|
||||
|
||||
if request.json.get(ATTR_DATA) is not None:
|
||||
event_payload[ATTR_DATA] = request.json.get(ATTR_DATA)
|
||||
if data.get(ATTR_DATA) is not None:
|
||||
event_payload[ATTR_DATA] = data.get(ATTR_DATA)
|
||||
|
||||
try:
|
||||
event_payload = CALLBACK_EVENT_PAYLOAD_SCHEMA(event_payload)
|
||||
|
||||
Reference in new issue
Block a user