From 7d41ce4e46c0f6a77218621619bd287c550a4665 Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Wed, 30 Dec 2015 22:43:00 -0700 Subject: [PATCH] Switch from json messages to plain text messages --- .../components/device_tracker/locative.py | 34 +++++++++---------- homeassistant/components/http.py | 14 +++++++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/device_tracker/locative.py b/homeassistant/components/device_tracker/locative.py index 72e458bc314..c635aa47858 100644 --- a/homeassistant/components/device_tracker/locative.py +++ b/homeassistant/components/device_tracker/locative.py @@ -46,8 +46,8 @@ def _handle_get_api_locative(hass, see, handler, path_match, data): try: gps_coords = (float(data['latitude']), float(data['longitude'])) except ValueError: - handler.write_json_message("Invalid latitude / longitude format.", - HTTP_UNPROCESSABLE_ENTITY) + handler.write_text("Invalid latitude / longitude format.", + HTTP_UNPROCESSABLE_ENTITY) _LOGGER.error("Received invalid latitude / longitude format.") return @@ -57,11 +57,11 @@ def _handle_get_api_locative(hass, see, handler, path_match, data): if "zone.{}".format(location_name.lower()) in zones: see(dev_id=device, location_name=location_name) - handler.write_json_message( + handler.write_text( "Set new location to {}".format(location_name)) else: see(dev_id=device, gps=gps_coords) - handler.write_json_message( + handler.write_text( "Set new location to {}".format(gps_coords)) 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(): 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: # 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. - handler.write_json_message( + handler.write_text( "Ignoring transition from {}".format(location_name)) elif direction == 'test': @@ -85,7 +85,7 @@ def _handle_get_api_locative(hass, see, handler, path_match, data): handler.write_text("Received test message.") else: - handler.write_json_message( + handler.write_text( "Received unidentified message: {}".format(direction)) _LOGGER.error("Received unidentified message from Locative: %s", direction) @@ -93,33 +93,33 @@ def _handle_get_api_locative(hass, see, handler, path_match, data): def _check_data(handler, data): if not isinstance(data, dict): - handler.write_json_message("Error while parsing Locative message.", - HTTP_INTERNAL_SERVER_ERROR) + handler.write_text("Error while parsing Locative message.", + HTTP_INTERNAL_SERVER_ERROR) _LOGGER.error("Error while parsing Locative message: " "data is not a dict.") return False if 'latitude' not in data or 'longitude' not in data: - handler.write_json_message("Latitude and longitude not specified.", - HTTP_UNPROCESSABLE_ENTITY) + handler.write_text("Latitude and longitude not specified.", + HTTP_UNPROCESSABLE_ENTITY) _LOGGER.error("Latitude and longitude not specified.") return False if 'device' not in data: - handler.write_json_message("Device id not specified.", - HTTP_UNPROCESSABLE_ENTITY) + handler.write_text("Device id not specified.", + HTTP_UNPROCESSABLE_ENTITY) _LOGGER.error("Device id not specified.") return False if 'id' not in data: - handler.write_json_message("Location id not specified.", - HTTP_UNPROCESSABLE_ENTITY) + handler.write_text("Location id not specified.", + HTTP_UNPROCESSABLE_ENTITY) _LOGGER.error("Location id not specified.") return False if 'trigger' not in data: - handler.write_json_message("Trigger is not specified.", - HTTP_UNPROCESSABLE_ENTITY) + handler.write_text("Trigger is not specified.", + HTTP_UNPROCESSABLE_ENTITY) _LOGGER.error("Trigger is not specified.") return False diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index 7a4e87de5a8..cd701c24bb6 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -21,7 +21,7 @@ from urllib.parse import urlparse, parse_qs import homeassistant.core as ha 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_CONTENT_ENCODING, HTTP_HEADER_VARY, HTTP_HEADER_CONTENT_LENGTH, 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, 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): """ Returns a file to the user. """ try: