mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Todoist service validation error consistency (#123122)
This commit is contained in:
parent
67e3139dcf
commit
ab811f70b1
@ -21,7 +21,7 @@ from homeassistant.components.calendar import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
|
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
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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
|
async def handle_new_task(call: ServiceCall) -> None: # noqa: C901
|
||||||
"""Call when a user creates a new Todoist Task from Home Assistant."""
|
"""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()
|
projects = await coordinator.async_get_projects()
|
||||||
project_id: str | None = None
|
project_id: str | None = None
|
||||||
for project in projects:
|
for project in projects:
|
||||||
if project_name == project.name.lower():
|
if project_name == project.name.lower():
|
||||||
project_id = project.id
|
project_id = project.id
|
||||||
|
break
|
||||||
if project_id is None:
|
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
|
# Optional section within project
|
||||||
section_id: str | None = None
|
section_id: str | None = None
|
||||||
|
@ -21,6 +21,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
|
"project_invalid": {
|
||||||
|
"message": "Invalid project name \"{project}\""
|
||||||
|
},
|
||||||
"section_invalid": {
|
"section_invalid": {
|
||||||
"message": "Project \"{project}\" has no section \"{section}\""
|
"message": "Project \"{project}\" has no section \"{section}\""
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ from homeassistant.components.todoist.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import CONF_TOKEN, Platform
|
from homeassistant.const import CONF_TOKEN, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.entity_component import async_update_entity
|
from homeassistant.helpers.entity_component import async_update_entity
|
||||||
from homeassistant.util import dt as dt_util
|
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(
|
async def test_create_task_service_call_with_section(
|
||||||
hass: HomeAssistant, api: AsyncMock
|
hass: HomeAssistant, api: AsyncMock
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user