diff --git a/homeassistant/components/remote_calendar/config_flow.py b/homeassistant/components/remote_calendar/config_flow.py index 1ceeb7a3937..cc9f45e2767 100644 --- a/homeassistant/components/remote_calendar/config_flow.py +++ b/homeassistant/components/remote_calendar/config_flow.py @@ -1,5 +1,6 @@ """Config flow for Remote Calendar integration.""" +from http import HTTPStatus import logging from typing import Any @@ -50,6 +51,13 @@ class RemoteCalendarConfigFlow(ConfigFlow, domain=DOMAIN): client = get_async_client(self.hass) try: res = await client.get(user_input[CONF_URL], follow_redirects=True) + if res.status_code == HTTPStatus.FORBIDDEN: + errors["base"] = "forbidden" + return self.async_show_form( + step_id="user", + data_schema=STEP_USER_DATA_SCHEMA, + errors=errors, + ) res.raise_for_status() except (HTTPError, InvalidURL) as err: errors["base"] = "cannot_connect" diff --git a/homeassistant/components/remote_calendar/strings.json b/homeassistant/components/remote_calendar/strings.json index 1ad62821818..fff2d4abbb3 100644 --- a/homeassistant/components/remote_calendar/strings.json +++ b/homeassistant/components/remote_calendar/strings.json @@ -19,6 +19,7 @@ }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "forbidden": "The server understood the request but refuses to authorize it.", "invalid_ics_file": "[%key:component::local_calendar::config::error::invalid_ics_file%]" } }, diff --git a/tests/components/remote_calendar/test_config_flow.py b/tests/components/remote_calendar/test_config_flow.py index 9eb9cb40134..9aff1594db3 100644 --- a/tests/components/remote_calendar/test_config_flow.py +++ b/tests/components/remote_calendar/test_config_flow.py @@ -165,8 +165,17 @@ async def test_unsupported_inputs( ## and then the exception isn't raised anymore. +@pytest.mark.parametrize( + ("http_status", "error"), + [ + (401, "cannot_connect"), + (403, "forbidden"), + ], +) @respx.mock -async def test_form_http_status_error(hass: HomeAssistant, ics_content: str) -> None: +async def test_form_http_status_error( + hass: HomeAssistant, ics_content: str, http_status: int, error: str +) -> None: """Test we http status.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -174,7 +183,7 @@ async def test_form_http_status_error(hass: HomeAssistant, ics_content: str) -> assert result["type"] is FlowResultType.FORM respx.get(CALENDER_URL).mock( return_value=Response( - status_code=403, + status_code=http_status, ) ) @@ -186,7 +195,7 @@ async def test_form_http_status_error(hass: HomeAssistant, ics_content: str) -> }, ) assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} + assert result2["errors"] == {"base": error} respx.get(CALENDER_URL).mock( return_value=Response( status_code=200,