diff --git a/homeassistant/components/openuv/diagnostics.py b/homeassistant/components/openuv/diagnostics.py new file mode 100644 index 00000000000..02b56ce0e90 --- /dev/null +++ b/homeassistant/components/openuv/diagnostics.py @@ -0,0 +1,35 @@ +"""Diagnostics support for OpenUV.""" +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_API_KEY, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant + +from . import OpenUV +from .const import DOMAIN + +CONF_COORDINATES = "coordinates" + +TO_REDACT = { + CONF_API_KEY, + CONF_LATITUDE, + CONF_LONGITUDE, +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + openuv: OpenUV = hass.data[DOMAIN][entry.entry_id] + + return { + "entry": { + "data": async_redact_data(entry.data, TO_REDACT), + "options": async_redact_data(entry.options, TO_REDACT), + }, + "data": async_redact_data(openuv.data, TO_REDACT), + } diff --git a/tests/components/openuv/conftest.py b/tests/components/openuv/conftest.py index f97878da9db..859769f2b58 100644 --- a/tests/components/openuv/conftest.py +++ b/tests/components/openuv/conftest.py @@ -1,4 +1,5 @@ """Define test fixtures for OpenUV.""" +import json from unittest.mock import patch import pytest @@ -12,7 +13,7 @@ from homeassistant.const import ( ) from homeassistant.setup import async_setup_component -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, load_fixture @pytest.fixture(name="config_entry") @@ -39,12 +40,29 @@ def config_fixture(hass): } +@pytest.fixture(name="data_protection_window", scope="session") +def data_protection_window_fixture(): + """Define a fixture to return UV protection window data.""" + return json.loads(load_fixture("protection_window_data.json", "openuv")) + + +@pytest.fixture(name="data_uv_index", scope="session") +def data_uv_index_fixture(): + """Define a fixture to return UV index data.""" + return json.loads(load_fixture("uv_index_data.json", "openuv")) + + @pytest.fixture(name="setup_openuv") -async def setup_openuv_fixture(hass, config): +async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index): """Define a fixture to set up OpenUV.""" - with patch("homeassistant.components.openuv.Client.uv_index"), patch( - "homeassistant.components.openuv.Client.uv_protection_window" - ), patch("homeassistant.components.openuv.PLATFORMS", []): + with patch( + "homeassistant.components.openuv.Client.uv_index", return_value=data_uv_index + ), patch( + "homeassistant.components.openuv.Client.uv_protection_window", + return_value=data_protection_window, + ), patch( + "homeassistant.components.openuv.PLATFORMS", [] + ): assert await async_setup_component(hass, DOMAIN, config) await hass.async_block_till_done() yield diff --git a/tests/components/openuv/fixtures/protection_window_data.json b/tests/components/openuv/fixtures/protection_window_data.json new file mode 100644 index 00000000000..c27bd25d948 --- /dev/null +++ b/tests/components/openuv/fixtures/protection_window_data.json @@ -0,0 +1,9 @@ +{ + "result": { + "from_time": "2018-07-30T15:17:49.750Z", + "from_uv": 3.2509, + "to_time": "2018-07-30T22:47:49.750Z", + "to_uv": 3.6483 + } +} + diff --git a/tests/components/openuv/fixtures/uv_index_data.json b/tests/components/openuv/fixtures/uv_index_data.json new file mode 100644 index 00000000000..76c2b2ed988 --- /dev/null +++ b/tests/components/openuv/fixtures/uv_index_data.json @@ -0,0 +1,41 @@ +{ + "result": { + "uv": 8.2342, + "uv_time": "2018-07-30T20:53:06.302Z", + "uv_max": 10.3335, + "uv_max_time": "2018-07-30T19:07:11.505Z", + "ozone": 300.7, + "ozone_time": "2018-07-30T18:07:04.466Z", + "safe_exposure_time": { + "st1": 20, + "st2": 24, + "st3": 32, + "st4": 40, + "st5": 65, + "st6": 121 + }, + "sun_info": { + "sun_times": { + "solarNoon": "2018-07-30T19:07:11.505Z", + "nadir": "2018-07-30T07:07:11.505Z", + "sunrise": "2018-07-30T11:57:49.750Z", + "sunset": "2018-07-31T02:16:33.259Z", + "sunriseEnd": "2018-07-30T12:00:53.253Z", + "sunsetStart": "2018-07-31T02:13:29.756Z", + "dawn": "2018-07-30T11:27:27.911Z", + "dusk": "2018-07-31T02:46:55.099Z", + "nauticalDawn": "2018-07-30T10:50:01.621Z", + "nauticalDusk": "2018-07-31T03:24:21.389Z", + "nightEnd": "2018-07-30T10:08:47.846Z", + "night": "2018-07-31T04:05:35.163Z", + "goldenHourEnd": "2018-07-30T12:36:14.026Z", + "goldenHour": "2018-07-31T01:38:08.983Z" + }, + "sun_position": { + "azimuth": 0.9567419441563509, + "altitude": 1.0235714275875594 + } + } + } +} + diff --git a/tests/components/openuv/test_diagnostics.py b/tests/components/openuv/test_diagnostics.py new file mode 100644 index 00000000000..4999e5d2132 --- /dev/null +++ b/tests/components/openuv/test_diagnostics.py @@ -0,0 +1,69 @@ +"""Test OpenUV 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_openuv): + """Test config entry diagnostics.""" + await hass.services.async_call("openuv", "update_data") + assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { + "entry": { + "data": { + "api_key": REDACTED, + "elevation": 0, + "latitude": REDACTED, + "longitude": REDACTED, + }, + "options": { + "from_window": 3.5, + "to_window": 3.5, + }, + }, + "data": { + "uv": { + "uv": 8.2342, + "uv_time": "2018-07-30T20:53:06.302Z", + "uv_max": 10.3335, + "uv_max_time": "2018-07-30T19:07:11.505Z", + "ozone": 300.7, + "ozone_time": "2018-07-30T18:07:04.466Z", + "safe_exposure_time": { + "st1": 20, + "st2": 24, + "st3": 32, + "st4": 40, + "st5": 65, + "st6": 121, + }, + "sun_info": { + "sun_times": { + "solarNoon": "2018-07-30T19:07:11.505Z", + "nadir": "2018-07-30T07:07:11.505Z", + "sunrise": "2018-07-30T11:57:49.750Z", + "sunset": "2018-07-31T02:16:33.259Z", + "sunriseEnd": "2018-07-30T12:00:53.253Z", + "sunsetStart": "2018-07-31T02:13:29.756Z", + "dawn": "2018-07-30T11:27:27.911Z", + "dusk": "2018-07-31T02:46:55.099Z", + "nauticalDawn": "2018-07-30T10:50:01.621Z", + "nauticalDusk": "2018-07-31T03:24:21.389Z", + "nightEnd": "2018-07-30T10:08:47.846Z", + "night": "2018-07-31T04:05:35.163Z", + "goldenHourEnd": "2018-07-30T12:36:14.026Z", + "goldenHour": "2018-07-31T01:38:08.983Z", + }, + "sun_position": { + "azimuth": 0.9567419441563509, + "altitude": 1.0235714275875594, + }, + }, + }, + "protection_window": { + "from_time": "2018-07-30T15:17:49.750Z", + "from_uv": 3.2509, + "to_time": "2018-07-30T22:47:49.750Z", + "to_uv": 3.6483, + }, + }, + }