Improve AirNow Configuration Error Handling (#97267)

* Fix config flow error handling when no data is returned by AirNow API

* Add test for PyAirNow EmptyResponseError

* Typo Fix
pull/97293/head
J.P. Krauss 2023-07-26 12:30:25 -07:00 committed by GitHub
parent fd3c2c2811
commit 7113db8da4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -2,7 +2,7 @@
import logging
from pyairnow import WebServiceAPI
from pyairnow.errors import AirNowError, InvalidKeyError
from pyairnow.errors import AirNowError, EmptyResponseError, InvalidKeyError
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
@ -35,6 +35,8 @@ async def validate_input(hass: core.HomeAssistant, data):
raise InvalidAuth from exc
except AirNowError as exc:
raise CannotConnect from exc
except EmptyResponseError as exc:
raise InvalidLocation from exc
if not test_data:
raise InvalidLocation

View File

@ -14,7 +14,7 @@
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_location": "No results found for that location",
"invalid_location": "No results found for that location, try changing the location or station radius.",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {

View File

@ -1,7 +1,7 @@
"""Test the AirNow config flow."""
from unittest.mock import AsyncMock
from pyairnow.errors import AirNowError, InvalidKeyError
from pyairnow.errors import AirNowError, EmptyResponseError, InvalidKeyError
import pytest
from homeassistant import config_entries, data_entry_flow
@ -55,6 +55,17 @@ async def test_form_cannot_connect(hass: HomeAssistant, config, setup_airnow) ->
assert result2["errors"] == {"base": "cannot_connect"}
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=EmptyResponseError)])
async def test_form_empty_result(hass: HomeAssistant, config, setup_airnow) -> None:
"""Test we handle empty response error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_location"}
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=RuntimeError)])
async def test_form_unexpected(hass: HomeAssistant, config, setup_airnow) -> None:
"""Test we handle an unexpected error."""