From 04a99fdbfc8a8aa31719abe263e3c7ec1e8d77a7 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Thu, 16 Mar 2023 20:05:01 -0700 Subject: [PATCH] Add local calendar diagnostics platform (#89776) * Add local calendar diagnostics platform * Use redaction from ical * Update diagnostics for new ical version * Apply suggestions from code review Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Use snapshot tests for local calendar diagnostics * Setup diagnostics directly in tests rather than via dependencies --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- .../components/local_calendar/diagnostics.py | 27 ++++++++ .../snapshots/test_diagnostics.ambr | 32 ++++++++++ .../local_calendar/test_diagnostics.py | 62 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 homeassistant/components/local_calendar/diagnostics.py create mode 100644 tests/components/local_calendar/snapshots/test_diagnostics.ambr create mode 100644 tests/components/local_calendar/test_diagnostics.py diff --git a/homeassistant/components/local_calendar/diagnostics.py b/homeassistant/components/local_calendar/diagnostics.py new file mode 100644 index 00000000000..51b53ff0073 --- /dev/null +++ b/homeassistant/components/local_calendar/diagnostics.py @@ -0,0 +1,27 @@ +"""Provides diagnostics for local calendar.""" + +import datetime +from typing import Any + +from ical.diagnostics import redact_ics + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.util import dt as dt_util + +from .const import DOMAIN + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + payload: dict[str, Any] = { + "now": dt_util.now().isoformat(), + "timezone": str(dt_util.DEFAULT_TIME_ZONE), + "system_timezone": str(datetime.datetime.utcnow().astimezone().tzinfo), + } + store = hass.data[DOMAIN][config_entry.entry_id] + ics = await store.async_load() + payload["ics"] = "\n".join(redact_ics(ics)) + return payload diff --git a/tests/components/local_calendar/snapshots/test_diagnostics.ambr b/tests/components/local_calendar/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..e61b9da7a90 --- /dev/null +++ b/tests/components/local_calendar/snapshots/test_diagnostics.ambr @@ -0,0 +1,32 @@ +# serializer version: 1 +# name: test_api_date_time_event + dict({ + 'ics': ''' + BEGIN:VCALENDAR + PRODID:-//github.com/allenporter/ical//4.5.0//EN + VERSION:*** + BEGIN:VEVENT + DTSTAMP:20230313T190500 + UID:*** + DTSTART:19970714T110000 + DTEND:19970714T220000 + SUMMARY:*** + CREATED:20230313T190500 + RRULE:FREQ=DAILY + SEQUENCE:*** + END:VEVENT + END:VCALENDAR + ''', + 'now': '2023-03-13T13:05:00-06:00', + 'system_timezone': 'tzlocal()', + 'timezone': 'America/Regina', + }) +# --- +# name: test_empty_calendar + dict({ + 'ics': '', + 'now': '2023-03-13T13:05:00-06:00', + 'system_timezone': 'tzlocal()', + 'timezone': 'America/Regina', + }) +# --- diff --git a/tests/components/local_calendar/test_diagnostics.py b/tests/components/local_calendar/test_diagnostics.py new file mode 100644 index 00000000000..8b033cf4fdb --- /dev/null +++ b/tests/components/local_calendar/test_diagnostics.py @@ -0,0 +1,62 @@ +"""Tests for diagnostics platform of local calendar.""" + +from freezegun import freeze_time +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from .conftest import TEST_ENTITY, ClientFixture + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +@pytest.fixture(autouse=True) +async def setup_diag(hass): + """Set up diagnostics platform.""" + assert await async_setup_component(hass, "diagnostics", {}) + + +@freeze_time("2023-03-13 12:05:00-07:00") +async def test_empty_calendar( + hass: HomeAssistant, + setup_integration: None, + hass_client: ClientSessionGenerator, + config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics against an empty calendar.""" + data = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert data == snapshot + + +@freeze_time("2023-03-13 12:05:00-07:00") +async def test_api_date_time_event( + hass: HomeAssistant, + setup_integration: None, + config_entry: MockConfigEntry, + hass_client: ClientSessionGenerator, + ws_client: ClientFixture, + snapshot: SnapshotAssertion, +) -> None: + """Test an event with a start/end date time.""" + + client = await ws_client() + await client.cmd_result( + "create", + { + "entity_id": TEST_ENTITY, + "event": { + "summary": "Bastille Day Party", + "dtstart": "1997-07-14T17:00:00+00:00", + "dtend": "1997-07-15T04:00:00+00:00", + "rrule": "FREQ=DAILY", + }, + }, + ) + + data = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert data == snapshot