mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add support for general API exception in Sense integration (#68517)
This commit is contained in:
parent
b9526b05ee
commit
c5a3ba4065
@ -2,7 +2,7 @@
|
|||||||
"domain": "emulated_kasa",
|
"domain": "emulated_kasa",
|
||||||
"name": "Emulated Kasa",
|
"name": "Emulated Kasa",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/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"],
|
"codeowners": ["@kbickar"],
|
||||||
"quality_scale": "internal",
|
"quality_scale": "internal",
|
||||||
"iot_class": "local_push",
|
"iot_class": "local_push",
|
||||||
|
@ -12,7 +12,7 @@ from homeassistant import config_entries
|
|||||||
from homeassistant.const import CONF_CODE, CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
from homeassistant.const import CONF_CODE, CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.validate_input(user_input)
|
await self.validate_input(user_input)
|
||||||
except SenseMFARequiredException:
|
except SenseMFARequiredException:
|
||||||
return await self.async_step_validation()
|
return await self.async_step_validation()
|
||||||
except SENSE_TIMEOUT_EXCEPTIONS:
|
except SENSE_CONNECT_EXCEPTIONS:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except SenseAuthenticationException:
|
except SenseAuthenticationException:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
@ -93,7 +93,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input:
|
if user_input:
|
||||||
try:
|
try:
|
||||||
await self._gateway.validate_mfa(user_input[CONF_CODE])
|
await self._gateway.validate_mfa(user_input[CONF_CODE])
|
||||||
except SENSE_TIMEOUT_EXCEPTIONS:
|
except SENSE_CONNECT_EXCEPTIONS:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except SenseAuthenticationException:
|
except SenseAuthenticationException:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
|
@ -3,8 +3,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from sense_energy import SenseAPITimeoutException
|
from sense_energy import (
|
||||||
from sense_energy.sense_exceptions import SenseWebsocketException
|
SenseAPIException,
|
||||||
|
SenseAPITimeoutException,
|
||||||
|
SenseWebsocketException,
|
||||||
|
)
|
||||||
|
|
||||||
DOMAIN = "sense"
|
DOMAIN = "sense"
|
||||||
DEFAULT_TIMEOUT = 10
|
DEFAULT_TIMEOUT = 10
|
||||||
@ -40,6 +43,11 @@ ICON = "mdi:flash"
|
|||||||
|
|
||||||
SENSE_TIMEOUT_EXCEPTIONS = (asyncio.TimeoutError, SenseAPITimeoutException)
|
SENSE_TIMEOUT_EXCEPTIONS = (asyncio.TimeoutError, SenseAPITimeoutException)
|
||||||
SENSE_EXCEPTIONS = (socket.gaierror, SenseWebsocketException)
|
SENSE_EXCEPTIONS = (socket.gaierror, SenseWebsocketException)
|
||||||
|
SENSE_CONNECT_EXCEPTIONS = (
|
||||||
|
asyncio.TimeoutError,
|
||||||
|
SenseAPITimeoutException,
|
||||||
|
SenseAPIException,
|
||||||
|
)
|
||||||
|
|
||||||
MDI_ICONS = {
|
MDI_ICONS = {
|
||||||
"ac": "air-conditioner",
|
"ac": "air-conditioner",
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"domain": "sense",
|
"domain": "sense",
|
||||||
"name": "Sense",
|
"name": "Sense",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/sense",
|
"documentation": "https://www.home-assistant.io/integrations/sense",
|
||||||
"requirements": ["sense_energy==0.10.2"],
|
"requirements": ["sense_energy==0.10.3"],
|
||||||
"codeowners": ["@kbickar"],
|
"codeowners": ["@kbickar"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"dhcp": [
|
"dhcp": [
|
||||||
|
@ -2118,7 +2118,7 @@ sendgrid==6.8.2
|
|||||||
|
|
||||||
# homeassistant.components.emulated_kasa
|
# homeassistant.components.emulated_kasa
|
||||||
# homeassistant.components.sense
|
# homeassistant.components.sense
|
||||||
sense_energy==0.10.2
|
sense_energy==0.10.3
|
||||||
|
|
||||||
# homeassistant.components.sentry
|
# homeassistant.components.sentry
|
||||||
sentry-sdk==1.5.8
|
sentry-sdk==1.5.8
|
||||||
|
@ -1352,7 +1352,7 @@ securetar==2022.2.0
|
|||||||
|
|
||||||
# homeassistant.components.emulated_kasa
|
# homeassistant.components.emulated_kasa
|
||||||
# homeassistant.components.sense
|
# homeassistant.components.sense
|
||||||
sense_energy==0.10.2
|
sense_energy==0.10.3
|
||||||
|
|
||||||
# homeassistant.components.sentry
|
# homeassistant.components.sentry
|
||||||
sentry-sdk==1.5.8
|
sentry-sdk==1.5.8
|
||||||
|
@ -3,6 +3,7 @@ from unittest.mock import AsyncMock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sense_energy import (
|
from sense_energy import (
|
||||||
|
SenseAPIException,
|
||||||
SenseAPITimeoutException,
|
SenseAPITimeoutException,
|
||||||
SenseAuthenticationException,
|
SenseAuthenticationException,
|
||||||
SenseMFARequiredException,
|
SenseMFARequiredException,
|
||||||
@ -189,7 +190,7 @@ async def test_form_mfa_required_exception(hass, mock_sense):
|
|||||||
assert result3["errors"] == {"base": "unknown"}
|
assert result3["errors"] == {"base": "unknown"}
|
||||||
|
|
||||||
|
|
||||||
async def test_form_cannot_connect(hass):
|
async def test_form_timeout(hass):
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
@ -208,6 +209,25 @@ async def test_form_cannot_connect(hass):
|
|||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
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):
|
async def test_form_unknown_exception(hass):
|
||||||
"""Test we handle unknown error."""
|
"""Test we handle unknown error."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user