-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add label support #69996
Closed
Closed
Add label support #69996
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
"core", | ||
"device_registry", | ||
"entity_registry", | ||
"label_registry", | ||
"script", | ||
"scene", | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Websocket API to interact with the label registry.""" | ||
from typing import Any | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components import websocket_api | ||
from homeassistant.components.websocket_api.connection import ActiveConnection | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.label_registry import LabelEntry, async_get | ||
|
||
|
||
async def async_setup(hass: HomeAssistant) -> bool: | ||
"""Register the Label Registry WS commands.""" | ||
websocket_api.async_register_command(hass, websocket_list_labels) | ||
websocket_api.async_register_command(hass, websocket_create_label) | ||
websocket_api.async_register_command(hass, websocket_delete_label) | ||
websocket_api.async_register_command(hass, websocket_update_label) | ||
return True | ||
|
||
|
||
@websocket_api.websocket_command( | ||
{ | ||
vol.Required("type"): "config/label_registry/list", | ||
} | ||
) | ||
@callback | ||
def websocket_list_labels( | ||
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
) -> None: | ||
"""Handle list labels command.""" | ||
registry = async_get(hass) | ||
connection.send_result( | ||
msg["id"], | ||
[_entry_dict(entry) for entry in registry.async_list_labels()], | ||
) | ||
|
||
|
||
@websocket_api.websocket_command( | ||
{ | ||
vol.Required("type"): "config/label_registry/create", | ||
vol.Required("name"): str, | ||
vol.Optional("color"): vol.Any(str, None), | ||
vol.Optional("description"): vol.Any(str, None), | ||
vol.Optional("icon"): vol.Any(str, None), | ||
} | ||
) | ||
@websocket_api.require_admin | ||
@callback | ||
def websocket_create_label( | ||
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
) -> None: | ||
"""Create label command.""" | ||
registry = async_get(hass) | ||
|
||
data = dict(msg) | ||
data.pop("type") | ||
data.pop("id") | ||
|
||
try: | ||
entry = registry.async_create(**data) | ||
except ValueError as err: | ||
connection.send_error(msg["id"], "invalid_info", str(err)) | ||
else: | ||
connection.send_result(msg["id"], _entry_dict(entry)) | ||
|
||
|
||
@websocket_api.websocket_command( | ||
{ | ||
vol.Required("type"): "config/label_registry/delete", | ||
vol.Required("label_id"): str, | ||
} | ||
) | ||
@websocket_api.require_admin | ||
@callback | ||
def websocket_delete_label( | ||
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
) -> None: | ||
"""Delete label command.""" | ||
registry = async_get(hass) | ||
|
||
try: | ||
registry.async_delete(msg["label_id"]) | ||
except KeyError: | ||
connection.send_error(msg["id"], "invalid_info", "Label ID doesn't exist") | ||
else: | ||
connection.send_message(websocket_api.result_message(msg["id"], "success")) | ||
|
||
|
||
@websocket_api.websocket_command( | ||
{ | ||
vol.Required("type"): "config/label_registry/update", | ||
vol.Required("label_id"): str, | ||
vol.Optional("color"): vol.Any(str, None), | ||
vol.Optional("description"): vol.Any(str, None), | ||
vol.Optional("icon"): vol.Any(str, None), | ||
vol.Optional("name"): str, | ||
} | ||
) | ||
@websocket_api.require_admin | ||
@callback | ||
def websocket_update_label( | ||
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
) -> None: | ||
"""Handle update label websocket command.""" | ||
registry = async_get(hass) | ||
|
||
data = dict(msg) | ||
data.pop("type") | ||
data.pop("id") | ||
|
||
try: | ||
entry = registry.async_update(**data) | ||
except ValueError as err: | ||
connection.send_error(msg["id"], "invalid_info", str(err)) | ||
else: | ||
connection.send_result(msg["id"], _entry_dict(entry)) | ||
|
||
|
||
@callback | ||
def _entry_dict(entry: LabelEntry) -> dict[str, Any]: | ||
"""Convert entry to API format.""" | ||
return { | ||
"color": entry.color, | ||
"description": entry.description, | ||
"icon": entry.icon, | ||
"label_id": entry.label_id, | ||
"name": entry.name, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this will be awesome! 🚀 🎉