diff --git a/homeassistant/components/recorder/basic_websocket_api.py b/homeassistant/components/recorder/basic_websocket_api.py new file mode 100644 index 00000000000..9cbc77b30c0 --- /dev/null +++ b/homeassistant/components/recorder/basic_websocket_api.py @@ -0,0 +1,56 @@ +"""The Recorder websocket API.""" + +from __future__ import annotations + +from typing import Any + +import voluptuous as vol + +from homeassistant.components import websocket_api +from homeassistant.core import HomeAssistant, callback + +from .util import get_instance + + +@callback +def async_setup(hass: HomeAssistant) -> None: + """Set up the recorder websocket API.""" + websocket_api.async_register_command(hass, ws_info) + + +@websocket_api.websocket_command( + { + vol.Required("type"): "recorder/info", + } +) +@callback +def ws_info( + hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] +) -> None: + """Return status of the recorder.""" + if instance := get_instance(hass): + backlog = instance.backlog + migration_in_progress = instance.migration_in_progress + migration_is_live = instance.migration_is_live + recording = instance.recording + # We avoid calling is_alive() as it can block waiting + # for the thread state lock which will block the event loop. + is_running = instance.is_running + max_backlog = instance.max_backlog + else: + backlog = None + migration_in_progress = False + migration_is_live = False + recording = False + is_running = False + max_backlog = None + + recorder_info = { + "backlog": backlog, + "max_backlog": max_backlog, + "migration_in_progress": migration_in_progress, + "migration_is_live": migration_is_live, + "recording": recording, + "thread_running": is_running, + } + connection.send_result(msg["id"], recorder_info) diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index 195d3d3efb0..5e0eef37721 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -79,7 +79,6 @@ def async_setup(hass: HomeAssistant) -> None: websocket_api.async_register_command(hass, ws_get_statistics_metadata) websocket_api.async_register_command(hass, ws_list_statistic_ids) websocket_api.async_register_command(hass, ws_import_statistics) - websocket_api.async_register_command(hass, ws_info) websocket_api.async_register_command(hass, ws_update_statistics_metadata) websocket_api.async_register_command(hass, ws_validate_statistics) @@ -475,41 +474,3 @@ def ws_import_statistics( else: async_add_external_statistics(hass, metadata, stats) connection.send_result(msg["id"]) - - -@websocket_api.websocket_command( - { - vol.Required("type"): "recorder/info", - } -) -@callback -def ws_info( - hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] -) -> None: - """Return status of the recorder.""" - if instance := get_instance(hass): - backlog = instance.backlog - migration_in_progress = instance.migration_in_progress - migration_is_live = instance.migration_is_live - recording = instance.recording - # We avoid calling is_alive() as it can block waiting - # for the thread state lock which will block the event loop. - is_running = instance.is_running - max_backlog = instance.max_backlog - else: - backlog = None - migration_in_progress = False - migration_is_live = False - recording = False - is_running = False - max_backlog = None - - recorder_info = { - "backlog": backlog, - "max_backlog": max_backlog, - "migration_in_progress": migration_in_progress, - "migration_is_live": migration_is_live, - "recording": recording, - "thread_running": is_running, - } - connection.send_result(msg["id"], recorder_info) diff --git a/homeassistant/helpers/recorder.py b/homeassistant/helpers/recorder.py index 6155fc9b320..ac534a7230a 100644 --- a/homeassistant/helpers/recorder.py +++ b/homeassistant/helpers/recorder.py @@ -33,7 +33,11 @@ def async_migration_in_progress(hass: HomeAssistant) -> bool: @callback def async_initialize_recorder(hass: HomeAssistant) -> None: """Initialize recorder data.""" + # pylint: disable-next=import-outside-toplevel + from homeassistant.components.recorder.basic_websocket_api import async_setup + hass.data[DOMAIN] = RecorderData() + async_setup(hass) async def async_wait_recorder(hass: HomeAssistant) -> bool: diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 1bf56372620..bcdf07502b0 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -2491,7 +2491,7 @@ async def test_recorder_info_no_instance( client = await hass_ws_client() with patch( - "homeassistant.components.recorder.websocket_api.get_instance", + "homeassistant.components.recorder.basic_websocket_api.get_instance", return_value=None, ): await client.send_json_auto_id({"type": "recorder/info"})