Home Assistant Unofficial Reference 2024.12.1
websocket_api.py
Go to the documentation of this file.
1 """Websocket API handlers for the logger integration."""
2 
3 from typing import Any
4 
5 import voluptuous as vol
6 
7 from homeassistant.components import websocket_api
8 from homeassistant.components.websocket_api import ActiveConnection
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.loader import IntegrationNotFound, async_get_integration
11 from homeassistant.setup import async_get_loaded_integrations
12 
13 from .const import LOGSEVERITY
14 from .helpers import (
15  LoggerSetting,
16  LogPersistance,
17  LogSettingsType,
18  async_get_domain_config,
19  get_logger,
20 )
21 
22 
23 @callback
24 def async_load_websocket_api(hass: HomeAssistant) -> None:
25  """Set up the websocket API."""
26  websocket_api.async_register_command(hass, handle_integration_log_info)
27  websocket_api.async_register_command(hass, handle_integration_log_level)
28  websocket_api.async_register_command(hass, handle_module_log_level)
29 
30 
31 @callback
32 @websocket_api.websocket_command({vol.Required("type"): "logger/log_info"})
34  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
35 ) -> None:
36  """Handle integrations logger info."""
37  connection.send_result(
38  msg["id"],
39  [
40  {
41  "domain": integration,
42  "level": get_logger(
43  f"homeassistant.components.{integration}"
44  ).getEffectiveLevel(),
45  }
46  for integration in async_get_loaded_integrations(hass)
47  ],
48  )
49 
50 
51 @websocket_api.websocket_command( { vol.Required("type"): "logger/integration_log_level",
52  vol.Required("integration"): str,
53  vol.Required("level"): vol.In(LOGSEVERITY),
54  vol.Required("persistence"): vol.Coerce(LogPersistance),
55  }
56 )
57 @websocket_api.async_response
59  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
60 ) -> None:
61  """Handle setting integration log level."""
62  try:
63  await async_get_integration(hass, msg["integration"])
64  except IntegrationNotFound:
65  connection.send_error(
66  msg["id"], websocket_api.ERR_NOT_FOUND, "Integration not found"
67  )
68  return
69  await async_get_domain_config(hass).settings.async_update(
70  hass,
71  msg["integration"],
73  level=msg["level"],
74  persistence=msg["persistence"],
75  type=LogSettingsType.INTEGRATION,
76  ),
77  )
78  connection.send_message(websocket_api.messages.result_message(msg["id"]))
79 
80 
81 @websocket_api.websocket_command( { vol.Required("type"): "logger/log_level",
82  vol.Required("module"): str,
83  vol.Required("level"): vol.In(LOGSEVERITY),
84  vol.Required("persistence"): vol.Coerce(LogPersistance),
85  }
86 )
87 @websocket_api.async_response
88 async def handle_module_log_level(
89  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
90 ) -> None:
91  """Handle setting integration log level."""
92  await async_get_domain_config(hass).settings.async_update(
93  hass,
94  msg["module"],
96  level=msg["level"],
97  persistence=msg["persistence"],
98  type=LogSettingsType.MODULE,
99  ),
100  )
101  connection.send_message(websocket_api.messages.result_message(msg["id"]))
102 
LoggerDomainConfig async_get_domain_config(HomeAssistant hass)
Definition: helpers.py:43
None handle_module_log_level(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None async_load_websocket_api(HomeAssistant hass)
None handle_integration_log_level(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None handle_integration_log_info(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
Integration async_get_integration(HomeAssistant hass, str domain)
Definition: loader.py:1354
set[str] async_get_loaded_integrations(core.HomeAssistant hass)
Definition: setup.py:651