Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions homeassistant/components/analytics/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.storage import Store
from homeassistant.helpers.system_info import async_get_system_info
from homeassistant.loader import async_get_integration
from homeassistant.loader import IntegrationNotFound, async_get_integration
from homeassistant.setup import async_get_loaded_integrations

from .const import (
ANALYTICS_ENDPOINT_URL,
Expand Down Expand Up @@ -139,13 +140,18 @@ async def send_analytics(self, _=None) -> None:
configured_integrations = await asyncio.gather(
*[
async_get_integration(self.hass, domain)
for domain in self.hass.config.components
# Filter out platforms.
if "." not in domain
]
for domain in async_get_loaded_integrations(self.hass)
],
return_exceptions=True,
)

for integration in configured_integrations:
if isinstance(integration, IntegrationNotFound):
continue

if isinstance(integration, BaseException):
raise integration

if integration.disabled or not integration.is_built_in:
continue

Expand Down
40 changes: 40 additions & 0 deletions tests/components/analytics/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, Mock, patch

import aiohttp
import pytest

from homeassistant.components.analytics.analytics import Analytics
from homeassistant.components.analytics.const import (
Expand All @@ -13,6 +14,7 @@
ATTR_USAGE,
)
from homeassistant.const import __version__ as HA_VERSION
from homeassistant.loader import IntegrationNotFound

MOCK_HUUID = "abcdefg"

Expand Down Expand Up @@ -222,6 +224,44 @@ async def test_send_statistics(hass, caplog, aioclient_mock):
assert "'integrations':" not in caplog.text


async def test_send_statistics_one_integration_fails(hass, caplog, aioclient_mock):
"""Test send statistics prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
await analytics.save_preferences({ATTR_BASE: True, ATTR_STATISTICS: True})
assert analytics.preferences[ATTR_BASE]
assert analytics.preferences[ATTR_STATISTICS]
hass.config.components = ["default_config"]

with patch(
"homeassistant.components.analytics.analytics.async_get_integration",
side_effect=IntegrationNotFound("any"),
), patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
await analytics.send_analytics()

post_call = aioclient_mock.mock_calls[0]
assert "huuid" in post_call[2]
assert post_call[2]["integration_count"] == 0


async def test_send_statistics_async_get_integration_unknown_exception(
hass, caplog, aioclient_mock
):
"""Test send statistics prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
await analytics.save_preferences({ATTR_BASE: True, ATTR_STATISTICS: True})
assert analytics.preferences[ATTR_BASE]
assert analytics.preferences[ATTR_STATISTICS]
hass.config.components = ["default_config"]

with pytest.raises(ValueError), patch(
"homeassistant.components.analytics.analytics.async_get_integration",
side_effect=ValueError,
), patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
await analytics.send_analytics()


async def test_send_statistics_with_supervisor(hass, caplog, aioclient_mock):
"""Test send statistics prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
Expand Down