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