From 224d0d80b23188673567462d9278973e0ede51be Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Mon, 24 Jan 2022 08:16:26 -0700 Subject: [PATCH] Add diagnostics to Flu Near You (#64597) --- .../components/flunearyou/diagnostics.py | 36 ++++++++++ tests/components/flunearyou/conftest.py | 12 +++- .../flunearyou/fixtures/user_data.json | 51 ++++++++++++++ .../components/flunearyou/test_diagnostics.py | 69 +++++++++++++++++++ 4 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/flunearyou/diagnostics.py create mode 100644 tests/components/flunearyou/fixtures/user_data.json create mode 100644 tests/components/flunearyou/test_diagnostics.py diff --git a/homeassistant/components/flunearyou/diagnostics.py b/homeassistant/components/flunearyou/diagnostics.py new file mode 100644 index 00000000000..666ffb66c67 --- /dev/null +++ b/homeassistant/components/flunearyou/diagnostics.py @@ -0,0 +1,36 @@ +"""Diagnostics support for Flu Near You.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import CATEGORY_CDC_REPORT, CATEGORY_USER_REPORT, DOMAIN + +TO_REDACT = { + CONF_LATITUDE, + CONF_LONGITUDE, +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinators: dict[str, DataUpdateCoordinator] = hass.data[DOMAIN][entry.entry_id] + + return { + "data": { + CATEGORY_CDC_REPORT: async_redact_data( + coordinators[CATEGORY_CDC_REPORT].data, TO_REDACT + ), + CATEGORY_USER_REPORT: [ + async_redact_data(report, TO_REDACT) + for report in coordinators[CATEGORY_USER_REPORT].data + ], + }, + } diff --git a/tests/components/flunearyou/conftest.py b/tests/components/flunearyou/conftest.py index ff539b08a42..a3de61149aa 100644 --- a/tests/components/flunearyou/conftest.py +++ b/tests/components/flunearyou/conftest.py @@ -34,12 +34,20 @@ def data_cdc_fixture(): return json.loads(load_fixture("cdc_data.json", "flunearyou")) +@pytest.fixture(name="data_user", scope="session") +def data_user_fixture(): + """Define user data.""" + return json.loads(load_fixture("user_data.json", "flunearyou")) + + @pytest.fixture(name="setup_flunearyou") -async def setup_flunearyou_fixture(hass, config, data_cdc): +async def setup_flunearyou_fixture(hass, data_cdc, data_user, config): """Define a fixture to set up Flu Near You.""" with patch( "pyflunearyou.cdc.CdcReport.status_by_coordinates", return_value=data_cdc - ), patch("pyflunearyou.user.UserReport.status_by_coordinates"), patch( + ), patch( + "pyflunearyou.user.UserReport.status_by_coordinates", return_value=data_user + ), patch( "homeassistant.components.flunearyou.PLATFORMS", [] ): assert await async_setup_component(hass, DOMAIN, config) diff --git a/tests/components/flunearyou/fixtures/user_data.json b/tests/components/flunearyou/fixtures/user_data.json new file mode 100644 index 00000000000..47d54d1c41e --- /dev/null +++ b/tests/components/flunearyou/fixtures/user_data.json @@ -0,0 +1,51 @@ +[ + { + "id": 1, + "city": "Chester(72934)", + "place_id": "49377", + "zip": "72934", + "contained_by": "610", + "latitude": "35.687603", + "longitude": "-94.253845", + "none": 1, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1" + }, + { + "id": 2, + "city": "Los Angeles(90046)", + "place_id": "23818", + "zip": "90046", + "contained_by": "204", + "latitude": "34.114731", + "longitude": "-118.363724", + "none": 2, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1" + }, + { + "id": 3, + "city": "Corvallis(97330)", + "place_id": "21462", + "zip": "97330", + "contained_by": "239", + "latitude": "44.638504", + "longitude": "-123.292938", + "none": 3, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1" + } +] + diff --git a/tests/components/flunearyou/test_diagnostics.py b/tests/components/flunearyou/test_diagnostics.py new file mode 100644 index 00000000000..a01d78d5de2 --- /dev/null +++ b/tests/components/flunearyou/test_diagnostics.py @@ -0,0 +1,69 @@ +"""Test Flu Near You diagnostics.""" +from homeassistant.components.diagnostics import REDACTED + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_entry_diagnostics(hass, config_entry, hass_client, setup_flunearyou): + """Test config entry diagnostics.""" + assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { + "data": { + "cdc_report": { + "level": "Low", + "level2": "None", + "week_date": "2020-05-16", + "name": "Washington State", + "fill": {"color": "#00B7B6", "opacity": 0.7}, + }, + "user_report": [ + { + "id": 1, + "city": "Chester(72934)", + "place_id": "49377", + "zip": "72934", + "contained_by": "610", + "latitude": REDACTED, + "longitude": REDACTED, + "none": 1, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1", + }, + { + "id": 2, + "city": "Los Angeles(90046)", + "place_id": "23818", + "zip": "90046", + "contained_by": "204", + "latitude": REDACTED, + "longitude": REDACTED, + "none": 2, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1", + }, + { + "id": 3, + "city": "Corvallis(97330)", + "place_id": "21462", + "zip": "97330", + "contained_by": "239", + "latitude": REDACTED, + "longitude": REDACTED, + "none": 3, + "symptoms": 0, + "flu": 0, + "lepto": 0, + "dengue": 0, + "chick": 0, + "icon": "1", + }, + ], + }, + }