From 658299ee21c11b51bafa83dcec0d1171b5ba0743 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 13 Apr 2025 17:42:42 -0700 Subject: [PATCH] Strip whitespace from new todo list item names (#142889) Strip whitspace from new todo list item names --- homeassistant/components/todo/__init__.py | 8 ++++++-- tests/components/todo/test_init.py | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/todo/__init__.py b/homeassistant/components/todo/__init__.py index c1c921343b8..b8c90f917d4 100644 --- a/homeassistant/components/todo/__init__.py +++ b/homeassistant/components/todo/__init__.py @@ -129,7 +129,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: vol.All( cv.make_entity_service_schema( { - vol.Required(ATTR_ITEM): vol.All(cv.string, vol.Length(min=1)), + vol.Required(ATTR_ITEM): vol.All( + cv.string, str.strip, vol.Length(min=1) + ), **TODO_ITEM_FIELD_SCHEMA, } ), @@ -144,7 +146,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: cv.make_entity_service_schema( { vol.Required(ATTR_ITEM): vol.All(cv.string, vol.Length(min=1)), - vol.Optional(ATTR_RENAME): vol.All(cv.string, vol.Length(min=1)), + vol.Optional(ATTR_RENAME): vol.All( + cv.string, str.strip, vol.Length(min=1) + ), vol.Optional(ATTR_STATUS): vol.In( {TodoItemStatus.NEEDS_ACTION, TodoItemStatus.COMPLETED}, ), diff --git a/tests/components/todo/test_init.py b/tests/components/todo/test_init.py index 11ef3d6f044..adada97a9e4 100644 --- a/tests/components/todo/test_init.py +++ b/tests/components/todo/test_init.py @@ -160,9 +160,18 @@ async def test_unsupported_websocket( assert resp.get("error", {}).get("code") == "not_found" +@pytest.mark.parametrize( + ("new_item_name"), + [ + ("New item"), + ("New item "), + (" New item"), + ], +) async def test_add_item_service( hass: HomeAssistant, test_entity: TodoListEntity, + new_item_name: str, ) -> None: """Test adding an item in a To-do list.""" @@ -171,7 +180,7 @@ async def test_add_item_service( await hass.services.async_call( DOMAIN, TodoServices.ADD_ITEM, - {ATTR_ITEM: "New item"}, + {ATTR_ITEM: new_item_name}, target={ATTR_ENTITY_ID: "todo.entity1"}, blocking=True, ) @@ -209,6 +218,7 @@ async def test_add_item_service_raises( [ ({}, vol.Invalid, "required key not provided"), ({ATTR_ITEM: ""}, vol.Invalid, "length of value must be at least 1"), + ({ATTR_ITEM: " "}, vol.Invalid, "length of value must be at least 1"), ( {ATTR_ITEM: "Submit forms", ATTR_DESCRIPTION: "Submit tax forms"}, ServiceValidationError, @@ -331,9 +341,18 @@ async def test_add_item_service_extended_fields( assert item == expected_item +@pytest.mark.parametrize( + ("new_item_name"), + [ + ("Updated item"), + ("Updated item "), + (" Updated item "), + ], +) async def test_update_todo_item_service_by_id( hass: HomeAssistant, test_entity: TodoListEntity, + new_item_name: str, ) -> None: """Test updating an item in a To-do list.""" @@ -342,7 +361,7 @@ async def test_update_todo_item_service_by_id( await hass.services.async_call( DOMAIN, TodoServices.UPDATE_ITEM, - {ATTR_ITEM: "1", ATTR_RENAME: "Updated item", ATTR_STATUS: "completed"}, + {ATTR_ITEM: "1", ATTR_RENAME: new_item_name, ATTR_STATUS: "completed"}, target={ATTR_ENTITY_ID: "todo.entity1"}, blocking=True, )