From 1d100dcac969afe3edb1319e8b5554dc988a37eb Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 2 Nov 2016 21:15:23 -0700 Subject: [PATCH] Bugfix/frontend group urls (#4185) * Remove unnecessary sleeps * Frontend: fix serving index when refreshing view page. --- homeassistant/components/frontend/__init__.py | 13 +++++++++--- .../device_tracker/test_locative.py | 2 -- tests/components/media_player/test_demo.py | 2 -- tests/components/test_alexa.py | 2 -- tests/components/test_api.py | 2 -- tests/components/test_emulated_hue.py | 2 -- tests/components/test_frontend.py | 20 +++++++++++-------- tests/components/test_http.py | 2 -- tests/test_remote.py | 2 -- 9 files changed, 22 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index a96e871c42f..195d79ec5da 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -8,8 +8,8 @@ import os from aiohttp import web from homeassistant.core import callback -from homeassistant.const import EVENT_HOMEASSISTANT_START -from homeassistant.components import api +from homeassistant.const import EVENT_HOMEASSISTANT_START, HTTP_NOT_FOUND +from homeassistant.components import api, group from homeassistant.components.http import HomeAssistantView from .version import FINGERPRINTS @@ -184,7 +184,7 @@ class IndexView(HomeAssistantView): url = '/' name = "frontend:index" requires_auth = False - extra_urls = ['/states', '/states/'] + extra_urls = ['/states', '/states/{entity_id}'] def __init__(self, hass, extra_urls): """Initialize the frontend view.""" @@ -202,6 +202,13 @@ class IndexView(HomeAssistantView): @asyncio.coroutine def get(self, request, entity_id=None): """Serve the index view.""" + if entity_id is not None: + state = self.hass.states.get(entity_id) + + if (not state or state.domain != 'group' or + not state.attributes.get(group.ATTR_VIEW)): + return self.json_message('Entity not found', HTTP_NOT_FOUND) + if self.hass.http.development: core_url = '/static/home-assistant-polymer/build/core.js' ui_url = '/static/home-assistant-polymer/src/home-assistant.html' diff --git a/tests/components/device_tracker/test_locative.py b/tests/components/device_tracker/test_locative.py index a724b5d26d5..b1977556c63 100644 --- a/tests/components/device_tracker/test_locative.py +++ b/tests/components/device_tracker/test_locative.py @@ -1,5 +1,4 @@ """The tests the for Locative device tracker platform.""" -import time import unittest from unittest.mock import patch @@ -46,7 +45,6 @@ def setUpModule(): }) hass.start() - time.sleep(0.05) def tearDownModule(): # pylint: disable=invalid-name diff --git a/tests/components/media_player/test_demo.py b/tests/components/media_player/test_demo.py index fc9c64d7fcd..8bcda323010 100644 --- a/tests/components/media_player/test_demo.py +++ b/tests/components/media_player/test_demo.py @@ -9,7 +9,6 @@ import homeassistant.components.media_player as mp import homeassistant.components.http as http import requests -import time from tests.common import get_test_home_assistant, get_test_instance_port @@ -254,7 +253,6 @@ class TestMediaPlayerWeb(unittest.TestCase): }) self.hass.start() - time.sleep(0.05) def tearDown(self): """Stop everything that was started.""" diff --git a/tests/components/test_alexa.py b/tests/components/test_alexa.py index e5ae13d91c6..28a80868163 100644 --- a/tests/components/test_alexa.py +++ b/tests/components/test_alexa.py @@ -1,7 +1,6 @@ """The tests for the Alexa component.""" # pylint: disable=protected-access import json -import time import datetime import unittest @@ -115,7 +114,6 @@ def setUpModule(): }) hass.start() - time.sleep(0.05) # pylint: disable=invalid-name diff --git a/tests/components/test_api.py b/tests/components/test_api.py index f14fc84ae63..a70048956eb 100644 --- a/tests/components/test_api.py +++ b/tests/components/test_api.py @@ -3,7 +3,6 @@ import asyncio from contextlib import closing import json -import time import unittest from unittest.mock import Mock, patch @@ -50,7 +49,6 @@ def setUpModule(): bootstrap.setup_component(hass, 'api') hass.start() - time.sleep(0.05) # pylint: disable=invalid-name diff --git a/tests/components/test_emulated_hue.py b/tests/components/test_emulated_hue.py index ef4ea7f234e..edb2181c813 100755 --- a/tests/components/test_emulated_hue.py +++ b/tests/components/test_emulated_hue.py @@ -1,5 +1,4 @@ """The tests for the emulated Hue component.""" -import time import json import unittest @@ -43,7 +42,6 @@ def setup_hass_instance(emulated_hue_config): def start_hass_instance(hass): """Start the Home Assistant instance to test.""" hass.start() - time.sleep(0.05) class TestEmulatedHue(unittest.TestCase): diff --git a/tests/components/test_frontend.py b/tests/components/test_frontend.py index 46686a5c66f..3ff366babd9 100644 --- a/tests/components/test_frontend.py +++ b/tests/components/test_frontend.py @@ -1,7 +1,6 @@ """The tests for Home Assistant frontend.""" # pylint: disable=protected-access import re -import time import unittest import requests @@ -32,9 +31,6 @@ def setUpModule(): hass = get_test_home_assistant() - hass.bus.listen('test_event', lambda _: _) - hass.states.set('test.test', 'a_state') - assert bootstrap.setup_component( hass, http.DOMAIN, {http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD, @@ -43,7 +39,6 @@ def setUpModule(): assert bootstrap.setup_component(hass, 'frontend') hass.start() - time.sleep(0.05) # pylint: disable=invalid-name @@ -63,7 +58,6 @@ class TestFrontend(unittest.TestCase): def test_frontend_and_static(self): """Test if we can get the frontend.""" req = requests.get(_url("")) - self.assertEqual(200, req.status_code) # Test we can retrieve frontend.js @@ -72,9 +66,7 @@ class TestFrontend(unittest.TestCase): req.text) self.assertIsNotNone(frontendjs) - req = requests.get(_url(frontendjs.groups(0)[0])) - self.assertEqual(200, req.status_code) def test_404(self): @@ -84,3 +76,15 @@ class TestFrontend(unittest.TestCase): def test_we_cannot_POST_to_root(self): """Test that POST is not allow to root.""" self.assertEqual(405, requests.post(_url("")).status_code) + + def test_states_routes(self): + """All served by index.""" + req = requests.get(_url("/states")) + self.assertEqual(200, req.status_code) + + req = requests.get(_url("/states/group.non_existing")) + self.assertEqual(404, req.status_code) + + hass.states.set('group.existing', 'on', {'view': True}) + req = requests.get(_url("/states/group.existing")) + self.assertEqual(200, req.status_code) diff --git a/tests/components/test_http.py b/tests/components/test_http.py index 565809a8cc3..42a0498ae60 100644 --- a/tests/components/test_http.py +++ b/tests/components/test_http.py @@ -1,7 +1,6 @@ """The tests for the Home Assistant HTTP component.""" # pylint: disable=protected-access import logging -import time from ipaddress import ip_network from unittest.mock import patch @@ -61,7 +60,6 @@ def setUpModule(): for trusted_network in TRUSTED_NETWORKS] hass.start() - time.sleep(0.05) # pylint: disable=invalid-name diff --git a/tests/test_remote.py b/tests/test_remote.py index df41c2ebd10..8692fd4a133 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -2,7 +2,6 @@ # pylint: disable=protected-access import asyncio import threading -import time import unittest from unittest.mock import patch @@ -51,7 +50,6 @@ def setUpModule(): bootstrap.setup_component(hass, 'api') hass.start() - time.sleep(0.05) master_api = remote.API('127.0.0.1', API_PASSWORD, MASTER_PORT)