Switch from json messages to plain text messages

This commit is contained in:
Philip Lundrigan 2015-12-30 22:43:00 -07:00
parent c23375a18b
commit 7d41ce4e46
2 changed files with 30 additions and 18 deletions

View File

@ -46,8 +46,8 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
try: try:
gps_coords = (float(data['latitude']), float(data['longitude'])) gps_coords = (float(data['latitude']), float(data['longitude']))
except ValueError: except ValueError:
handler.write_json_message("Invalid latitude / longitude format.", handler.write_text("Invalid latitude / longitude format.",
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Received invalid latitude / longitude format.") _LOGGER.error("Received invalid latitude / longitude format.")
return return
@ -57,11 +57,11 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
if "zone.{}".format(location_name.lower()) in zones: if "zone.{}".format(location_name.lower()) in zones:
see(dev_id=device, location_name=location_name) see(dev_id=device, location_name=location_name)
handler.write_json_message( handler.write_text(
"Set new location to {}".format(location_name)) "Set new location to {}".format(location_name))
else: else:
see(dev_id=device, gps=gps_coords) see(dev_id=device, gps=gps_coords)
handler.write_json_message( handler.write_text(
"Set new location to {}".format(gps_coords)) "Set new location to {}".format(gps_coords))
elif direction == 'exit': elif direction == 'exit':
@ -70,13 +70,13 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
if current_zone.lower() == location_name.lower(): if current_zone.lower() == location_name.lower():
see(dev_id=device, location_name=STATE_NOT_HOME) see(dev_id=device, location_name=STATE_NOT_HOME)
handler.write_json_message("Set new location to not home") handler.write_text("Set new location to not home")
else: else:
# 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 before # aren't currently in. This occurs when a zone is entered before
# the previous zone was exited. The enter message will be sent # the previous zone was exited. The enter message will be sent
# first, then the exit message will be sent second. # first, then the exit message will be sent second.
handler.write_json_message( handler.write_text(
"Ignoring transition from {}".format(location_name)) "Ignoring transition from {}".format(location_name))
elif direction == 'test': elif direction == 'test':
@ -85,7 +85,7 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
handler.write_text("Received test message.") handler.write_text("Received test message.")
else: else:
handler.write_json_message( handler.write_text(
"Received unidentified message: {}".format(direction)) "Received unidentified message: {}".format(direction))
_LOGGER.error("Received unidentified message from Locative: %s", _LOGGER.error("Received unidentified message from Locative: %s",
direction) direction)
@ -93,33 +93,33 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
def _check_data(handler, data): def _check_data(handler, data):
if not isinstance(data, dict): if not isinstance(data, dict):
handler.write_json_message("Error while parsing Locative message.", handler.write_text("Error while parsing Locative message.",
HTTP_INTERNAL_SERVER_ERROR) HTTP_INTERNAL_SERVER_ERROR)
_LOGGER.error("Error while parsing Locative message: " _LOGGER.error("Error while parsing Locative message: "
"data is not a dict.") "data is not a dict.")
return False return False
if 'latitude' not in data or 'longitude' not in data: if 'latitude' not in data or 'longitude' not in data:
handler.write_json_message("Latitude and longitude not specified.", handler.write_text("Latitude and longitude not specified.",
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Latitude and longitude not specified.") _LOGGER.error("Latitude and longitude not specified.")
return False return False
if 'device' not in data: if 'device' not in data:
handler.write_json_message("Device id not specified.", handler.write_text("Device id not specified.",
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Device id not specified.") _LOGGER.error("Device id not specified.")
return False return False
if 'id' not in data: if 'id' not in data:
handler.write_json_message("Location id not specified.", handler.write_text("Location id not specified.",
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Location id not specified.") _LOGGER.error("Location id not specified.")
return False return False
if 'trigger' not in data: if 'trigger' not in data:
handler.write_json_message("Trigger is not specified.", handler.write_text("Trigger is not specified.",
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Trigger is not specified.") _LOGGER.error("Trigger is not specified.")
return False return False

View File

@ -21,7 +21,7 @@ from urllib.parse import urlparse, parse_qs
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.const import ( from homeassistant.const import (
SERVER_PORT, CONTENT_TYPE_JSON, SERVER_PORT, CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT_PLAIN,
HTTP_HEADER_HA_AUTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_ACCEPT_ENCODING, HTTP_HEADER_HA_AUTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_ACCEPT_ENCODING,
HTTP_HEADER_CONTENT_ENCODING, HTTP_HEADER_VARY, HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_ENCODING, HTTP_HEADER_VARY, HTTP_HEADER_CONTENT_LENGTH,
HTTP_HEADER_CACHE_CONTROL, HTTP_HEADER_EXPIRES, HTTP_OK, HTTP_UNAUTHORIZED, HTTP_HEADER_CACHE_CONTROL, HTTP_HEADER_EXPIRES, HTTP_OK, HTTP_UNAUTHORIZED,
@ -293,6 +293,18 @@ class RequestHandler(SimpleHTTPRequestHandler):
json.dumps(data, indent=4, sort_keys=True, json.dumps(data, indent=4, sort_keys=True,
cls=rem.JSONEncoder).encode("UTF-8")) cls=rem.JSONEncoder).encode("UTF-8"))
def write_text(self, message, status_code=HTTP_OK):
""" Helper method to return a text message to the caller. """
self.send_response(status_code)
self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_TEXT_PLAIN)
self.set_session_cookie_header()
self.end_headers()
if message is not None:
self.wfile.write(message.encode("UTF-8"))
def write_file(self, path, cache_headers=True): def write_file(self, path, cache_headers=True):
""" Returns a file to the user. """ """ Returns a file to the user. """
try: try: