Skip to content

Commit a6e575e

Browse files
authored
Add diagnostics for Telegram bot (home-assistant#154016)
1 parent 85392ae commit a6e575e

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Diagnostics platform for Telegram bot integration."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any
6+
7+
from yarl import URL
8+
9+
from homeassistant.components.diagnostics import REDACTED, async_redact_data
10+
from homeassistant.const import CONF_API_KEY, CONF_URL
11+
from homeassistant.core import HomeAssistant
12+
13+
from . import TelegramBotConfigEntry
14+
from .const import CONF_CHAT_ID
15+
16+
TO_REDACT = [CONF_API_KEY, CONF_CHAT_ID]
17+
18+
19+
async def async_get_config_entry_diagnostics(
20+
hass: HomeAssistant, config_entry: TelegramBotConfigEntry
21+
) -> dict[str, Any]:
22+
"""Return diagnostics for a config entry."""
23+
24+
data = async_redact_data(config_entry.data, TO_REDACT)
25+
if config_entry.data.get(CONF_URL):
26+
url = URL(config_entry.data[CONF_URL])
27+
data[CONF_URL] = url.with_host(REDACTED).human_repr()
28+
29+
return {
30+
"data": data,
31+
"options": async_redact_data(config_entry.options, TO_REDACT),
32+
"subentries_count": len(config_entry.subentries.values()),
33+
}

homeassistant/components/telegram_bot/quality_scale.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ rules:
4545

4646
# Gold
4747
devices: todo
48-
diagnostics: todo
48+
diagnostics: done
4949
discovery-update-info: todo
5050
discovery: todo
5151
docs-data-update: todo
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# serializer version: 1
2+
# name: test_diagnostics
3+
dict({
4+
'data': dict({
5+
'api_key': '**REDACTED**',
6+
'platform': 'webhooks',
7+
'proxy_url': None,
8+
'trusted_networks': list([
9+
'127.0.0.1/32',
10+
]),
11+
'url': 'https://**redacted**/',
12+
}),
13+
'options': dict({
14+
'parse_mode': 'markdown',
15+
}),
16+
'subentries_count': 2,
17+
})
18+
# ---
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Tests for Telegram bot diagnostics."""
2+
3+
from syrupy.assertion import SnapshotAssertion
4+
5+
from homeassistant.components.telegram_bot.const import DOMAIN
6+
from homeassistant.core import HomeAssistant
7+
8+
from tests.components.diagnostics import get_diagnostics_for_config_entry
9+
from tests.typing import ClientSessionGenerator
10+
11+
12+
async def test_diagnostics(
13+
hass: HomeAssistant,
14+
hass_client: ClientSessionGenerator,
15+
webhook_platform,
16+
mock_external_calls: None,
17+
mock_generate_secret_token,
18+
snapshot: SnapshotAssertion,
19+
) -> None:
20+
"""Test diagnostics."""
21+
22+
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
23+
24+
diagnostics = await get_diagnostics_for_config_entry(
25+
hass, hass_client, config_entry
26+
)
27+
assert diagnostics == snapshot

0 commit comments

Comments
 (0)