Home Assistant Unofficial Reference 2024.12.1
basic_websocket_api.py
Go to the documentation of this file.
1 """The Recorder websocket API."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.components import websocket_api
10 from homeassistant.core import HomeAssistant, callback
11 
12 from .util import get_instance
13 
14 
15 @callback
16 def async_setup(hass: HomeAssistant) -> None:
17  """Set up the recorder websocket API."""
18  websocket_api.async_register_command(hass, ws_info)
19 
20 
21 @websocket_api.websocket_command( { vol.Required("type"): "recorder/info",
22  }
23 )
24 @callback
25 def ws_info(
26  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
27 ) -> None:
28  """Return status of the recorder."""
29  if instance := get_instance(hass):
30  backlog = instance.backlog
31  migration_in_progress = instance.migration_in_progress
32  migration_is_live = instance.migration_is_live
33  recording = instance.recording
34  # We avoid calling is_alive() as it can block waiting
35  # for the thread state lock which will block the event loop.
36  is_running = instance.is_running
37  max_backlog = instance.max_backlog
38  else:
39  backlog = None
40  migration_in_progress = False
41  migration_is_live = False
42  recording = False
43  is_running = False
44  max_backlog = None
45 
46  recorder_info = {
47  "backlog": backlog,
48  "max_backlog": max_backlog,
49  "migration_in_progress": migration_in_progress,
50  "migration_is_live": migration_is_live,
51  "recording": recording,
52  "thread_running": is_running,
53  }
54  connection.send_result(msg["id"], recorder_info)
55 
None ws_info(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Recorder get_instance(HomeAssistant hass)
Definition: recorder.py:74