Register WS command recorder/info early (#122425)
parent
ba276a5cb6
commit
42716723e6
|
@ -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)
|
|
@ -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_get_statistics_metadata)
|
||||||
websocket_api.async_register_command(hass, ws_list_statistic_ids)
|
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_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_update_statistics_metadata)
|
||||||
websocket_api.async_register_command(hass, ws_validate_statistics)
|
websocket_api.async_register_command(hass, ws_validate_statistics)
|
||||||
|
|
||||||
|
@ -475,41 +474,3 @@ def ws_import_statistics(
|
||||||
else:
|
else:
|
||||||
async_add_external_statistics(hass, metadata, stats)
|
async_add_external_statistics(hass, metadata, stats)
|
||||||
connection.send_result(msg["id"])
|
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)
|
|
||||||
|
|
|
@ -33,7 +33,11 @@ def async_migration_in_progress(hass: HomeAssistant) -> bool:
|
||||||
@callback
|
@callback
|
||||||
def async_initialize_recorder(hass: HomeAssistant) -> None:
|
def async_initialize_recorder(hass: HomeAssistant) -> None:
|
||||||
"""Initialize recorder data."""
|
"""Initialize recorder data."""
|
||||||
|
# pylint: disable-next=import-outside-toplevel
|
||||||
|
from homeassistant.components.recorder.basic_websocket_api import async_setup
|
||||||
|
|
||||||
hass.data[DOMAIN] = RecorderData()
|
hass.data[DOMAIN] = RecorderData()
|
||||||
|
async_setup(hass)
|
||||||
|
|
||||||
|
|
||||||
async def async_wait_recorder(hass: HomeAssistant) -> bool:
|
async def async_wait_recorder(hass: HomeAssistant) -> bool:
|
||||||
|
|
|
@ -2491,7 +2491,7 @@ async def test_recorder_info_no_instance(
|
||||||
client = await hass_ws_client()
|
client = await hass_ws_client()
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.recorder.websocket_api.get_instance",
|
"homeassistant.components.recorder.basic_websocket_api.get_instance",
|
||||||
return_value=None,
|
return_value=None,
|
||||||
):
|
):
|
||||||
await client.send_json_auto_id({"type": "recorder/info"})
|
await client.send_json_auto_id({"type": "recorder/info"})
|
||||||
|
|
Loading…
Reference in New Issue