Add google calendar service proper exception handling (#74686)

pull/74692/head
Allen Porter 2022-07-07 20:50:19 -07:00 committed by GitHub
parent 0e3f7bc63a
commit 561c9a77d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 21 deletions

View File

@ -30,7 +30,11 @@ from homeassistant.const import (
CONF_OFFSET,
)
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.aiohttp_client import async_get_clientsession
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"
)
await calendar_service.async_create_event(
call.data[EVENT_CALENDAR_ID],
Event(
summary=call.data[EVENT_SUMMARY],
description=call.data[EVENT_DESCRIPTION],
start=start,
end=end,
),
)
try:
await calendar_service.async_create_event(
call.data[EVENT_CALENDAR_ID],
Event(
summary=call.data[EVENT_SUMMARY],
description=call.data[EVENT_DESCRIPTION],
start=start,
end=end,
),
)
except ApiException as err:
raise HomeAssistantError(str(err)) from err
hass.services.async_register(
DOMAIN, SERVICE_ADD_EVENT, _add_event, schema=ADD_EVENT_SERVICE_SCHEMA

View File

@ -22,7 +22,7 @@ from homeassistant.components.calendar import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE_ID, CONF_ENTITIES, CONF_NAME, CONF_OFFSET
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import PlatformNotReady
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
from homeassistant.helpers import (
config_validation as cv,
entity_platform,
@ -362,12 +362,15 @@ async def async_create_event(entity: GoogleCalendarEntity, call: ServiceCall) ->
if start is None or end is None:
raise ValueError("Missing required fields to set start or end date/datetime")
await entity.calendar_service.async_create_event(
entity.calendar_id,
Event(
summary=call.data[EVENT_SUMMARY],
description=call.data[EVENT_DESCRIPTION],
start=start,
end=end,
),
)
try:
await entity.calendar_service.async_create_event(
entity.calendar_id,
Event(
summary=call.data[EVENT_SUMMARY],
description=call.data[EVENT_DESCRIPTION],
start=start,
end=end,
),
)
except ApiException as err:
raise HomeAssistantError(str(err)) from err

View File

@ -306,9 +306,12 @@ def mock_insert_event(
) -> Callable[[...], None]:
"""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(
f"{API_BASE_URL}/calendars/{calendar_id}/events",
exc=exc,
)
return

View File

@ -8,6 +8,7 @@ import time
from typing import Any
from unittest.mock import Mock, patch
from aiohttp.client_exceptions import ClientError
import pytest
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.const import STATE_OFF
from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
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(
"config_entry_token_expiry", [datetime.datetime.max.timestamp() + 1]
)