Handle new API / Frontend early boot (#1770)

* Handle new API / Frontend early boot

* Adjust part 2

* fix hanging landingpage

* Fix catch error

* Fix watchdog
This commit is contained in:
Pascal Vizeli 2020-06-02 14:53:57 +02:00 committed by GitHub
parent ec43448163
commit af412c3105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -75,6 +75,8 @@ class APIProxy(CoreSysAttributes):
async def stream(self, request: web.Request):
"""Proxy HomeAssistant EventStream Requests."""
self._check_access(request)
if not await self.sys_homeassistant.check_api_state():
raise HTTPBadGateway()
_LOGGER.info("Home Assistant EventStream start")
async with self._api_client(request, "stream", timeout=None) as client:
@ -94,6 +96,8 @@ class APIProxy(CoreSysAttributes):
async def api(self, request: web.Request):
"""Proxy Home Assistant API Requests."""
self._check_access(request)
if not await self.sys_homeassistant.check_api_state():
raise HTTPBadGateway()
# Normal request
path = request.match_info.get("path", "")
@ -153,6 +157,8 @@ class APIProxy(CoreSysAttributes):
async def websocket(self, request: web.Request):
"""Initialize a WebSocket API connection."""
if not await self.sys_homeassistant.check_api_state():
raise HTTPBadGateway()
_LOGGER.info("Home Assistant WebSocket API request initialize")
# init server

View File

@ -374,6 +374,10 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
await self.instance.run()
except DockerAPIError:
raise HomeAssistantError() from None
# Don't block for landingpage
if self.version == "landingpage":
return
await self._block_till_run()
@process_lock
@ -559,12 +563,21 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
async def check_api_state(self) -> bool:
"""Return True if Home Assistant up and running."""
# Check if port is up
if not await self.sys_run_in_executor(
check_port, self.ip_address, self.api_port
):
return False
# Check if API is up
with suppress(HomeAssistantAPIError):
async with self.make_request("get", "api/") as resp:
async with self.make_request("get", "api/config") as resp:
if resp.status in (200, 201):
return True
status = resp.status
_LOGGER.warning("Home Assistant API config mismatch: %s", status)
data = await resp.json()
if data.get("state", "RUNNING") == "RUNNING":
return True
else:
_LOGGER.debug("Home Assistant API return: %d", resp.status)
return False
@ -589,9 +602,7 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
break
# 2: Check if API response
if await self.sys_run_in_executor(
check_port, self.ip_address, self.api_port
):
if await self.check_api_state():
_LOGGER.info("Detect a running Home Assistant instance")
self._error_state = False
return