From 29aa89bea095d174fae39d1cf33b45eb2a54c297 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 19 Jul 2023 13:31:48 -0500 Subject: [PATCH] Add lightweight API to get core state (#96860) --- homeassistant/components/api/__init__.py | 20 ++++++++++++++++++++ homeassistant/const.py | 1 + tests/components/api/test_init.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/homeassistant/components/api/__init__.py b/homeassistant/components/api/__init__.py index 6538bd345de..b465a6b7037 100644 --- a/homeassistant/components/api/__init__.py +++ b/homeassistant/components/api/__init__.py @@ -18,6 +18,7 @@ from homeassistant.const import ( URL_API, URL_API_COMPONENTS, URL_API_CONFIG, + URL_API_CORE_STATE, URL_API_ERROR_LOG, URL_API_EVENTS, URL_API_SERVICES, @@ -55,6 +56,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Register the API with the HTTP interface.""" hass.http.register_view(APIStatusView) + hass.http.register_view(APICoreStateView) hass.http.register_view(APIEventStream) hass.http.register_view(APIConfigView) hass.http.register_view(APIStatesView) @@ -84,6 +86,24 @@ class APIStatusView(HomeAssistantView): return self.json_message("API running.") +class APICoreStateView(HomeAssistantView): + """View to handle core state requests.""" + + url = URL_API_CORE_STATE + name = "api:core:state" + + @ha.callback + def get(self, request: web.Request) -> web.Response: + """Retrieve the current core state. + + This API is intended to be a fast and lightweight way to check if the + Home Assistant core is running. Its primary use case is for supervisor + to check if Home Assistant is running. + """ + hass: HomeAssistant = request.app["hass"] + return self.json({"state": hass.state.value}) + + class APIEventStream(HomeAssistantView): """View to handle EventStream requests.""" diff --git a/homeassistant/const.py b/homeassistant/const.py index f3d3d48fdd2..5394e273a4c 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1101,6 +1101,7 @@ SERVER_PORT: Final = 8123 URL_ROOT: Final = "/" URL_API: Final = "/api/" URL_API_STREAM: Final = "/api/stream" +URL_API_CORE_STATE: Final = "/api/core/state" URL_API_CONFIG: Final = "/api/config" URL_API_STATES: Final = "/api/states" URL_API_STATES_ENTITY: Final = "/api/states/{}" diff --git a/tests/components/api/test_init.py b/tests/components/api/test_init.py index 61da000fc07..5ba9d60996b 100644 --- a/tests/components/api/test_init.py +++ b/tests/components/api/test_init.py @@ -678,3 +678,19 @@ async def test_api_call_service_bad_data( "/api/services/test_domain/test_service", json={"hello": 5} ) assert resp.status == HTTPStatus.BAD_REQUEST + + +async def test_api_status(hass: HomeAssistant, mock_api_client: TestClient) -> None: + """Test getting the api status.""" + resp = await mock_api_client.get("/api/") + assert resp.status == HTTPStatus.OK + json = await resp.json() + assert json["message"] == "API running." + + +async def test_api_core_state(hass: HomeAssistant, mock_api_client: TestClient) -> None: + """Test getting core status.""" + resp = await mock_api_client.get("/api/core/state") + assert resp.status == HTTPStatus.OK + json = await resp.json() + assert json["state"] == "RUNNING"