Add diagnostics to YouTube (#96975)

This commit is contained in:
Joost Lekkerkerker 2023-07-21 11:58:49 +02:00 committed by GitHub
parent e4d65cbae1
commit 33c2fc008a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,24 @@
"""Diagnostics support for YouTube."""
from __future__ import annotations
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import ATTR_DESCRIPTION, ATTR_LATEST_VIDEO, COORDINATOR, DOMAIN
from .coordinator import YouTubeDataUpdateCoordinator
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator: YouTubeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
COORDINATOR
]
sensor_data = {}
for channel_id, channel_data in coordinator.data.items():
channel_data.get(ATTR_LATEST_VIDEO, {}).pop(ATTR_DESCRIPTION)
sensor_data[channel_id] = channel_data
return sensor_data

View File

@ -0,0 +1,17 @@
# serializer version: 1
# name: test_diagnostics
dict({
'UC_x5XG1OV2P6uZZ5FSM9Ttw': dict({
'icon': 'https://yt3.ggpht.com/fca_HuJ99xUxflWdex0XViC3NfctBFreIl8y4i9z411asnGTWY-Ql3MeH_ybA4kNaOjY7kyA=s800-c-k-c0x00ffffff-no-rj',
'id': 'UC_x5XG1OV2P6uZZ5FSM9Ttw',
'latest_video': dict({
'published_at': '2023-05-11T00:20:46Z',
'thumbnail': 'https://i.ytimg.com/vi/wysukDrMdqU/sddefault.jpg',
'title': "What's new in Google Home in less than 1 minute",
'video_id': 'wysukDrMdqU',
}),
'subscriber_count': 2290000,
'title': 'Google for Developers',
}),
})
# ---

View File

@ -0,0 +1,23 @@
"""Tests for the diagnostics data provided by the YouTube integration."""
from syrupy import SnapshotAssertion
from homeassistant.components.youtube.const import DOMAIN
from homeassistant.core import HomeAssistant
from .conftest import ComponentSetup
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
setup_integration: ComponentSetup,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics."""
await setup_integration()
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot