Ensure frontend is available if integrations fail to start - Part 1 of 2 (#36093)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
9c45115468
commit
fbe7b4ddfa
13 files changed
+173
-63
No files matched your search
@@ -4,7 +4,7 @@ import logging
|
||||
import os
|
||||
import ssl
|
||||
from traceback import extract_stack
|
||||
from typing import Optional, cast
|
||||
from typing import Dict, Optional, cast
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import HTTPMovedPermanently
|
||||
@@ -15,7 +15,7 @@ from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
SERVER_PORT,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.helpers import storage
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.loader import bind_hass
|
||||
@@ -216,29 +216,25 @@ async def async_setup(hass, config):
|
||||
ssl_profile=ssl_profile,
|
||||
)
|
||||
|
||||
async def stop_server(event):
|
||||
startup_listeners = []
|
||||
|
||||
async def stop_server(event: Event) -> None:
|
||||
"""Stop the server."""
|
||||
await server.stop()
|
||||
|
||||
async def start_server(event):
|
||||
async def start_server(event: Event) -> None:
|
||||
"""Start the server."""
|
||||
|
||||
for listener in startup_listeners:
|
||||
listener()
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_server)
|
||||
await server.start()
|
||||
|
||||
# If we are set up successful, we store the HTTP settings for safe mode.
|
||||
store = storage.Store(hass, STORAGE_VERSION, STORAGE_KEY)
|
||||
await start_http_server_and_save_config(hass, dict(conf), server)
|
||||
|
||||
if CONF_TRUSTED_PROXIES in conf:
|
||||
conf_to_save = dict(conf)
|
||||
conf_to_save[CONF_TRUSTED_PROXIES] = [
|
||||
str(ip.network_address) for ip in conf_to_save[CONF_TRUSTED_PROXIES]
|
||||
]
|
||||
else:
|
||||
conf_to_save = conf
|
||||
|
||||
await store.async_save(conf_to_save)
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_server)
|
||||
startup_listeners.append(
|
||||
hass.bus.async_listen(EVENT_HOMEASSISTANT_START, start_server)
|
||||
)
|
||||
|
||||
hass.http = server
|
||||
|
||||
@@ -418,3 +414,20 @@ class HomeAssistantHTTP:
|
||||
"""Stop the aiohttp server."""
|
||||
await self.site.stop()
|
||||
await self.runner.cleanup()
|
||||
|
||||
|
||||
async def start_http_server_and_save_config(
|
||||
hass: HomeAssistant, conf: Dict, server: HomeAssistantHTTP
|
||||
) -> None:
|
||||
"""Startup the http server and save the config."""
|
||||
await server.start() # type: ignore
|
||||
|
||||
# If we are set up successful, we store the HTTP settings for safe mode.
|
||||
store = storage.Store(hass, STORAGE_VERSION, STORAGE_KEY)
|
||||
|
||||
if CONF_TRUSTED_PROXIES in conf:
|
||||
conf[CONF_TRUSTED_PROXIES] = [
|
||||
str(ip.network_address) for ip in conf[CONF_TRUSTED_PROXIES]
|
||||
]
|
||||
|
||||
await store.async_save(conf)
|
||||
@@ -14,7 +14,7 @@ from aiohttp.web_exceptions import (
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import exceptions
|
||||
from homeassistant.const import CONTENT_TYPE_JSON, HTTP_OK
|
||||
from homeassistant.const import CONTENT_TYPE_JSON, HTTP_OK, HTTP_SERVICE_UNAVAILABLE
|
||||
from homeassistant.core import Context, is_callback
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
|
||||
@@ -107,8 +107,8 @@ def request_handler_factory(view: HomeAssistantView, handler: Callable) -> Calla
|
||||
|
||||
async def handle(request: web.Request) -> web.StreamResponse:
|
||||
"""Handle incoming request."""
|
||||
if not request.app[KEY_HASS].is_running:
|
||||
return web.Response(status=503)
|
||||
if request.app[KEY_HASS].is_stopping:
|
||||
return web.Response(status=HTTP_SERVICE_UNAVAILABLE)
|
||||
|
||||
authenticated = request.get(KEY_AUTHENTICATED, False)
|
||||
|
||||
|
||||
Reference in new issue
Block a user