Include platform only integrations in analytics (#48493)

This commit is contained in:
J. Nick Koston 2021-03-29 22:46:02 -10:00 committed by GitHub
parent e47d576ee7
commit f8265f44be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 5 deletions

View File

@ -12,7 +12,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
from homeassistant.helpers.system_info import async_get_system_info 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 ( from .const import (
ANALYTICS_ENDPOINT_URL, ANALYTICS_ENDPOINT_URL,
@ -139,13 +140,18 @@ class Analytics:
configured_integrations = await asyncio.gather( configured_integrations = await asyncio.gather(
*[ *[
async_get_integration(self.hass, domain) async_get_integration(self.hass, domain)
for domain in self.hass.config.components for domain in async_get_loaded_integrations(self.hass)
# Filter out platforms. ],
if "." not in domain return_exceptions=True,
]
) )
for integration in configured_integrations: 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: if integration.disabled or not integration.is_built_in:
continue continue

View File

@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, Mock, patch from unittest.mock import AsyncMock, Mock, patch
import aiohttp import aiohttp
import pytest
from homeassistant.components.analytics.analytics import Analytics from homeassistant.components.analytics.analytics import Analytics
from homeassistant.components.analytics.const import ( from homeassistant.components.analytics.const import (
@ -13,6 +14,7 @@ from homeassistant.components.analytics.const import (
ATTR_USAGE, ATTR_USAGE,
) )
from homeassistant.const import __version__ as HA_VERSION from homeassistant.const import __version__ as HA_VERSION
from homeassistant.loader import IntegrationNotFound
MOCK_HUUID = "abcdefg" MOCK_HUUID = "abcdefg"
@ -222,6 +224,44 @@ async def test_send_statistics(hass, caplog, aioclient_mock):
assert "'integrations':" not in caplog.text 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): async def test_send_statistics_with_supervisor(hass, caplog, aioclient_mock):
"""Test send statistics prefrences are defined.""" """Test send statistics prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200) aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)