diff --git a/homeassistant/components/config/label_registry.py b/homeassistant/components/config/label_registry.py index d1bb74393af..1d5d526016d 100644 --- a/homeassistant/components/config/label_registry.py +++ b/homeassistant/components/config/label_registry.py @@ -10,6 +10,35 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.label_registry import LabelEntry, async_get +SUPPORTED_LABEL_THEME_COLORS = { + "primary", + "accent", + "disabled", + "red", + "pink", + "purple", + "deep-purple", + "indigo", + "blue", + "light-blue", + "cyan", + "teal", + "green", + "light-green", + "lime", + "yellow", + "amber", + "orange", + "deep-orange", + "brown", + "light-grey", + "grey", + "dark-grey", + "blue-grey", + "black", + "white", +} + @callback def async_setup(hass: HomeAssistant) -> bool: @@ -42,7 +71,9 @@ def websocket_list_labels( { vol.Required("type"): "config/label_registry/create", vol.Required("name"): str, - vol.Optional("color"): vol.Any(cv.color_hex, None), + vol.Optional("color"): vol.Any( + cv.color_hex, vol.In(SUPPORTED_LABEL_THEME_COLORS), None + ), vol.Optional("description"): vol.Any(str, None), vol.Optional("icon"): vol.Any(cv.icon, None), } @@ -93,7 +124,9 @@ def websocket_delete_label( { vol.Required("type"): "config/label_registry/update", vol.Required("label_id"): str, - vol.Optional("color"): vol.Any(cv.color_hex, None), + vol.Optional("color"): vol.Any( + cv.color_hex, vol.In(SUPPORTED_LABEL_THEME_COLORS), None + ), vol.Optional("description"): vol.Any(str, None), vol.Optional("icon"): vol.Any(cv.icon, None), vol.Optional("name"): str, diff --git a/tests/components/config/test_label_registry.py b/tests/components/config/test_label_registry.py index aa082a3de38..040b3bfe28a 100644 --- a/tests/components/config/test_label_registry.py +++ b/tests/components/config/test_label_registry.py @@ -99,6 +99,27 @@ async def test_create_label( "name": "MOCKERY", } + await client.send_json_auto_id( + { + "name": "MAGIC", + "type": "config/label_registry/create", + "color": "indigo", + "description": "This is the third label", + "icon": "mdi:three", + } + ) + + msg = await client.receive_json() + + assert len(label_registry.labels) == 3 + assert msg["result"] == { + "color": "indigo", + "description": "This is the third label", + "icon": "mdi:three", + "label_id": "magic", + "name": "MAGIC", + } + async def test_create_label_with_name_already_in_use( client: MockHAClientWebSocket, @@ -210,6 +231,28 @@ async def test_update_label( "name": "UPDATED AGAIN", } + await client.send_json_auto_id( + { + "label_id": label.label_id, + "name": "UPDATED YET AGAIN", + "icon": None, + "color": "primary", + "description": None, + "type": "config/label_registry/update", + } + ) + + msg = await client.receive_json() + + assert len(label_registry.labels) == 1 + assert msg["result"] == { + "color": "primary", + "description": None, + "icon": None, + "label_id": "mock", + "name": "UPDATED YET AGAIN", + } + async def test_update_with_name_already_in_use( client: MockHAClientWebSocket,