Todoist service validation error consistency (#123122)

This commit is contained in:
Chris Buckley 2024-08-05 10:24:49 +01:00 committed by GitHub
parent 67e3139dcf
commit ab811f70b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View file

@ -21,7 +21,7 @@ from homeassistant.components.calendar import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -215,14 +215,21 @@ def async_register_services( # noqa: C901
async def handle_new_task(call: ServiceCall) -> None: # noqa: C901
"""Call when a user creates a new Todoist Task from Home Assistant."""
project_name = call.data[PROJECT_NAME].lower()
project_name = call.data[PROJECT_NAME]
projects = await coordinator.async_get_projects()
project_id: str | None = None
for project in projects:
if project_name == project.name.lower():
project_id = project.id
break
if project_id is None:
raise HomeAssistantError(f"Invalid project name '{project_name}'")
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="project_invalid",
translation_placeholders={
"project": project_name,
},
)
# Optional section within project
section_id: str | None = None

View file

@ -21,6 +21,9 @@
}
},
"exceptions": {
"project_invalid": {
"message": "Invalid project name \"{project}\""
},
"section_invalid": {
"message": "Project \"{project}\" has no section \"{section}\""
}

View file

@ -23,6 +23,7 @@ from homeassistant.components.todoist.const import (
)
from homeassistant.const import CONF_TOKEN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util import dt as dt_util
@ -270,6 +271,25 @@ async def test_create_task_service_call(hass: HomeAssistant, api: AsyncMock) ->
)
async def test_create_task_service_call_raises(
hass: HomeAssistant, api: AsyncMock
) -> None:
"""Test adding an item to an invalid project raises an error."""
with pytest.raises(ServiceValidationError, match="project_invalid"):
await hass.services.async_call(
DOMAIN,
SERVICE_NEW_TASK,
{
ASSIGNEE: "user",
CONTENT: "task",
LABELS: ["Label1"],
PROJECT_NAME: "Missing Project",
},
blocking=True,
)
async def test_create_task_service_call_with_section(
hass: HomeAssistant, api: AsyncMock
) -> None: