1 """Websocket API to interact with the label registry."""
5 import voluptuous
as vol
13 SUPPORTED_LABEL_THEME_COLORS = {
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)
53 @websocket_api.websocket_command(
{
vol.Required("type"):
"config/label_registry/list",
58 hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
60 """Handle list labels command."""
61 registry = lr.async_get(hass)
62 connection.send_result(
64 [
_entry_dict(entry)
for entry
in registry.async_list_labels()],
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
73 vol.Optional(
"description"): vol.Any(str,
None),
74 vol.Optional(
"icon"): vol.Any(cv.icon,
None),
77 @websocket_api.require_admin
80 hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
82 """Create label command."""
83 registry = lr.async_get(hass)
90 entry = registry.async_create(**data)
91 except ValueError
as err:
92 connection.send_error(msg[
"id"],
"invalid_info",
str(err))
94 connection.send_result(msg[
"id"],
_entry_dict(entry))
97 @websocket_api.websocket_command(
{
vol.Required("type"):
"config/label_registry/delete",
98 vol.Required(
"label_id"): str,
101 @websocket_api.require_admin
104 hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
106 """Delete label command."""
107 registry = lr.async_get(hass)
110 registry.async_delete(msg[
"label_id"])
112 connection.send_error(msg[
"id"],
"invalid_info",
"Label ID doesn't exist")
114 connection.send_result(msg[
"id"])
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
122 vol.Optional(
"description"): vol.Any(str,
None),
123 vol.Optional(
"icon"): vol.Any(cv.icon,
None),
124 vol.Optional(
"name"): str,
127 @websocket_api.require_admin
130 hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
132 """Handle update label websocket command."""
133 registry = lr.async_get(hass)
140 entry = registry.async_update(**data)
141 except ValueError
as err:
142 connection.send_error(msg[
"id"],
"invalid_info",
str(err))
144 connection.send_result(msg[
"id"],
_entry_dict(entry))
148 def _entry_dict(entry: LabelEntry) -> dict[str, Any]:
149 """Convert entry to API format."""
151 "color": entry.color,
152 "created_at": entry.created_at.timestamp(),
153 "description": entry.description,
155 "label_id": entry.label_id,
157 "modified_at": entry.modified_at.timestamp(),
159
None websocket_update_label(HomeAssistant hass, ActiveConnection connection, dict[str, Any] msg)
bool async_setup(HomeAssistant hass)
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)