Handle uncaught exceptions in Analytics insights (#117558)
This commit is contained in:
parent
59645aeb0f
commit
4cded378bf
3 changed files with 23 additions and 11 deletions
|
@ -82,6 +82,9 @@ class HomeassistantAnalyticsConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
except HomeassistantAnalyticsConnectionError:
|
except HomeassistantAnalyticsConnectionError:
|
||||||
LOGGER.exception("Error connecting to Home Assistant analytics")
|
LOGGER.exception("Error connecting to Home Assistant analytics")
|
||||||
return self.async_abort(reason="cannot_connect")
|
return self.async_abort(reason="cannot_connect")
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
LOGGER.exception("Unexpected error")
|
||||||
|
return self.async_abort(reason="unknown")
|
||||||
|
|
||||||
options = [
|
options = [
|
||||||
SelectOptionDict(
|
SelectOptionDict(
|
||||||
|
|
|
@ -13,7 +13,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
|
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"no_integration_selected": "You must select at least one integration to track"
|
"no_integration_selected": "You must select at least one integration to track"
|
||||||
|
|
|
@ -6,12 +6,12 @@ from unittest.mock import AsyncMock
|
||||||
import pytest
|
import pytest
|
||||||
from python_homeassistant_analytics import HomeassistantAnalyticsConnectionError
|
from python_homeassistant_analytics import HomeassistantAnalyticsConnectionError
|
||||||
|
|
||||||
from homeassistant import config_entries
|
|
||||||
from homeassistant.components.analytics_insights.const import (
|
from homeassistant.components.analytics_insights.const import (
|
||||||
CONF_TRACKED_CUSTOM_INTEGRATIONS,
|
CONF_TRACKED_CUSTOM_INTEGRATIONS,
|
||||||
CONF_TRACKED_INTEGRATIONS,
|
CONF_TRACKED_INTEGRATIONS,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ async def test_form(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
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": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ async def test_submitting_empty_form(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we can't submit an empty form."""
|
"""Test we can't submit an empty form."""
|
||||||
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": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
|
||||||
|
@ -128,20 +128,28 @@ async def test_submitting_empty_form(
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("exception", "reason"),
|
||||||
|
[
|
||||||
|
(HomeassistantAnalyticsConnectionError, "cannot_connect"),
|
||||||
|
(Exception, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_form_cannot_connect(
|
async def test_form_cannot_connect(
|
||||||
hass: HomeAssistant, mock_analytics_client: AsyncMock
|
hass: HomeAssistant,
|
||||||
|
mock_analytics_client: AsyncMock,
|
||||||
|
exception: Exception,
|
||||||
|
reason: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
|
|
||||||
mock_analytics_client.get_integrations.side_effect = (
|
mock_analytics_client.get_integrations.side_effect = exception
|
||||||
HomeassistantAnalyticsConnectionError
|
|
||||||
)
|
|
||||||
|
|
||||||
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": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "cannot_connect"
|
assert result["reason"] == reason
|
||||||
|
|
||||||
|
|
||||||
async def test_form_already_configured(
|
async def test_form_already_configured(
|
||||||
|
@ -159,7 +167,7 @@ async def test_form_already_configured(
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
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": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "single_instance_allowed"
|
assert result["reason"] == "single_instance_allowed"
|
||||||
|
|
Loading…
Add table
Reference in a new issue