From 369930e81744368359671493059239edb7600d93 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Fri, 31 Jul 2026 08:39:38 +0200 Subject: [PATCH] Add slugify websocket command (#177623) --- homeassistant/components/websocket_api/commands.py | 13 +++++++++++++ tests/components/websocket_api/test_commands.py | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 5bc658490e65..fe87a4e9a6bc 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -86,6 +86,7 @@ from homeassistant.setup import ( async_get_setup_timings, async_wait_component, ) +from homeassistant.util import slugify from homeassistant.util.json import format_unserializable_data from . import const, decorators, messages @@ -126,6 +127,7 @@ def async_register_commands( async_reg(hass, handle_manifest_list) async_reg(hass, handle_ping) async_reg(hass, handle_render_template) + async_reg(hass, handle_slugify) async_reg(hass, handle_subscribe_bootstrap_integrations) async_reg(hass, handle_subscribe_condition) async_reg(hass, handle_subscribe_condition_platforms) @@ -726,6 +728,17 @@ def handle_ping( connection.send_message(pong_message(msg["id"])) +@callback +@decorators.websocket_command( + {vol.Required("type"): "slugify", vol.Required("text"): str} +) +def handle_slugify( + hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] +) -> None: + """Handle slugify command.""" + connection.send_result(msg["id"], {"slug": slugify(msg["text"])}) + + @lru_cache def _cached_template(template_str: str, hass: HomeAssistant) -> template.Template: """Return a cached template.""" diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index f87f7634c515..5aeb3f78bcd1 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -1247,6 +1247,17 @@ async def test_ping(websocket_client: MockHAClientWebSocket) -> None: assert msg["type"] == "pong" +async def test_slugify(websocket_client: MockHAClientWebSocket) -> None: + """Test slugify command.""" + await websocket_client.send_json_auto_id( + {"type": "slugify", "text": "Living room Thermostat Temperature"} + ) + + msg = await websocket_client.receive_json() + assert msg["success"] is True + assert msg["result"] == {"slug": "living_room_thermostat_temperature"} + + async def test_call_service_context_with_user( hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator,