hass-core/tests/components/upnp/test_init.py

128 lines
4.2 KiB
Python
Raw Normal View History

2018-09-17 22:08:09 +02:00
"""Test UPnP/IGD setup process."""
2018-08-29 21:19:04 +02:00
2021-01-01 22:31:56 +01:00
from unittest.mock import AsyncMock, patch
from homeassistant.components.upnp.const import (
2021-01-29 10:23:34 +01:00
CONFIG_ENTRY_ST,
CONFIG_ENTRY_UDN,
DISCOVERY_HOSTNAME,
DISCOVERY_LOCATION,
2021-01-29 10:23:34 +01:00
DISCOVERY_NAME,
DISCOVERY_ST,
DISCOVERY_UDN,
2021-01-29 10:23:34 +01:00
DISCOVERY_UNIQUE_ID,
DISCOVERY_USN,
DOMAIN,
)
2018-09-17 22:08:09 +02:00
from homeassistant.components.upnp.device import Device
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.setup import async_setup_component
2018-08-29 21:19:04 +02:00
from .mock_device import MockDevice
2018-08-29 21:19:04 +02:00
from tests.common import MockConfigEntry
2018-09-08 00:11:23 +02:00
async def test_async_setup_entry_default(hass: HomeAssistantType):
2018-08-29 21:19:04 +02:00
"""Test async_setup_entry."""
2019-07-31 12:25:30 -07:00
udn = "uuid:device_1"
2021-01-29 10:23:34 +01:00
location = "http://192.168.1.1/desc.xml"
mock_device = MockDevice(udn)
2021-01-29 10:23:34 +01:00
discoveries = [
{
2021-01-29 10:23:34 +01:00
DISCOVERY_LOCATION: location,
DISCOVERY_NAME: mock_device.name,
DISCOVERY_ST: mock_device.device_type,
2021-01-29 10:23:34 +01:00
DISCOVERY_UDN: mock_device.udn,
DISCOVERY_UNIQUE_ID: mock_device.unique_id,
DISCOVERY_USN: mock_device.usn,
DISCOVERY_HOSTNAME: mock_device.hostname,
}
]
entry = MockConfigEntry(
domain=DOMAIN,
2021-01-29 10:23:34 +01:00
data={
CONFIG_ENTRY_UDN: mock_device.udn,
CONFIG_ENTRY_ST: mock_device.device_type,
},
)
2018-08-30 16:38:43 +02:00
config = {
# no upnp
}
async_create_device = AsyncMock(return_value=mock_device)
2021-01-29 10:23:34 +01:00
async_discover = AsyncMock()
with patch.object(Device, "async_create_device", async_create_device), patch.object(
Device, "async_discover", async_discover
):
# initialisation of component, no device discovered
2021-01-29 10:23:34 +01:00
async_discover.return_value = []
await async_setup_component(hass, "upnp", config)
await hass.async_block_till_done()
# loading of config_entry, device discovered
async_discover.return_value = discoveries
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id) is True
2021-01-29 10:23:34 +01:00
# ensure device is stored/used
async_create_device.assert_called_with(hass, discoveries[0][DISCOVERY_LOCATION])
2021-01-29 10:23:34 +01:00
async def test_sync_setup_entry_multiple_discoveries(hass: HomeAssistantType):
2021-01-29 10:23:34 +01:00
"""Test async_setup_entry."""
udn_0 = "uuid:device_1"
location_0 = "http://192.168.1.1/desc.xml"
mock_device_0 = MockDevice(udn_0)
udn_1 = "uuid:device_2"
location_1 = "http://192.168.1.2/desc.xml"
mock_device_1 = MockDevice(udn_1)
discoveries = [
{
DISCOVERY_LOCATION: location_0,
DISCOVERY_NAME: mock_device_0.name,
DISCOVERY_ST: mock_device_0.device_type,
DISCOVERY_UDN: mock_device_0.udn,
DISCOVERY_UNIQUE_ID: mock_device_0.unique_id,
DISCOVERY_USN: mock_device_0.usn,
DISCOVERY_HOSTNAME: mock_device_0.hostname,
2021-01-29 10:23:34 +01:00
},
{
DISCOVERY_LOCATION: location_1,
DISCOVERY_NAME: mock_device_1.name,
DISCOVERY_ST: mock_device_1.device_type,
DISCOVERY_UDN: mock_device_1.udn,
DISCOVERY_UNIQUE_ID: mock_device_1.unique_id,
DISCOVERY_USN: mock_device_1.usn,
DISCOVERY_HOSTNAME: mock_device_1.hostname,
2021-01-29 10:23:34 +01:00
},
]
entry = MockConfigEntry(
domain=DOMAIN,
2021-01-29 10:23:34 +01:00
data={
CONFIG_ENTRY_UDN: mock_device_1.udn,
CONFIG_ENTRY_ST: mock_device_1.device_type,
},
)
config = {
# no upnp
}
async_create_device = AsyncMock(return_value=mock_device_1)
async_discover = AsyncMock()
with patch.object(Device, "async_create_device", async_create_device), patch.object(
Device, "async_discover", async_discover
):
# initialisation of component, no device discovered
async_discover.return_value = []
2019-07-31 12:25:30 -07:00
await async_setup_component(hass, "upnp", config)
await hass.async_block_till_done()
2018-08-29 21:19:04 +02:00
# loading of config_entry, device discovered
2021-01-29 10:23:34 +01:00
async_discover.return_value = discoveries
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id) is True
2018-08-30 16:38:43 +02:00
# ensure device is stored/used
2021-01-29 10:23:34 +01:00
async_create_device.assert_called_with(hass, discoveries[1][DISCOVERY_LOCATION])