From f8265f44befb4add1ff23d90f913ddc77a27d1fc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 29 Mar 2021 22:46:02 -1000 Subject: [PATCH] Include platform only integrations in analytics (#48493) --- .../components/analytics/analytics.py | 16 +++++--- tests/components/analytics/test_analytics.py | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/analytics/analytics.py b/homeassistant/components/analytics/analytics.py index 2963e41f90b..ef7c2fbde6e 100644 --- a/homeassistant/components/analytics/analytics.py +++ b/homeassistant/components/analytics/analytics.py @@ -12,7 +12,8 @@ from homeassistant.core import HomeAssistant 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, @@ -139,13 +140,18 @@ class Analytics: 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 diff --git a/tests/components/analytics/test_analytics.py b/tests/components/analytics/test_analytics.py index d4692b4fcc0..1a636d16598 100644 --- a/tests/components/analytics/test_analytics.py +++ b/tests/components/analytics/test_analytics.py @@ -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 ( @@ -13,6 +14,7 @@ from homeassistant.components.analytics.const import ( ATTR_USAGE, ) from homeassistant.const import __version__ as HA_VERSION +from homeassistant.loader import IntegrationNotFound MOCK_HUUID = "abcdefg" @@ -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)