* Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
121 lines
4.7 KiB
Python
121 lines
4.7 KiB
Python
"""Tests for the DLNA DMR data module."""
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
from unittest.mock import ANY, Mock, patch
|
|
|
|
from async_upnp_client import UpnpEventHandler
|
|
from async_upnp_client.aiohttp import AiohttpNotifyServer
|
|
import pytest
|
|
|
|
from homeassistant.components.dlna_dmr.const import DOMAIN
|
|
from homeassistant.components.dlna_dmr.data import EventListenAddr, get_domain_data
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|
from homeassistant.core import Event, HomeAssistant
|
|
|
|
|
|
@pytest.fixture
|
|
def aiohttp_notify_servers_mock() -> Iterable[Mock]:
|
|
"""Construct mock AiohttpNotifyServer on demand, eliminating network use.
|
|
|
|
This fixture provides a list of the constructed servers.
|
|
"""
|
|
with patch(
|
|
"homeassistant.components.dlna_dmr.data.AiohttpNotifyServer"
|
|
) as mock_constructor:
|
|
servers = []
|
|
|
|
def make_server(*_args, **_kwargs):
|
|
server = Mock(spec=AiohttpNotifyServer)
|
|
servers.append(server)
|
|
server.event_handler = Mock(spec=UpnpEventHandler)
|
|
return server
|
|
|
|
mock_constructor.side_effect = make_server
|
|
|
|
yield mock_constructor
|
|
|
|
# Every server must be stopped if it was started
|
|
for server in servers:
|
|
assert server.start_server.call_count == server.stop_server.call_count
|
|
|
|
|
|
async def test_get_domain_data(hass: HomeAssistant) -> None:
|
|
"""Test the get_domain_data function returns the same data every time."""
|
|
assert DOMAIN not in hass.data
|
|
domain_data = get_domain_data(hass)
|
|
assert domain_data is not None
|
|
assert get_domain_data(hass) is domain_data
|
|
|
|
|
|
async def test_event_notifier(
|
|
hass: HomeAssistant, aiohttp_notify_servers_mock: Mock
|
|
) -> None:
|
|
"""Test getting and releasing event notifiers."""
|
|
domain_data = get_domain_data(hass)
|
|
|
|
listen_addr = EventListenAddr(None, 0, None)
|
|
event_notifier = await domain_data.async_get_event_notifier(listen_addr, hass)
|
|
assert event_notifier is not None
|
|
|
|
# Check that the parameters were passed through to the AiohttpNotifyServer
|
|
aiohttp_notify_servers_mock.assert_called_with(
|
|
requester=ANY, listen_port=0, listen_host=None, callback_url=None, loop=ANY
|
|
)
|
|
|
|
# Same address should give same notifier
|
|
listen_addr_2 = EventListenAddr(None, 0, None)
|
|
event_notifier_2 = await domain_data.async_get_event_notifier(listen_addr_2, hass)
|
|
assert event_notifier_2 is event_notifier
|
|
|
|
# Different address should give different notifier
|
|
listen_addr_3 = EventListenAddr(
|
|
"192.88.99.4", 9999, "http://192.88.99.4:9999/notify"
|
|
)
|
|
event_notifier_3 = await domain_data.async_get_event_notifier(listen_addr_3, hass)
|
|
assert event_notifier_3 is not None
|
|
assert event_notifier_3 is not event_notifier
|
|
|
|
# Check that the parameters were passed through to the AiohttpNotifyServer
|
|
aiohttp_notify_servers_mock.assert_called_with(
|
|
requester=ANY,
|
|
listen_port=9999,
|
|
listen_host="192.88.99.4",
|
|
callback_url="http://192.88.99.4:9999/notify",
|
|
loop=ANY,
|
|
)
|
|
|
|
# There should be 2 notifiers total, one with 2 references, and a stop callback
|
|
assert set(domain_data.event_notifiers.keys()) == {listen_addr, listen_addr_3}
|
|
assert domain_data.event_notifier_refs == {listen_addr: 2, listen_addr_3: 1}
|
|
assert domain_data.stop_listener_remove is not None
|
|
|
|
# Releasing notifiers should delete them when they have not more references
|
|
await domain_data.async_release_event_notifier(listen_addr)
|
|
assert set(domain_data.event_notifiers.keys()) == {listen_addr, listen_addr_3}
|
|
assert domain_data.event_notifier_refs == {listen_addr: 1, listen_addr_3: 1}
|
|
assert domain_data.stop_listener_remove is not None
|
|
|
|
await domain_data.async_release_event_notifier(listen_addr)
|
|
assert set(domain_data.event_notifiers.keys()) == {listen_addr_3}
|
|
assert domain_data.event_notifier_refs == {listen_addr: 0, listen_addr_3: 1}
|
|
assert domain_data.stop_listener_remove is not None
|
|
|
|
await domain_data.async_release_event_notifier(listen_addr_3)
|
|
assert set(domain_data.event_notifiers.keys()) == set()
|
|
assert domain_data.event_notifier_refs == {listen_addr: 0, listen_addr_3: 0}
|
|
assert domain_data.stop_listener_remove is None
|
|
|
|
|
|
async def test_cleanup_event_notifiers(hass: HomeAssistant) -> None:
|
|
"""Test cleanup function clears all event notifiers."""
|
|
domain_data = get_domain_data(hass)
|
|
await domain_data.async_get_event_notifier(EventListenAddr(None, 0, None), hass)
|
|
await domain_data.async_get_event_notifier(
|
|
EventListenAddr(None, 0, "different"), hass
|
|
)
|
|
|
|
await domain_data.async_cleanup_event_notifiers(Event(EVENT_HOMEASSISTANT_STOP))
|
|
|
|
assert not domain_data.event_notifiers
|
|
assert not domain_data.event_notifier_refs
|