Add lightweight API to get core state (#96860)
parent
39b242f154
commit
29aa89bea0
|
@ -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."""
|
||||
|
||||
|
|
|
@ -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/{}"
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue