Catch ApiError while checking credentials in NAM integration (#81243)

* Catch ApiError while checking credentials

* Update tests

* Suggested change
This commit is contained in:
Maciej Bieniek 2022-10-30 20:01:10 +01:00 committed by GitHub
parent a1eec7b55d
commit ec038835f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -56,6 +56,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try:
await nam.async_check_credentials()
except ApiError as err:
raise ConfigEntryNotReady from err
except AuthFailed as err:
raise ConfigEntryAuthFailed from err

View file

@ -32,12 +32,30 @@ async def test_config_not_ready(hass):
unique_id="aa:bb:cc:dd:ee:ff",
data={"host": "10.10.2.3"},
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
side_effect=ApiError("API Error"),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_config_not_ready_while_checking_credentials(hass):
"""Test for setup failure if the connection fails while checking credentials."""
entry = MockConfigEntry(
domain=DOMAIN,
title="10.10.2.3",
unique_id="aa:bb:cc:dd:ee:ff",
data={"host": "10.10.2.3"},
)
entry.add_to_hass(hass)
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=ApiError("API Error"),
):
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
@ -50,12 +68,12 @@ async def test_config_auth_failed(hass):
unique_id="aa:bb:cc:dd:ee:ff",
data={"host": "10.10.2.3"},
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=AuthFailed("Authorization has failed"),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_ERROR