Home Assistant Unofficial Reference 2024.12.1
category_registry.py
Go to the documentation of this file.
1 """Websocket API to interact with the category registry."""
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.helpers import category_registry as cr, config_validation as cv
11 
12 
13 @callback
14 def async_setup(hass: HomeAssistant) -> bool:
15  """Register the category registry WS commands."""
16  websocket_api.async_register_command(hass, websocket_list_categories)
17  websocket_api.async_register_command(hass, websocket_create_category)
18  websocket_api.async_register_command(hass, websocket_delete_category)
19  websocket_api.async_register_command(hass, websocket_update_category)
20  return True
21 
22 
23 @websocket_api.websocket_command( { vol.Required("type"): "config/category_registry/list",
24  vol.Required("scope"): str,
25  }
26 )
27 @callback
29  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
30 ) -> None:
31  """Handle list categories command."""
32  category_registry = cr.async_get(hass)
33  connection.send_result(
34  msg["id"],
35  [
36  _entry_dict(entry)
37  for entry in category_registry.async_list_categories(scope=msg["scope"])
38  ],
39  )
40 
41 
42 @websocket_api.websocket_command( { vol.Required("type"): "config/category_registry/create",
43  vol.Required("scope"): str,
44  vol.Required("name"): str,
45  vol.Optional("icon"): vol.Any(cv.icon, None),
46  }
47 )
48 @websocket_api.require_admin
49 @callback
51  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
52 ) -> None:
53  """Create category command."""
54  category_registry = cr.async_get(hass)
55 
56  data = dict(msg)
57  data.pop("type")
58  data.pop("id")
59 
60  try:
61  entry = category_registry.async_create(**data)
62  except ValueError as err:
63  connection.send_error(msg["id"], "invalid_info", str(err))
64  else:
65  connection.send_result(msg["id"], _entry_dict(entry))
66 
67 
68 @websocket_api.websocket_command( { vol.Required("type"): "config/category_registry/delete",
69  vol.Required("scope"): str,
70  vol.Required("category_id"): str,
71  }
72 )
73 @websocket_api.require_admin
74 @callback
76  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
77 ) -> None:
78  """Delete category command."""
79  category_registry = cr.async_get(hass)
80 
81  try:
82  category_registry.async_delete(
83  scope=msg["scope"], category_id=msg["category_id"]
84  )
85  except KeyError:
86  connection.send_error(msg["id"], "invalid_info", "Category ID doesn't exist")
87  else:
88  connection.send_result(msg["id"])
89 
90 
91 @websocket_api.websocket_command( { vol.Required("type"): "config/category_registry/update",
92  vol.Required("scope"): str,
93  vol.Required("category_id"): str,
94  vol.Optional("name"): str,
95  vol.Optional("icon"): vol.Any(cv.icon, None),
96  }
97 )
98 @websocket_api.require_admin
99 @callback
101  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
102 ) -> None:
103  """Handle update category websocket command."""
104  category_registry = cr.async_get(hass)
105 
106  data = dict(msg)
107  data.pop("type")
108  data.pop("id")
109 
110  try:
111  entry = category_registry.async_update(**data)
112  except ValueError as err:
113  connection.send_error(msg["id"], "invalid_info", str(err))
114  except KeyError:
115  connection.send_error(msg["id"], "invalid_info", "Category ID doesn't exist")
116  else:
117  connection.send_result(msg["id"], _entry_dict(entry))
118 
119 
120 @callback
121 def _entry_dict(entry: cr.CategoryEntry) -> dict[str, Any]:
122  """Convert entry to API format."""
123  return {
124  "category_id": entry.category_id,
125  "created_at": entry.created_at.timestamp(),
126  "icon": entry.icon,
127  "modified_at": entry.modified_at.timestamp(),
128  "name": entry.name,
129  }
130 
None websocket_delete_category(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None websocket_create_category(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
dict[str, Any] _entry_dict(cr.CategoryEntry entry)
None websocket_list_categories(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None websocket_update_category(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)