Catch ApiError
while checking credentials in NAM integration (#81243)
* Catch ApiError while checking credentials * Update tests * Suggested change
This commit is contained in:
parent
a1eec7b55d
commit
ec038835f6
2 changed files with 22 additions and 2 deletions
|
@ -56,6 +56,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await nam.async_check_credentials()
|
await nam.async_check_credentials()
|
||||||
|
except ApiError as err:
|
||||||
|
raise ConfigEntryNotReady from err
|
||||||
except AuthFailed as err:
|
except AuthFailed as err:
|
||||||
raise ConfigEntryAuthFailed from err
|
raise ConfigEntryAuthFailed from err
|
||||||
|
|
||||||
|
|
|
@ -32,12 +32,30 @@ async def test_config_not_ready(hass):
|
||||||
unique_id="aa:bb:cc:dd:ee:ff",
|
unique_id="aa:bb:cc:dd:ee:ff",
|
||||||
data={"host": "10.10.2.3"},
|
data={"host": "10.10.2.3"},
|
||||||
)
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.nam.NettigoAirMonitor.initialize",
|
"homeassistant.components.nam.NettigoAirMonitor.initialize",
|
||||||
side_effect=ApiError("API Error"),
|
side_effect=ApiError("API Error"),
|
||||||
):
|
):
|
||||||
|
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)
|
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)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
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",
|
unique_id="aa:bb:cc:dd:ee:ff",
|
||||||
data={"host": "10.10.2.3"},
|
data={"host": "10.10.2.3"},
|
||||||
)
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
|
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
|
||||||
side_effect=AuthFailed("Authorization has failed"),
|
side_effect=AuthFailed("Authorization has failed"),
|
||||||
):
|
):
|
||||||
entry.add_to_hass(hass)
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
assert entry.state is ConfigEntryState.SETUP_ERROR
|
assert entry.state is ConfigEntryState.SETUP_ERROR
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue