diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 0404f4a0df6..dc18dd2481d 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -289,10 +289,21 @@ class HomeAssistantWSGI(object): else: context = None + # Aiohttp freezes apps after start so that no changes can be made. + # However in Home Assistant components can be discovered after boot. + # This will now raise a RunTimeError. + # To work around this we now fake that we are frozen. + # A more appropriate fix would be to create a new app and + # re-register all redirects, views, static paths. + self.app._frozen = True # pylint: disable=protected-access + self._handler = self.app.make_handler() + self.server = yield from self.hass.loop.create_server( self._handler, self.server_host, self.server_port, ssl=context) + self.app._frozen = False # pylint: disable=protected-access + @asyncio.coroutine def stop(self): """Stop the wsgi server.""" diff --git a/tests/components/http/test_init.py b/tests/components/http/test_init.py index a1e0532bc14..cd0d4fe1ffa 100644 --- a/tests/components/http/test_init.py +++ b/tests/components/http/test_init.py @@ -1,4 +1,5 @@ """The tests for the Home Assistant HTTP component.""" +import asyncio import requests from homeassistant import bootstrap, const @@ -109,3 +110,42 @@ class TestHttp: assert req.headers.get(allow_origin) == HTTP_BASE_URL assert req.headers.get(allow_headers) == \ const.HTTP_HEADER_HA_AUTH.upper() + + +class TestView(http.HomeAssistantView): + + name = 'test' + url = '/hello' + + @asyncio.coroutine + def get(self, request): + """Return a get request.""" + return 'hello' + + +@asyncio.coroutine +def test_registering_view_while_running(hass, test_client): + """Test that we can register a view while the server is running.""" + yield from bootstrap.async_setup_component( + hass, http.DOMAIN, { + http.DOMAIN: { + http.CONF_SERVER_PORT: get_test_instance_port(), + } + } + ) + + yield from bootstrap.async_setup_component(hass, 'api') + + yield from hass.async_start() + + yield from hass.async_block_till_done() + + hass.http.register_view(TestView) + + client = yield from test_client(hass.http.app) + + resp = yield from client.get('/hello') + assert resp.status == 200 + + text = yield from resp.text() + assert text == 'hello'