Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed May 21, 2016
1 parent 8fe2654 commit 9aa9e57
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 58 deletions.
17 changes: 2 additions & 15 deletions homeassistant/components/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
HTTP_BAD_REQUEST, HTTP_CREATED, HTTP_NOT_FOUND,
HTTP_UNPROCESSABLE_ENTITY, MATCH_ALL, URL_API, URL_API_COMPONENTS,
URL_API_CONFIG, URL_API_DISCOVERY_INFO, URL_API_ERROR_LOG,
URL_API_EVENT_FORWARD, URL_API_EVENTS, URL_API_LOG_OUT, URL_API_SERVICES,
URL_API_EVENT_FORWARD, URL_API_EVENTS, URL_API_SERVICES,
URL_API_STATES, URL_API_STATES_ENTITY, URL_API_STREAM, URL_API_TEMPLATE,
__version__)
from homeassistant.exceptions import TemplateError
Expand Down Expand Up @@ -48,7 +48,6 @@ def setup(hass, config):
hass.wsgi.register_view(APIEventForwardingView)
hass.wsgi.register_view(APIComponentsView)
hass.wsgi.register_view(APIErrorLogView)
hass.wsgi.register_view(APILogOutView)
hass.wsgi.register_view(APITemplateView)

return True
Expand Down Expand Up @@ -129,7 +128,7 @@ def stream():
self.hass.bus.listen(MATCH_ALL, thread_forward_events)

attached_ping = track_utc_time_change(
self.hass, thread_ping, second=range(0, 60, 3)) #(0, 30))
self.hass, thread_ping, second=(0, 30))

_LOGGER.debug('STREAM %s ATTACHED', id(stop_obj))

Expand Down Expand Up @@ -401,18 +400,6 @@ def get(self, request):
return self.file(request, self.hass.config.path(ERROR_LOG_FILENAME))


class APILogOutView(HomeAssistantView):
"""View to handle Log Out requests."""

url = URL_API_LOG_OUT
name = "api:log-out"

def post(self, request):
"""Handle log out."""
# TODO kill session
return {}


class APITemplateView(HomeAssistantView):
"""View to handle requests."""

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def get(self, request, entity_id=None):

template = self.templates.get_template('index.html')

# pylint is wrong
# pylint: disable=no-member
resp = template.render(app_url=app_url, auth=auth,
icons=mdi_version.VERSION)

Expand Down
40 changes: 13 additions & 27 deletions homeassistant/components/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def setup(hass, config):
def request_class():
"""Generate request class.
Done in method because of imports."""
Done in method because of imports.
"""
from werkzeug.exceptions import BadRequest
from werkzeug.wrappers import BaseRequest, AcceptMixin
from werkzeug.utils import cached_property
Expand Down Expand Up @@ -100,6 +101,7 @@ def routing_map(hass):

class EntityValidator(BaseConverter):
"""Validate entity_id in urls."""

regex = r"(\w+)\.(\w+)"

def __init__(self, url_map, exist=True, domain=None):
Expand Down Expand Up @@ -259,17 +261,16 @@ def dispatch_request(self, request):
return self.views[endpoint].handle_request(request, **values)
except RequestRedirect as ex:
return ex
except BadRequest as ex:
return self._handle_error(request, str(ex), 400)
except NotFound as ex:
return self._handle_error(request, str(ex), 404)
except MethodNotAllowed as ex:
return self._handle_error(request, str(ex), 405)
except Unauthorized as ex:
return self._handle_error(request, str(ex), 401)
# TODO This long chain of except blocks is silly. _handle_error should
# just take the exception as an argument and parse the status code
# itself
except (BadRequest, NotFound, MethodNotAllowed,
Unauthorized) as ex:
resp = ex.get_response(request.environ)
if request.accept_mimetypes.accept_json:
resp.data = json.dumps({
"result": "error",
"message": str(ex),
})
resp.mimetype = "application/json"
return resp

def base_app(self, environ, start_response):
"""WSGI Handler of requests to base app."""
Expand All @@ -288,19 +289,6 @@ def __call__(self, environ, start_response):
environ['PATH_INFO'] = "{}.{}".format(*fingerprinted.groups())
return app(environ, start_response)

def _handle_error(self, request, message, status):
"""Handle a WSGI request error."""
from werkzeug.wrappers import Response
if request.accept_mimetypes.accept_json:
message = json.dumps({
"result": "error",
"message": message,
})
mimetype = "application/json"
else:
mimetype = "text/plain"
return Response(message, status=status, mimetype=mimetype)


class HomeAssistantView(object):
"""Base view for all views."""
Expand Down Expand Up @@ -339,8 +327,6 @@ def handle_request(self, request, **values):
except AttributeError:
raise MethodNotAllowed

# TODO: session support + uncomment session test

# Auth code verbose on purpose
authenticated = False

Expand Down
8 changes: 4 additions & 4 deletions homeassistant/util/eventlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


def spawn(hub, func, *args, **kwargs):
"""Spawns a function on specified hub."""
"""Spawn a function on specified hub."""
import eventlet
g = eventlet.greenthread.GreenThread(hub.greenlet)
hub.schedule_call_global(0, g.switch, func, args, kwargs)
return g
gthread = eventlet.greenthread.GreenThread(hub.greenlet)
hub.schedule_call_global(0, gthread.switch, func, args, kwargs)
return gthread
3 changes: 0 additions & 3 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ python-nmap==0.6.0
# homeassistant.components.notify.pushover
python-pushover==0.2

# homeassistant.components.socket_io
python-socketio==1.3

# homeassistant.components.statsd
python-statsd==1.7.2

Expand Down
9 changes: 0 additions & 9 deletions tests/components/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,6 @@ def test_access_with_password_in_url(self):

self.assertEqual(200, req.status_code)

# def test_access_via_session(self):
# """Test access wia session."""
# session = requests.Session()
# req = session.get(_url(const.URL_API), headers=HA_HEADERS)
# self.assertEqual(200, req.status_code)

# req = session.get(_url(const.URL_API))
# self.assertEqual(200, req.status_code)

def test_api_list_state_entities(self):
"""Test if the debug interface allows us to list state entities."""
req = requests.get(_url(const.URL_API_STATES),
Expand Down

0 comments on commit 9aa9e57

Please sign in to comment.