diff --git a/supervisor/api/proxy.py b/supervisor/api/proxy.py index 99548f2e0..812390ff4 100644 --- a/supervisor/api/proxy.py +++ b/supervisor/api/proxy.py @@ -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 diff --git a/supervisor/homeassistant.py b/supervisor/homeassistant.py index 0aeba636e..f2b43b6bf 100644 --- a/supervisor/homeassistant.py +++ b/supervisor/homeassistant.py @@ -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