Add slugify websocket command (#177623)

This commit is contained in:
Paul Bottein
2026-07-31 08:39:38 +02:00
committed by GitHub
parent 257b9ac03f
commit 369930e817
2 changed files with 24 additions and 0 deletions
@@ -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."""
@@ -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,