diff --git a/tests/components/doorbird/__init__.py b/tests/components/doorbird/__init__.py index e71eacc1a42..dc0f041ba31 100644 --- a/tests/components/doorbird/__init__.py +++ b/tests/components/doorbird/__init__.py @@ -16,9 +16,23 @@ VALID_CONFIG = { } +def _get_aiohttp_client_error(status: int) -> aiohttp.ClientResponseError: + """Return a mock aiohttp client response error.""" + return aiohttp.ClientResponseError( + request_info=Mock(), + history=Mock(), + status=status, + ) + + def mock_unauthorized_exception() -> aiohttp.ClientResponseError: """Return a mock unauthorized exception.""" - return aiohttp.ClientResponseError(request_info=Mock(), history=Mock(), status=401) + return _get_aiohttp_client_error(401) + + +def mock_not_found_exception() -> aiohttp.ClientResponseError: + """Return a mock not found exception.""" + return _get_aiohttp_client_error(404) def get_mock_doorbird_api( diff --git a/tests/components/doorbird/test_config_flow.py b/tests/components/doorbird/test_config_flow.py index 02b1209af98..3abdd2b87a3 100644 --- a/tests/components/doorbird/test_config_flow.py +++ b/tests/components/doorbird/test_config_flow.py @@ -19,7 +19,12 @@ from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNA from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import VALID_CONFIG, get_mock_doorbird_api, mock_unauthorized_exception +from . import ( + VALID_CONFIG, + get_mock_doorbird_api, + mock_not_found_exception, + mock_unauthorized_exception, +) from tests.common import MockConfigEntry @@ -281,6 +286,100 @@ async def test_form_user_invalid_auth(hass: HomeAssistant) -> None: assert result2["errors"] == {"base": "invalid_auth"} +async def test_form_user_doorbird_not_found( + doorbird_api: DoorBird, hass: HomeAssistant +) -> None: + """Test handling unable to connect to the device.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + mock_error = mock_not_found_exception() + doorbirdapi = get_mock_doorbird_api(info_side_effect=mock_error) + with patch( + "homeassistant.components.doorbird.config_flow.DoorBird", + return_value=doorbirdapi, + ): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + VALID_CONFIG, + ) + + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "cannot_connect"} + + with ( + patch( + "homeassistant.components.doorbird.async_setup", return_value=True + ) as mock_setup, + patch( + "homeassistant.components.doorbird.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + ): + result3 = await hass.config_entries.flow.async_configure( + result2["flow_id"], VALID_CONFIG + ) + await hass.async_block_till_done() + + assert result3["type"] is FlowResultType.CREATE_ENTRY + assert result3["title"] == "1.2.3.4" + assert result3["data"] == { + "host": "1.2.3.4", + "name": "mydoorbird", + "password": "password", + "username": "friend", + } + assert len(mock_setup.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_form_user_doorbird_unknown_exception( + doorbird_api: DoorBird, hass: HomeAssistant +) -> None: + """Test handling unable an unknown exception.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + doorbirdapi = get_mock_doorbird_api(info_side_effect=ValueError) + with patch( + "homeassistant.components.doorbird.config_flow.DoorBird", + return_value=doorbirdapi, + ): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + VALID_CONFIG, + ) + + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "unknown"} + + with ( + patch( + "homeassistant.components.doorbird.async_setup", return_value=True + ) as mock_setup, + patch( + "homeassistant.components.doorbird.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + ): + result3 = await hass.config_entries.flow.async_configure( + result2["flow_id"], VALID_CONFIG + ) + await hass.async_block_till_done() + + assert result3["type"] is FlowResultType.CREATE_ENTRY + assert result3["title"] == "1.2.3.4" + assert result3["data"] == { + "host": "1.2.3.4", + "name": "mydoorbird", + "password": "password", + "username": "friend", + } + assert len(mock_setup.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_options_flow(hass: HomeAssistant) -> None: """Test config flow options."""