Home Assistant Unofficial Reference 2024.12.1
websocket.py
Go to the documentation of this file.
1 """Websocket commands for the Backup integration."""
2 
3 from typing import Any
4 
5 import voluptuous as vol
6 
7 from homeassistant.components import websocket_api
8 from homeassistant.core import HomeAssistant, callback
9 
10 from .const import DATA_MANAGER, LOGGER
11 
12 
13 @callback
14 def async_register_websocket_handlers(hass: HomeAssistant, with_hassio: bool) -> None:
15  """Register websocket commands."""
16  if with_hassio:
17  websocket_api.async_register_command(hass, handle_backup_end)
18  websocket_api.async_register_command(hass, handle_backup_start)
19  return
20 
21  websocket_api.async_register_command(hass, handle_details)
22  websocket_api.async_register_command(hass, handle_info)
23  websocket_api.async_register_command(hass, handle_create)
24  websocket_api.async_register_command(hass, handle_remove)
25  websocket_api.async_register_command(hass, handle_restore)
26 
27 
28 @websocket_api.require_admin
29 @websocket_api.websocket_command({vol.Required("type"): "backup/info"})
30 @websocket_api.async_response
31 async def handle_info(
32  hass: HomeAssistant,
34  msg: dict[str, Any],
35 ) -> None:
36  """List all stored backups."""
37  manager = hass.data[DATA_MANAGER]
38  backups = await manager.async_get_backups()
39  connection.send_result(
40  msg["id"],
41  {
42  "backups": list(backups.values()),
43  "backing_up": manager.backing_up,
44  },
45  )
46 
47 
48 @websocket_api.require_admin
49 @websocket_api.websocket_command( { vol.Required("type"): "backup/details",
50  vol.Required("slug"): str,
51  }
52 )
53 @websocket_api.async_response
54 async def handle_details(
55  hass: HomeAssistant,
57  msg: dict[str, Any],
58 ) -> None:
59  """Get backup details for a specific slug."""
60  backup = await hass.data[DATA_MANAGER].async_get_backup(slug=msg["slug"])
61  connection.send_result(
62  msg["id"],
63  {
64  "backup": backup,
65  },
66  )
67 
68 
69 @websocket_api.require_admin
70 @websocket_api.websocket_command( { vol.Required("type"): "backup/remove",
71  vol.Required("slug"): str,
72  }
73 )
74 @websocket_api.async_response
75 async def handle_remove(
76  hass: HomeAssistant,
78  msg: dict[str, Any],
79 ) -> None:
80  """Remove a backup."""
81  await hass.data[DATA_MANAGER].async_remove_backup(slug=msg["slug"])
82  connection.send_result(msg["id"])
83 
84 
85 @websocket_api.require_admin
86 @websocket_api.websocket_command( { vol.Required("type"): "backup/restore",
87  vol.Required("slug"): str,
88  }
89 )
90 @websocket_api.async_response
91 async def handle_restore(
92  hass: HomeAssistant,
94  msg: dict[str, Any],
95 ) -> None:
96  """Restore a backup."""
97  await hass.data[DATA_MANAGER].async_restore_backup(msg["slug"])
98  connection.send_result(msg["id"])
99 
100 
101 @websocket_api.require_admin
102 @websocket_api.websocket_command({vol.Required("type"): "backup/generate"})
103 @websocket_api.async_response
104 async def handle_create(
105  hass: HomeAssistant,
106  connection: websocket_api.ActiveConnection,
107  msg: dict[str, Any],
108 ) -> None:
109  """Generate a backup."""
110  backup = await hass.data[DATA_MANAGER].async_create_backup()
111  connection.send_result(msg["id"], backup)
112 
113 
114 @websocket_api.ws_require_user(only_supervisor=True)
115 @websocket_api.websocket_command({vol.Required("type"): "backup/start"})
116 @websocket_api.async_response
117 async def handle_backup_start(
118  hass: HomeAssistant,
119  connection: websocket_api.ActiveConnection,
120  msg: dict[str, Any],
121 ) -> None:
122  """Backup start notification."""
123  manager = hass.data[DATA_MANAGER]
124  manager.backing_up = True
125  LOGGER.debug("Backup start notification")
126 
127  try:
128  await manager.async_pre_backup_actions()
129  except Exception as err: # noqa: BLE001
130  connection.send_error(msg["id"], "pre_backup_actions_failed", str(err))
131  return
132 
133  connection.send_result(msg["id"])
134 
135 
136 @websocket_api.ws_require_user(only_supervisor=True)
137 @websocket_api.websocket_command({vol.Required("type"): "backup/end"})
138 @websocket_api.async_response
139 async def handle_backup_end(
140  hass: HomeAssistant,
141  connection: websocket_api.ActiveConnection,
142  msg: dict[str, Any],
143 ) -> None:
144  """Backup end notification."""
145  manager = hass.data[DATA_MANAGER]
146  manager.backing_up = False
147  LOGGER.debug("Backup end notification")
148 
149  try:
150  await manager.async_post_backup_actions()
151  except Exception as err: # noqa: BLE001
152  connection.send_error(msg["id"], "post_backup_actions_failed", str(err))
153  return
154 
155  connection.send_result(msg["id"])
156 
None handle_info(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:35
None handle_backup_start(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:127
None handle_backup_end(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:149
None handle_details(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:60
None handle_remove(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:83
None async_register_websocket_handlers(HomeAssistant hass, bool with_hassio)
Definition: websocket.py:14
None handle_create(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:114
None handle_restore(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: websocket.py:101
dict async_create_backup(HomeAssistant hass, dict payload, bool partial=False)
Definition: handler.py:83