Return web.Response correctly

This commit is contained in:
Rohan Kapoor 2019-01-13 17:07:45 -08:00
parent e476949c3e
commit 452d7cfd61

View file

@ -8,12 +8,13 @@ import logging
from typing import Dict
import voluptuous as vol
from aiohttp import web
import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, ATTR_LATITUDE, \
ATTR_LONGITUDE, STATE_NOT_HOME, CONF_WEBHOOK_ID, ATTR_ID
ATTR_LONGITUDE, STATE_NOT_HOME, CONF_WEBHOOK_ID, ATTR_ID, HTTP_OK
from homeassistant.helpers import config_entry_flow
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -68,7 +69,10 @@ async def handle_webhook(hass, webhook_id, request):
try:
data = WEBHOOK_SCHEMA(dict(await request.post()))
except vol.MultipleInvalid as error:
return error.error_message, HTTP_UNPROCESSABLE_ENTITY
return web.Response(
body=error.error_message,
status=HTTP_UNPROCESSABLE_ENTITY
)
device = data[ATTR_DEVICE_ID]
location_name = data.get(ATTR_ID, data[ATTR_TRIGGER]).lower()
@ -83,7 +87,10 @@ async def handle_webhook(hass, webhook_id, request):
gps_location,
location_name
)
return 'Setting location to {}'.format(location_name)
return web.Response(
body='Setting location to {}'.format(location_name),
status=HTTP_OK
)
if direction == 'exit':
current_state = hass.states.get(
@ -98,24 +105,36 @@ async def handle_webhook(hass, webhook_id, request):
gps_location,
location_name
)
return 'Setting location to not home'
return web.Response(
body='Setting location to not home',
status=HTTP_OK
)
# Ignore the message if it is telling us to exit a zone that we
# aren't currently in. This occurs when a zone is entered
# before the previous zone was exited. The enter message will
# be sent first, then the exit message will be sent second.
return 'Ignoring exit from {} (already in {})'.format(
location_name, current_state)
return web.Response(
body='Ignoring exit from {} (already in {})'.format(
location_name, current_state
),
status=HTTP_OK
)
if direction == 'test':
# In the app, a test message can be sent. Just return something to
# the user to let them know that it works.
return 'Received test message.'
return web.Response(
body='Received test message.',
status=HTTP_OK
)
_LOGGER.error('Received unidentified message from Locative: %s',
direction)
return ('Received unidentified message: {}'.format(direction),
HTTP_UNPROCESSABLE_ENTITY)
return web.Response(
body='Received unidentified message: {}'.format(direction),
status=HTTP_UNPROCESSABLE_ENTITY
)
async def async_setup_entry(hass, entry):