Add diagnostics platform to ntfy platform (#143774)

This commit is contained in:
Manu 2025-04-27 19:58:15 +02:00 committed by GitHub
parent d95c9c496e
commit c704df004a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 109 additions and 1 deletions

View File

@ -0,0 +1,29 @@
"""Diagnostics platform for ntfy integration."""
from __future__ import annotations
from typing import Any
from yarl import URL
from homeassistant.components.diagnostics import REDACTED
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from . import NtfyConfigEntry
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: NtfyConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
url = URL(config_entry.data[CONF_URL])
return {
CONF_URL: (
url.human_repr()
if url.host == "ntfy.sh"
else url.with_host(REDACTED).human_repr()
),
"topics": dict(config_entry.subentries),
}

View File

@ -49,7 +49,7 @@ rules:
# Gold # Gold
devices: done devices: done
diagnostics: todo diagnostics: done
discovery-update-info: todo discovery-update-info: todo
discovery: todo discovery: todo
docs-data-update: todo docs-data-update: todo

View File

@ -0,0 +1,24 @@
# serializer version: 1
# name: test_diagnostics
dict({
'topics': dict({
'ABCDEF': dict({
'data': dict({
'topic': 'mytopic',
}),
'subentry_id': 'ABCDEF',
'subentry_type': 'topic',
'title': 'mytopic',
'unique_id': 'mytopic',
}),
}),
'url': 'https://ntfy.sh/',
})
# ---
# name: test_diagnostics_redacted_url
dict({
'topics': dict({
}),
'url': 'http://**redacted**/',
})
# ---

View File

@ -0,0 +1,55 @@
"""Tests for ntfy diagnostics."""
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.ntfy.const import DOMAIN
from homeassistant.const import CONF_URL
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.mark.usefixtures("mock_aiontfy")
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert (
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
== snapshot
)
@pytest.mark.usefixtures("mock_aiontfy")
async def test_diagnostics_redacted_url(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics redacted URL."""
config_entry = MockConfigEntry(
domain=DOMAIN,
title="mydomain",
data={
CONF_URL: "http://mydomain/",
},
entry_id="123456789",
subentries_data=[],
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert (
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
== snapshot
)