Retry setup in case of empty response from Pushover api (#80602)

Retry setup in case of empty response
This commit is contained in:
Rami Mosleh 2022-10-21 18:52:02 +03:00 committed by GitHub
parent c70614fd7c
commit 3aa24afad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -35,7 +35,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
pushover_api.validate, entry.data[CONF_USER_KEY]
)
except BadAPIRequestError as err:
except (BadAPIRequestError, ValueError) as err:
if "application token is invalid" in str(err):
raise ConfigEntryAuthFailed(err) from err
raise ConfigEntryNotReady(err) from err

View File

@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
import aiohttp
from pushover_complete import BadAPIRequestError
import pytest
from requests_mock import Mocker
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.components.pushover.const import DOMAIN
@ -19,7 +20,7 @@ from tests.common import MockConfigEntry
from tests.components.repairs import get_repairs
@pytest.fixture(autouse=True)
@pytest.fixture(autouse=False)
def mock_pushover():
"""Mock pushover."""
with patch(
@ -33,6 +34,7 @@ async def test_setup(
hass_ws_client: Callable[
[HomeAssistant], Awaitable[aiohttp.ClientWebSocketResponse]
],
mock_pushover: MagicMock,
) -> None:
"""Test integration failed due to an error."""
assert await async_setup_component(
@ -56,7 +58,9 @@ async def test_setup(
assert issues[0]["issue_id"] == "deprecated_yaml"
async def test_async_setup_entry_success(hass: HomeAssistant) -> None:
async def test_async_setup_entry_success(
hass: HomeAssistant, mock_pushover: MagicMock
) -> None:
"""Test pushover successful setup."""
entry = MockConfigEntry(
domain=DOMAIN,
@ -68,7 +72,7 @@ async def test_async_setup_entry_success(hass: HomeAssistant) -> None:
assert entry.state == ConfigEntryState.LOADED
async def test_unique_id_updated(hass: HomeAssistant) -> None:
async def test_unique_id_updated(hass: HomeAssistant, mock_pushover: MagicMock) -> None:
"""Test updating unique_id to new format."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, unique_id="MYUSERKEY")
entry.add_to_hass(hass)
@ -106,3 +110,20 @@ async def test_async_setup_entry_failed_conn_error(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.SETUP_RETRY
async def test_async_setup_entry_failed_json_error(
hass: HomeAssistant, requests_mock: Mocker
) -> None:
"""Test pushover failed setup due to bad json response from library."""
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
)
entry.add_to_hass(hass)
requests_mock.post(
"https://api.pushover.net/1/users/validate.json", status_code=204
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.SETUP_RETRY