Catch client connection error in Honeywell (#117502)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
mkmer 2024-05-24 04:40:30 -04:00 committed by GitHub
parent bb0b01e4a9
commit 5c263b039e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 8 deletions

View File

@ -2,6 +2,7 @@
from dataclasses import dataclass
from aiohttp.client_exceptions import ClientConnectionError
import aiosomecomfort
from homeassistant.config_entries import ConfigEntry
@ -68,6 +69,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
aiosomecomfort.device.ConnectionError,
aiosomecomfort.device.ConnectionTimeout,
aiosomecomfort.device.SomeComfortError,
ClientConnectionError,
TimeoutError,
) as ex:
raise ConfigEntryNotReady(

View File

@ -54,6 +54,7 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
"""Confirm re-authentication with Honeywell."""
errors: dict[str, str] = {}
assert self.entry is not None
if user_input:
try:
await self.is_valid(
@ -63,14 +64,12 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
except aiosomecomfort.AuthError:
errors["base"] = "invalid_auth"
except (
aiosomecomfort.ConnectionError,
aiosomecomfort.ConnectionTimeout,
TimeoutError,
):
errors["base"] = "cannot_connect"
else:
return self.async_update_reload_and_abort(
self.entry,
@ -83,7 +82,8 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_show_form(
step_id="reauth_confirm",
data_schema=self.add_suggested_values_to_schema(
REAUTH_SCHEMA, self.entry.data
REAUTH_SCHEMA,
self.entry.data,
),
errors=errors,
description_placeholders={"name": "Honeywell"},
@ -91,7 +91,7 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Create config entry. Show the setup form to the user."""
errors = {}
errors: dict[str, str] = {}
if user_input is not None:
try:
await self.is_valid(**user_input)
@ -103,7 +103,6 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
TimeoutError,
):
errors["base"] = "cannot_connect"
if not errors:
return self.async_create_entry(
title=DOMAIN,
@ -115,7 +114,9 @@ class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
vol.Required(CONF_PASSWORD): str,
}
return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=errors
step_id="user",
data_schema=vol.Schema(data_schema),
errors=errors,
)
async def is_valid(self, **kwargs) -> bool:

View File

@ -2,6 +2,7 @@
from unittest.mock import MagicMock, create_autospec, patch
from aiohttp.client_exceptions import ClientConnectionError
import aiosomecomfort
import pytest
@ -120,11 +121,23 @@ async def test_login_error(
assert config_entry.state is ConfigEntryState.SETUP_ERROR
@pytest.mark.parametrize(
"the_error",
[
aiosomecomfort.ConnectionError,
aiosomecomfort.device.ConnectionTimeout,
aiosomecomfort.device.SomeComfortError,
ClientConnectionError,
],
)
async def test_connection_error(
hass: HomeAssistant, client: MagicMock, config_entry: MagicMock
hass: HomeAssistant,
client: MagicMock,
config_entry: MagicMock,
the_error: Exception,
) -> None:
"""Test Connection errors from API."""
client.login.side_effect = aiosomecomfort.ConnectionError
client.login.side_effect = the_error
await init_integration(hass, config_entry)
assert config_entry.state is ConfigEntryState.SETUP_RETRY