Add support for general API exception in Sense integration (#68517)

This commit is contained in:
Keilin Bickar 2022-03-22 18:14:01 -04:00 committed by GitHub
parent b9526b05ee
commit c5a3ba4065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 10 deletions

View file

@ -2,7 +2,7 @@
"domain": "emulated_kasa",
"name": "Emulated Kasa",
"documentation": "https://www.home-assistant.io/integrations/emulated_kasa",
"requirements": ["sense_energy==0.10.2"],
"requirements": ["sense_energy==0.10.3"],
"codeowners": ["@kbickar"],
"quality_scale": "internal",
"iot_class": "local_push",

View file

@ -12,7 +12,7 @@ from homeassistant import config_entries
from homeassistant.const import CONF_CODE, CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import ACTIVE_UPDATE_RATE, DEFAULT_TIMEOUT, DOMAIN, SENSE_TIMEOUT_EXCEPTIONS
from .const import ACTIVE_UPDATE_RATE, DEFAULT_TIMEOUT, DOMAIN, SENSE_CONNECT_EXCEPTIONS
_LOGGER = logging.getLogger(__name__)
@ -76,7 +76,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
await self.validate_input(user_input)
except SenseMFARequiredException:
return await self.async_step_validation()
except SENSE_TIMEOUT_EXCEPTIONS:
except SENSE_CONNECT_EXCEPTIONS:
errors["base"] = "cannot_connect"
except SenseAuthenticationException:
errors["base"] = "invalid_auth"
@ -93,7 +93,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
if user_input:
try:
await self._gateway.validate_mfa(user_input[CONF_CODE])
except SENSE_TIMEOUT_EXCEPTIONS:
except SENSE_CONNECT_EXCEPTIONS:
errors["base"] = "cannot_connect"
except SenseAuthenticationException:
errors["base"] = "invalid_auth"

View file

@ -3,8 +3,11 @@
import asyncio
import socket
from sense_energy import SenseAPITimeoutException
from sense_energy.sense_exceptions import SenseWebsocketException
from sense_energy import (
SenseAPIException,
SenseAPITimeoutException,
SenseWebsocketException,
)
DOMAIN = "sense"
DEFAULT_TIMEOUT = 10
@ -40,6 +43,11 @@ ICON = "mdi:flash"
SENSE_TIMEOUT_EXCEPTIONS = (asyncio.TimeoutError, SenseAPITimeoutException)
SENSE_EXCEPTIONS = (socket.gaierror, SenseWebsocketException)
SENSE_CONNECT_EXCEPTIONS = (
asyncio.TimeoutError,
SenseAPITimeoutException,
SenseAPIException,
)
MDI_ICONS = {
"ac": "air-conditioner",

View file

@ -2,7 +2,7 @@
"domain": "sense",
"name": "Sense",
"documentation": "https://www.home-assistant.io/integrations/sense",
"requirements": ["sense_energy==0.10.2"],
"requirements": ["sense_energy==0.10.3"],
"codeowners": ["@kbickar"],
"config_flow": true,
"dhcp": [

View file

@ -2118,7 +2118,7 @@ sendgrid==6.8.2
# homeassistant.components.emulated_kasa
# homeassistant.components.sense
sense_energy==0.10.2
sense_energy==0.10.3
# homeassistant.components.sentry
sentry-sdk==1.5.8

View file

@ -1352,7 +1352,7 @@ securetar==2022.2.0
# homeassistant.components.emulated_kasa
# homeassistant.components.sense
sense_energy==0.10.2
sense_energy==0.10.3
# homeassistant.components.sentry
sentry-sdk==1.5.8

View file

@ -3,6 +3,7 @@ from unittest.mock import AsyncMock, patch
import pytest
from sense_energy import (
SenseAPIException,
SenseAPITimeoutException,
SenseAuthenticationException,
SenseMFARequiredException,
@ -189,7 +190,7 @@ async def test_form_mfa_required_exception(hass, mock_sense):
assert result3["errors"] == {"base": "unknown"}
async def test_form_cannot_connect(hass):
async def test_form_timeout(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -208,6 +209,25 @@ async def test_form_cannot_connect(hass):
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"sense_energy.ASyncSenseable.authenticate",
side_effect=SenseAPIException,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"timeout": "6", "email": "test-email", "password": "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_unknown_exception(hass):
"""Test we handle unknown error."""
result = await hass.config_entries.flow.async_init(