Add google calendar service proper exception handling (#74686)
This commit is contained in:
parent
0e3f7bc63a
commit
561c9a77d8
4 changed files with 63 additions and 21 deletions
|
@ -30,7 +30,11 @@ from homeassistant.const import (
|
||||||
CONF_OFFSET,
|
CONF_OFFSET,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import (
|
||||||
|
ConfigEntryAuthFailed,
|
||||||
|
ConfigEntryNotReady,
|
||||||
|
HomeAssistantError,
|
||||||
|
)
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow
|
from homeassistant.helpers import config_entry_oauth2_flow
|
||||||
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
|
||||||
|
@ -348,15 +352,18 @@ async def async_setup_add_event_service(
|
||||||
"Missing required fields to set start or end date/datetime"
|
"Missing required fields to set start or end date/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
await calendar_service.async_create_event(
|
try:
|
||||||
call.data[EVENT_CALENDAR_ID],
|
await calendar_service.async_create_event(
|
||||||
Event(
|
call.data[EVENT_CALENDAR_ID],
|
||||||
summary=call.data[EVENT_SUMMARY],
|
Event(
|
||||||
description=call.data[EVENT_DESCRIPTION],
|
summary=call.data[EVENT_SUMMARY],
|
||||||
start=start,
|
description=call.data[EVENT_DESCRIPTION],
|
||||||
end=end,
|
start=start,
|
||||||
),
|
end=end,
|
||||||
)
|
),
|
||||||
|
)
|
||||||
|
except ApiException as err:
|
||||||
|
raise HomeAssistantError(str(err)) from err
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_ADD_EVENT, _add_event, schema=ADD_EVENT_SERVICE_SCHEMA
|
DOMAIN, SERVICE_ADD_EVENT, _add_event, schema=ADD_EVENT_SERVICE_SCHEMA
|
||||||
|
|
|
@ -22,7 +22,7 @@ from homeassistant.components.calendar import (
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_DEVICE_ID, CONF_ENTITIES, CONF_NAME, CONF_OFFSET
|
from homeassistant.const import CONF_DEVICE_ID, CONF_ENTITIES, CONF_NAME, CONF_OFFSET
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
config_validation as cv,
|
config_validation as cv,
|
||||||
entity_platform,
|
entity_platform,
|
||||||
|
@ -362,12 +362,15 @@ async def async_create_event(entity: GoogleCalendarEntity, call: ServiceCall) ->
|
||||||
if start is None or end is None:
|
if start is None or end is None:
|
||||||
raise ValueError("Missing required fields to set start or end date/datetime")
|
raise ValueError("Missing required fields to set start or end date/datetime")
|
||||||
|
|
||||||
await entity.calendar_service.async_create_event(
|
try:
|
||||||
entity.calendar_id,
|
await entity.calendar_service.async_create_event(
|
||||||
Event(
|
entity.calendar_id,
|
||||||
summary=call.data[EVENT_SUMMARY],
|
Event(
|
||||||
description=call.data[EVENT_DESCRIPTION],
|
summary=call.data[EVENT_SUMMARY],
|
||||||
start=start,
|
description=call.data[EVENT_DESCRIPTION],
|
||||||
end=end,
|
start=start,
|
||||||
),
|
end=end,
|
||||||
)
|
),
|
||||||
|
)
|
||||||
|
except ApiException as err:
|
||||||
|
raise HomeAssistantError(str(err)) from err
|
||||||
|
|
|
@ -306,9 +306,12 @@ def mock_insert_event(
|
||||||
) -> Callable[[...], None]:
|
) -> Callable[[...], None]:
|
||||||
"""Fixture for capturing event creation."""
|
"""Fixture for capturing event creation."""
|
||||||
|
|
||||||
def _expect_result(calendar_id: str = CALENDAR_ID) -> None:
|
def _expect_result(
|
||||||
|
calendar_id: str = CALENDAR_ID, exc: ClientError | None = None
|
||||||
|
) -> None:
|
||||||
aioclient_mock.post(
|
aioclient_mock.post(
|
||||||
f"{API_BASE_URL}/calendars/{calendar_id}/events",
|
f"{API_BASE_URL}/calendars/{calendar_id}/events",
|
||||||
|
exc=exc,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
from aiohttp.client_exceptions import ClientError
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ from homeassistant.components.google.const import CONF_CALENDAR_ACCESS
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import STATE_OFF
|
from homeassistant.const import STATE_OFF
|
||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
|
@ -676,6 +678,33 @@ async def test_add_event_date_time(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_add_event_failure(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
component_setup: ComponentSetup,
|
||||||
|
mock_calendars_list: ApiResult,
|
||||||
|
test_api_calendar: dict[str, Any],
|
||||||
|
mock_events_list: ApiResult,
|
||||||
|
mock_insert_event: Callable[[..., dict[str, Any]], None],
|
||||||
|
setup_config_entry: MockConfigEntry,
|
||||||
|
add_event_call_service: Callable[dict[str, Any], Awaitable[None]],
|
||||||
|
) -> None:
|
||||||
|
"""Test service calls with incorrect fields."""
|
||||||
|
|
||||||
|
mock_calendars_list({"items": [test_api_calendar]})
|
||||||
|
mock_events_list({})
|
||||||
|
assert await component_setup()
|
||||||
|
|
||||||
|
mock_insert_event(
|
||||||
|
calendar_id=CALENDAR_ID,
|
||||||
|
exc=ClientError(),
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(HomeAssistantError):
|
||||||
|
await add_event_call_service(
|
||||||
|
{"start_date": "2022-05-01", "end_date": "2022-05-01"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"config_entry_token_expiry", [datetime.datetime.max.timestamp() + 1]
|
"config_entry_token_expiry", [datetime.datetime.max.timestamp() + 1]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue