mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add diagnostics to DLNA Digital Media Server (#189150)
Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: erwindouna <5011203+erwindouna@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude
copilot-swe-agent[bot]
erwindouna
parent
57607bc8fa
commit
754e3218b3
@@ -0,0 +1,44 @@
|
||||
"""Diagnostics support for DLNA DMS."""
|
||||
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from async_upnp_client.profiles.dlna import DmsDevice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .dms import get_domain_data
|
||||
|
||||
|
||||
def _device_diagnostics(device: DmsDevice) -> dict[str, Any]:
|
||||
"""Return diagnostics for a connected DMS device."""
|
||||
profile_device = device.profile_device
|
||||
return {
|
||||
"manufacturer": profile_device.manufacturer,
|
||||
"model_name": profile_device.model_name,
|
||||
"model_number": profile_device.model_number,
|
||||
"model_description": profile_device.model_description,
|
||||
"device_type": profile_device.device_type,
|
||||
"services": {
|
||||
service.service_type: sorted(service.actions)
|
||||
for service in profile_device.services.values()
|
||||
},
|
||||
"search_capabilities": device.search_capabilities,
|
||||
"sort_capabilities": device.sort_capabilities,
|
||||
"source_protocol_info": device.source_protocol_info,
|
||||
"system_update_id": device.system_update_id,
|
||||
}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
if TYPE_CHECKING:
|
||||
assert entry.unique_id
|
||||
source = get_domain_data(hass).devices[entry.unique_id]
|
||||
device = source.device
|
||||
return {
|
||||
"available": source.available,
|
||||
"device": _device_diagnostics(device) if device else None,
|
||||
}
|
||||
@@ -355,6 +355,11 @@ class DmsDeviceSource:
|
||||
"""Return a unique ID (slug) for this source for people to use in URLs."""
|
||||
return self.config_entry.data[CONF_SOURCE_ID] # type: ignore[no-any-return]
|
||||
|
||||
@property
|
||||
def device(self) -> DmsDevice | None:
|
||||
"""Return the connected DMS device, if any."""
|
||||
return self._device
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return an URL to an icon for the media server."""
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'available': True,
|
||||
'device': dict({
|
||||
'device_type': 'urn:schemas-upnp-org:device:MediaServer:1',
|
||||
'manufacturer': 'device_manufacturer',
|
||||
'model_description': 'Test media server',
|
||||
'model_name': 'device_model_name',
|
||||
'model_number': '1.0',
|
||||
'search_capabilities': list([
|
||||
'dc:title',
|
||||
'upnp:class',
|
||||
]),
|
||||
'services': dict({
|
||||
'urn:schemas-upnp-org:service:ConnectionManager:1': list([
|
||||
'GetProtocolInfo',
|
||||
]),
|
||||
'urn:schemas-upnp-org:service:ContentDirectory:1': list([
|
||||
'Browse',
|
||||
'GetSearchCapabilities',
|
||||
'GetSortCapabilities',
|
||||
'Search',
|
||||
]),
|
||||
}),
|
||||
'sort_capabilities': list([
|
||||
'dc:title',
|
||||
'dc:date',
|
||||
]),
|
||||
'source_protocol_info': list([
|
||||
'http-get:*:audio/mpeg:*',
|
||||
'http-get:*:video/mp4:*',
|
||||
]),
|
||||
'system_update_id': 42,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_diagnostics_disconnected
|
||||
dict({
|
||||
'available': False,
|
||||
'device': None,
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Tests for the DLNA DMS diagnostics."""
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
from async_upnp_client.exceptions import UpnpConnectionError
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from pytest_homeassistant.typing import ClientSessionGenerator
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def connected_device(dms_device_mock: Mock) -> Mock:
|
||||
"""Populate the DMS device mock with realistic values."""
|
||||
profile_device = dms_device_mock.profile_device
|
||||
profile_device.manufacturer = "device_manufacturer"
|
||||
profile_device.model_name = "device_model_name"
|
||||
profile_device.model_number = "1.0"
|
||||
profile_device.model_description = "Test media server"
|
||||
actions = {
|
||||
"urn:schemas-upnp-org:service:ContentDirectory:1": [
|
||||
"Browse",
|
||||
"GetSearchCapabilities",
|
||||
"GetSortCapabilities",
|
||||
"Search",
|
||||
],
|
||||
"urn:schemas-upnp-org:service:ConnectionManager:1": ["GetProtocolInfo"],
|
||||
}
|
||||
for service_type, service in profile_device.services.items():
|
||||
service.actions = dict.fromkeys(actions[service_type])
|
||||
|
||||
dms_device_mock.search_capabilities = ["dc:title", "upnp:class"]
|
||||
dms_device_mock.sort_capabilities = ["dc:title", "dc:date"]
|
||||
dms_device_mock.source_protocol_info = [
|
||||
"http-get:*:audio/mpeg:*",
|
||||
"http-get:*:video/mp4:*",
|
||||
]
|
||||
dms_device_mock.system_update_id = 42
|
||||
return dms_device_mock
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("connected_device")
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
config_entry_mock: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics for a connected device."""
|
||||
config_entry_mock.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry_mock.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry_mock)
|
||||
== snapshot
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("dms_device_mock")
|
||||
async def test_diagnostics_disconnected(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
upnp_factory_mock: Mock,
|
||||
config_entry_mock: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics when the device is not connected."""
|
||||
upnp_factory_mock.async_create_device.side_effect = UpnpConnectionError
|
||||
config_entry_mock.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry_mock.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry_mock)
|
||||
== snapshot
|
||||
)
|
||||
Reference in New Issue
Block a user