From 2b7685b71d62971f179c7c8c43ccc6a9e7b45d02 Mon Sep 17 00:00:00 2001 From: tronikos Date: Thu, 30 May 2024 19:13:54 -0700 Subject: [PATCH] Add Google Assistant SDK diagnostics (#118513) --- .../google_assistant_sdk/diagnostics.py | 24 ++++++++++++ script/hassfest/manifest.py | 1 - .../snapshots/test_diagnostics.ambr | 17 +++++++++ .../google_assistant_sdk/test_diagnostics.py | 38 +++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/google_assistant_sdk/diagnostics.py create mode 100644 tests/components/google_assistant_sdk/snapshots/test_diagnostics.ambr create mode 100644 tests/components/google_assistant_sdk/test_diagnostics.py diff --git a/homeassistant/components/google_assistant_sdk/diagnostics.py b/homeassistant/components/google_assistant_sdk/diagnostics.py new file mode 100644 index 00000000000..eacded4e2e6 --- /dev/null +++ b/homeassistant/components/google_assistant_sdk/diagnostics.py @@ -0,0 +1,24 @@ +"""Diagnostics support for Google Assistant SDK.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +TO_REDACT = {"access_token", "refresh_token"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + return async_redact_data( + { + "data": entry.data, + "options": entry.options, + }, + TO_REDACT, + ) diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index e92ec00b117..8ff0750250f 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -119,7 +119,6 @@ NO_DIAGNOSTICS = [ "dlna_dms", "gdacs", "geonetnz_quakes", - "google_assistant_sdk", "hyperion", # Modbus is excluded because it doesn't have to have a config flow # according to ADR-0010, since it's a protocol integration. This diff --git a/tests/components/google_assistant_sdk/snapshots/test_diagnostics.ambr b/tests/components/google_assistant_sdk/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..134bf6e5ad4 --- /dev/null +++ b/tests/components/google_assistant_sdk/snapshots/test_diagnostics.ambr @@ -0,0 +1,17 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'data': dict({ + 'auth_implementation': 'google_assistant_sdk', + 'token': dict({ + 'access_token': '**REDACTED**', + 'expires_at': 1717074000.0, + 'refresh_token': '**REDACTED**', + 'scope': 'https://www.googleapis.com/auth/assistant-sdk-prototype', + }), + }), + 'options': dict({ + 'language_code': 'en-US', + }), + }) +# --- diff --git a/tests/components/google_assistant_sdk/test_diagnostics.py b/tests/components/google_assistant_sdk/test_diagnostics.py new file mode 100644 index 00000000000..cf815c96943 --- /dev/null +++ b/tests/components/google_assistant_sdk/test_diagnostics.py @@ -0,0 +1,38 @@ +"""Tests for the diagnostics data provided by the Google Assistant SDK integration.""" + +from freezegun import freeze_time +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.google_assistant_sdk.const import CONF_LANGUAGE_CODE +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +@pytest.fixture(autouse=True) +def freeze_the_time(): + """Freeze the time.""" + with freeze_time("2024-05-30 12:00:00", tz_offset=0): + yield + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics.""" + config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry( + config_entry, + options={CONF_LANGUAGE_CODE: "en-US"}, + ) + await hass.config_entries.async_setup(config_entry.entry_id) + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + == snapshot + )