Home Assistant Unofficial Reference 2024.12.1
label_registry.py
Go to the documentation of this file.
1 """Websocket API to interact with the label 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 config_validation as cv, label_registry as lr
11 from homeassistant.helpers.label_registry import LabelEntry
12 
13 SUPPORTED_LABEL_THEME_COLORS = {
14  "primary",
15  "accent",
16  "disabled",
17  "red",
18  "pink",
19  "purple",
20  "deep-purple",
21  "indigo",
22  "blue",
23  "light-blue",
24  "cyan",
25  "teal",
26  "green",
27  "light-green",
28  "lime",
29  "yellow",
30  "amber",
31  "orange",
32  "deep-orange",
33  "brown",
34  "light-grey",
35  "grey",
36  "dark-grey",
37  "blue-grey",
38  "black",
39  "white",
40 }
41 
42 
43 @callback
44 def async_setup(hass: HomeAssistant) -> bool:
45  """Register the Label Registry WS commands."""
46  websocket_api.async_register_command(hass, websocket_list_labels)
47  websocket_api.async_register_command(hass, websocket_create_label)
48  websocket_api.async_register_command(hass, websocket_delete_label)
49  websocket_api.async_register_command(hass, websocket_update_label)
50  return True
51 
52 
53 @websocket_api.websocket_command( { vol.Required("type"): "config/label_registry/list",
54  }
55 )
56 @callback
58  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
59 ) -> None:
60  """Handle list labels command."""
61  registry = lr.async_get(hass)
62  connection.send_result(
63  msg["id"],
64  [_entry_dict(entry) for entry in registry.async_list_labels()],
65  )
66 
67 
68 @websocket_api.websocket_command( { vol.Required("type"): "config/label_registry/create",
69  vol.Required("name"): str,
70  vol.Optional("color"): vol.Any(
71  cv.color_hex, vol.In(SUPPORTED_LABEL_THEME_COLORS), None
72  ),
73  vol.Optional("description"): vol.Any(str, None),
74  vol.Optional("icon"): vol.Any(cv.icon, None),
75  }
76 )
77 @websocket_api.require_admin
78 @callback
80  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
81 ) -> None:
82  """Create label command."""
83  registry = lr.async_get(hass)
84 
85  data = dict(msg)
86  data.pop("type")
87  data.pop("id")
88 
89  try:
90  entry = registry.async_create(**data)
91  except ValueError as err:
92  connection.send_error(msg["id"], "invalid_info", str(err))
93  else:
94  connection.send_result(msg["id"], _entry_dict(entry))
95 
96 
97 @websocket_api.websocket_command( { vol.Required("type"): "config/label_registry/delete",
98  vol.Required("label_id"): str,
99  }
100 )
101 @websocket_api.require_admin
102 @callback
104  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
105 ) -> None:
106  """Delete label command."""
107  registry = lr.async_get(hass)
108 
109  try:
110  registry.async_delete(msg["label_id"])
111  except KeyError:
112  connection.send_error(msg["id"], "invalid_info", "Label ID doesn't exist")
113  else:
114  connection.send_result(msg["id"])
115 
116 
117 @websocket_api.websocket_command( { vol.Required("type"): "config/label_registry/update",
118  vol.Required("label_id"): str,
119  vol.Optional("color"): vol.Any(
120  cv.color_hex, vol.In(SUPPORTED_LABEL_THEME_COLORS), None
121  ),
122  vol.Optional("description"): vol.Any(str, None),
123  vol.Optional("icon"): vol.Any(cv.icon, None),
124  vol.Optional("name"): str,
125  }
126 )
127 @websocket_api.require_admin
128 @callback
130  hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
131 ) -> None:
132  """Handle update label websocket command."""
133  registry = lr.async_get(hass)
134 
135  data = dict(msg)
136  data.pop("type")
137  data.pop("id")
138 
139  try:
140  entry = registry.async_update(**data)
141  except ValueError as err:
142  connection.send_error(msg["id"], "invalid_info", str(err))
143  else:
144  connection.send_result(msg["id"], _entry_dict(entry))
145 
146 
147 @callback
148 def _entry_dict(entry: LabelEntry) -> dict[str, Any]:
149  """Convert entry to API format."""
150  return {
151  "color": entry.color,
152  "created_at": entry.created_at.timestamp(),
153  "description": entry.description,
154  "icon": entry.icon,
155  "label_id": entry.label_id,
156  "name": entry.name,
157  "modified_at": entry.modified_at.timestamp(),
158  }
159 
None websocket_update_label(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
dict[str, Any] _entry_dict(LabelEntry entry)
None websocket_delete_label(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None websocket_list_labels(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
None websocket_create_label(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)