From 151e075e287c29108e09fb453af4ae9b89e08dab Mon Sep 17 00:00:00 2001 From: Artur Pragacz <49985303+arturpragacz@users.noreply.github.com> Date: Wed, 18 Feb 2026 13:45:45 +0100 Subject: [PATCH] Do not send empty snapshots in analytics (#163351) --- .../components/analytics/analytics.py | 4 +++ tests/components/analytics/conftest.py | 18 ++++++++++ tests/components/analytics/test_analytics.py | 36 ++++++++++++++----- tests/components/analytics/test_init.py | 1 + 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 tests/components/analytics/conftest.py diff --git a/homeassistant/components/analytics/analytics.py b/homeassistant/components/analytics/analytics.py index 7778e3239ab..1634f01bf06 100644 --- a/homeassistant/components/analytics/analytics.py +++ b/homeassistant/components/analytics/analytics.py @@ -534,6 +534,10 @@ class Analytics: payload = await _async_snapshot_payload(self._hass) + if not payload: + LOGGER.info("Skipping snapshot submission, no data to send") + return + headers = { "Content-Type": "application/json", "User-Agent": f"home-assistant/{HA_VERSION}", diff --git a/tests/components/analytics/conftest.py b/tests/components/analytics/conftest.py new file mode 100644 index 00000000000..150fcc1df8c --- /dev/null +++ b/tests/components/analytics/conftest.py @@ -0,0 +1,18 @@ +"""Common fixtures for the analytics tests.""" + +from collections.abc import Generator +from unittest.mock import patch + +import pytest + +MOCK_SNAPSHOT_PAYLOAD = {"mock_integration": {"devices": [], "entities": []}} + + +@pytest.fixture +def mock_snapshot_payload() -> Generator[None]: + """Mock _async_snapshot_payload to return non-empty data.""" + with patch( + "homeassistant.components.analytics.analytics._async_snapshot_payload", + return_value=MOCK_SNAPSHOT_PAYLOAD, + ): + yield diff --git a/tests/components/analytics/test_analytics.py b/tests/components/analytics/test_analytics.py index cf0e327ef7f..d1c90085cb0 100644 --- a/tests/components/analytics/test_analytics.py +++ b/tests/components/analytics/test_analytics.py @@ -1464,7 +1464,7 @@ async def test_analytics_platforms( } -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_send_snapshot_disabled( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, @@ -1481,6 +1481,24 @@ async def test_send_snapshot_disabled( @pytest.mark.usefixtures("labs_snapshots_enabled") +async def test_send_snapshot_empty( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + aioclient_mock: AiohttpClientMocker, +) -> None: + """Test no snapshots are sent when payload is empty.""" + aioclient_mock.post(SNAPSHOT_ENDPOINT_URL, status=200, json={}) + + analytics = Analytics(hass) + + await analytics.save_preferences({ATTR_SNAPSHOTS: True}) + await analytics.send_snapshot() + + assert len(aioclient_mock.mock_calls) == 0 + assert "Skipping snapshot submission, no data to send" in caplog.text + + +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_send_snapshot_success( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, @@ -1505,7 +1523,7 @@ async def test_send_snapshot_success( assert "Submitted snapshot analytics to Home Assistant servers" in caplog.text -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_send_snapshot_with_existing_identifier( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, @@ -1541,7 +1559,7 @@ async def test_send_snapshot_with_existing_identifier( assert "Submitted snapshot analytics to Home Assistant servers" in caplog.text -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_send_snapshot_invalid_identifier( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, @@ -1578,7 +1596,7 @@ async def test_send_snapshot_invalid_identifier( assert "Invalid submission identifier" in caplog.text -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") @pytest.mark.parametrize( ("post_kwargs", "expected_log"), [ @@ -1643,7 +1661,7 @@ async def test_send_snapshot_error( assert expected_log in caplog.text -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_async_schedule( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, @@ -1680,7 +1698,7 @@ async def test_async_schedule( assert 0 <= preferences["snapshot_submission_time"] <= 86400 -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_async_schedule_disabled( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, @@ -1705,7 +1723,7 @@ async def test_async_schedule_disabled( assert len(aioclient_mock.mock_calls) == 0 -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_async_schedule_already_scheduled( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, @@ -1739,7 +1757,7 @@ async def test_async_schedule_already_scheduled( ) -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") @pytest.mark.parametrize(("onboarded"), [True, False]) async def test_async_schedule_cancel_when_disabled( hass: HomeAssistant, @@ -1778,7 +1796,7 @@ async def test_async_schedule_cancel_when_disabled( assert len(aioclient_mock.mock_calls) == 0 -@pytest.mark.usefixtures("labs_snapshots_enabled") +@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload") async def test_async_schedule_snapshots_url( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, diff --git a/tests/components/analytics/test_init.py b/tests/components/analytics/test_init.py index daae59e5445..2459a7320ed 100644 --- a/tests/components/analytics/test_init.py +++ b/tests/components/analytics/test_init.py @@ -37,6 +37,7 @@ async def test_setup(hass: HomeAssistant) -> None: assert DOMAIN in hass.data +@pytest.mark.usefixtures("mock_snapshot_payload") async def test_labs_feature_toggle( hass: HomeAssistant, hass_storage: dict[str, Any],