Improve evil genius labs error handling (#62365)

This commit is contained in:
Paulus Schoutsen 2021-12-20 00:00:49 -08:00 committed by GitHub
parent 02ad5f3779
commit a5c39e6fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 4 deletions

View File

@ -1,10 +1,12 @@
"""Config flow for Evil Genius Labs integration."""
from __future__ import annotations
import asyncio
import logging
from typing import Any
import aiohttp
import async_timeout
import pyevilgenius
import voluptuous as vol
@ -29,9 +31,11 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
)
try:
data = await hub.get_data()
info = await hub.get_info()
async with async_timeout.timeout(10):
data = await hub.get_data()
info = await hub.get_info()
except aiohttp.ClientError as err:
_LOGGER.debug("Unable to connect: %s", err)
raise CannotConnect from err
return {"title": data["name"]["value"], "unique_id": info["wiFiChipId"]}
@ -60,6 +64,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
info = await validate_input(self.hass, user_input)
except asyncio.TimeoutError:
errors["base"] = "timeout"
except CannotConnect:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except

View File

@ -9,6 +9,7 @@
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"timeout": "[%key:common::config_flow::error::timeout_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
}

View File

@ -56,7 +56,8 @@
"invalid_api_key": "Invalid API key",
"invalid_auth": "Invalid authentication",
"invalid_host": "Invalid hostname or IP address",
"unknown": "Unexpected error"
"unknown": "Unexpected error",
"timeout_connect": "Timeout establishing connection"
},
"abort": {
"single_instance_allowed": "Already configured. Only a single configuration possible.",

View File

@ -1,4 +1,5 @@
"""Test the Evil Genius Labs config flow."""
import asyncio
from unittest.mock import patch
import aiohttp
@ -43,7 +44,7 @@ async def test_form(hass: HomeAssistant, data_fixture, info_fixture) -> None:
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
async def test_form_cannot_connect(hass: HomeAssistant, caplog) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -62,6 +63,28 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_connect"}
assert "Unable to connect" in caplog.text
async def test_form_timeout(hass: HomeAssistant) -> None:
"""Test we handle timeout error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"pyevilgenius.EvilGeniusDevice.get_data",
side_effect=asyncio.TimeoutError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
},
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "timeout"}
async def test_form_unknown(hass: HomeAssistant) -> None: