diff --git a/homeassistant/components/dexcom/diagnostics.py b/homeassistant/components/dexcom/diagnostics.py new file mode 100644 index 000000000000..813c1c9f8f5a --- /dev/null +++ b/homeassistant/components/dexcom/diagnostics.py @@ -0,0 +1,31 @@ +"""Diagnostics support for the Dexcom integration.""" + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.const import CONF_PASSWORD, CONF_UNIQUE_ID, CONF_USERNAME +from homeassistant.core import HomeAssistant +from homeassistant.util import dt as dt_util + +from .coordinator import DexcomConfigEntry + +# The entry title and unique ID are the Dexcom username. +TO_REDACT = {CONF_PASSWORD, CONF_UNIQUE_ID, CONF_USERNAME, "title"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: DexcomConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator = entry.runtime_data + reading: dict[str, Any] | None = None + if (glucose := coordinator.data) is not None: + reading = { + "timestamp": glucose.datetime.isoformat(), + "age_seconds": round((dt_util.utcnow() - glucose.datetime).total_seconds()), + } + return { + "config_entry": async_redact_data(entry.as_dict(), TO_REDACT), + "last_update_success": coordinator.last_update_success, + "reading": reading, + } diff --git a/tests/components/dexcom/snapshots/test_diagnostics.ambr b/tests/components/dexcom/snapshots/test_diagnostics.ambr new file mode 100644 index 000000000000..75bb7813e5ee --- /dev/null +++ b/tests/components/dexcom/snapshots/test_diagnostics.ambr @@ -0,0 +1,32 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'config_entry': dict({ + 'data': dict({ + 'password': '**REDACTED**', + 'server': 'US', + 'username': '**REDACTED**', + }), + 'disabled_by': None, + 'discovery_keys': dict({ + }), + 'domain': 'dexcom', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'subentries': list([ + ]), + 'title': '**REDACTED**', + 'unique_id': '**REDACTED**', + 'version': 1, + }), + 'last_update_success': True, + 'reading': dict({ + 'age_seconds': 300, + 'timestamp': '2025-04-19T12:58:33.085000-04:00', + }), + }) +# --- diff --git a/tests/components/dexcom/test_diagnostics.py b/tests/components/dexcom/test_diagnostics.py new file mode 100644 index 000000000000..2070275f1204 --- /dev/null +++ b/tests/components/dexcom/test_diagnostics.py @@ -0,0 +1,52 @@ +"""Tests for the Dexcom diagnostics.""" + +from datetime import timedelta +from unittest.mock import patch + +from freezegun.api import FrozenDateTimeFactory +import pytest +from pytest_homeassistant.common import async_fire_time_changed +from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry +from pytest_homeassistant.typing import ClientSessionGenerator +from syrupy.assertion import SnapshotAssertion +from syrupy.filters import props + +from homeassistant.core import HomeAssistant + +from . import init_integration + + +@pytest.mark.freeze_time("2025-04-19T17:03:33+00:00") +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Test the diagnostics show the latest reading and redact the account.""" + entry = await init_integration(hass) + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result == snapshot(exclude=props("created_at", "entry_id", "modified_at")) + + +async def test_entry_diagnostics_no_reading( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the diagnostics when Dexcom has no current reading.""" + entry = await init_integration(hass) + + with patch( + "homeassistant.components.dexcom.Dexcom.get_current_glucose_reading", + return_value=None, + ): + freezer.tick(timedelta(minutes=3)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result["last_update_success"] is True + assert result["reading"] is None