Repository: home-assistant/core
File: homeassistant/components/todo/intent.py (Python)

Code before:
        if not match_result.is_match:
            raise intent.MatchFailedError(
                result=match_result, constraints=match_constraints
            )

        target_list = hass.data[DATA_COMPONENT].get_entity(
            match_result.states[0].entity_id
        )
        if target_list is None:
            raise intent.IntentHandleError(
                f"No to-do list: {list_name}", "list_not_found"
            )

        # Find items in list
        matching_item_uids = [
            todo_item.uid
            for todo_item in target_list.todo_items or ()
            if item in (todo_item.uid, todo_item.summary) and todo_item.uid is not None
        ]
        if not matching_item_uids:
            raise intent.IntentHandleError(
                f"Item '{item}' not found on list", "item_not_found"
            )

        # Remove items
        await target_list.async_delete_todo_items(matching_item_uids)

        response: intent.IntentResponse = intent_obj.create_response()
        response.async_set_results(
            [
                intent.IntentResponseTarget(
                    type=intent.IntentResponseTargetType.ENTITY,
                    name=list_name,
                    id=match_result.states[0].entity_id,
                )
            ]
        )
        return response


class ListCompleteItemIntent(intent.IntentHandler):
    """Handle ListCompleteItem intents."""

    intent_type = INTENT_LIST_COMPLETE_ITEM
    description = "Complete item on a todo list"
    slot_schema = {
        vol.Required("item"): intent.non_empty_string,
        vol.Required("name"): intent.non_empty_string,
    }
    platforms = {DOMAIN}


Review comment (refactor):
This is the only part that is unique to this intent handler. Extract it to a separate method and make a common class for all the intent handlers in this module.

Code after:
        matching_item = None
        for todo_item in target_list.todo_items or ():
            if (
                item in (todo_item.uid, todo_item.summary)
                and todo_item.status == TodoItemStatus.NEEDS_ACTION
            ):
                matching_item = todo_item
                break
        if not matching_item or not matching_item.uid:
            raise intent.IntentHandleError(
                f"Item '{item}' not found on list", "item_not_found"
            )

        # Mark as completed
        await target_list.async_update_todo_item(
            TodoItem(
                uid=matching_item.uid,
                summary=matching_item.summary,
                status=TodoItemStatus.COMPLETED,
            )
        )


class ListRemoveItemIntentHandler(ListBaseIntentHandler):
    """Handle LisRemoveItemIntent intents."""

    intent_type = INTENT_LIST_REMOVE_ITEM
    description = "Remove one or more items from a todo list"

    async def _async_do_handle(self, target_list: TodoListEntity, item: str) -> None:
        """Execute action specific to this intent handler."""

        # Find item in list
        matching_item = None
        for todo_item in target_list.todo_items or ():
            if item in (todo_item.uid, todo_item.summary):
                matching_item = todo_item
                break
        if not matching_item or not matching_item.uid:
            raise intent.IntentHandleError(
                f"Item '{item}' not found on list", "item_not_found"
            )

        # Remove items
        await target_list.async_delete_todo_items(uids=[matching_item.uid])