Improve UPnP configuration flow (#34737)
This commit is contained in:
1 parent
aeb891649e
commit
6afb42bf7a
12 files changed
+514
-139
No files matched your search
@@ -0,0 +1,77 @@
|
||||
"""Mock device for testing purposes."""
|
||||
|
||||
from typing import Mapping
|
||||
|
||||
from homeassistant.components.upnp.const import (
|
||||
BYTES_RECEIVED,
|
||||
BYTES_SENT,
|
||||
PACKETS_RECEIVED,
|
||||
PACKETS_SENT,
|
||||
TIMESTAMP,
|
||||
)
|
||||
from homeassistant.components.upnp.device import Device
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
|
||||
class MockDevice(Device):
|
||||
"""Mock device for Device."""
|
||||
|
||||
def __init__(self, udn):
|
||||
"""Initialize mock device."""
|
||||
igd_device = object()
|
||||
super().__init__(igd_device)
|
||||
self._udn = udn
|
||||
self.added_port_mappings = []
|
||||
self.removed_port_mappings = []
|
||||
|
||||
@classmethod
|
||||
async def async_create_device(cls, hass, ssdp_location):
|
||||
"""Return self."""
|
||||
return cls("UDN")
|
||||
|
||||
@property
|
||||
def udn(self) -> str:
|
||||
"""Get the UDN."""
|
||||
return self._udn
|
||||
|
||||
@property
|
||||
def manufacturer(self) -> str:
|
||||
"""Get manufacturer."""
|
||||
return "mock-manufacturer"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Get name."""
|
||||
return "mock-name"
|
||||
|
||||
@property
|
||||
def model_name(self) -> str:
|
||||
"""Get the model name."""
|
||||
return "mock-model-name"
|
||||
|
||||
@property
|
||||
def device_type(self) -> str:
|
||||
"""Get the device type."""
|
||||
return "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
||||
|
||||
async def _async_add_port_mapping(
|
||||
self, external_port: int, local_ip: str, internal_port: int
|
||||
) -> None:
|
||||
"""Add a port mapping."""
|
||||
entry = [external_port, local_ip, internal_port]
|
||||
self.added_port_mappings.append(entry)
|
||||
|
||||
async def _async_delete_port_mapping(self, external_port: int) -> None:
|
||||
"""Remove a port mapping."""
|
||||
entry = external_port
|
||||
self.removed_port_mappings.append(entry)
|
||||
|
||||
async def async_get_traffic_data(self) -> Mapping[str, any]:
|
||||
"""Get traffic data."""
|
||||
return {
|
||||
TIMESTAMP: dt_util.utcnow(),
|
||||
BYTES_RECEIVED: 0,
|
||||
BYTES_SENT: 0,
|
||||
PACKETS_RECEIVED: 0,
|
||||
PACKETS_SENT: 0,
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Test UPnP/IGD config flow."""
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.components import ssdp
|
||||
from homeassistant.components.upnp.const import (
|
||||
DISCOVERY_LOCATION,
|
||||
DISCOVERY_ST,
|
||||
DISCOVERY_UDN,
|
||||
DISCOVERY_USN,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.upnp.device import Device
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from .mock_device import MockDevice
|
||||
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
|
||||
|
||||
async def test_flow_ssdp_discovery(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through ssdp."""
|
||||
udn = "uuid:device_1"
|
||||
mock_device = MockDevice(udn)
|
||||
discovery_infos = [
|
||||
{
|
||||
DISCOVERY_ST: mock_device.device_type,
|
||||
DISCOVERY_UDN: mock_device.udn,
|
||||
DISCOVERY_LOCATION: "dummy",
|
||||
}
|
||||
]
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", AsyncMock(return_value=discovery_infos)):
|
||||
# Discovered via step ssdp.
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_SSDP},
|
||||
data={
|
||||
ssdp.ATTR_SSDP_ST: mock_device.device_type,
|
||||
ssdp.ATTR_UPNP_UDN: mock_device.udn,
|
||||
"friendlyName": mock_device.name,
|
||||
},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "ssdp_confirm"
|
||||
|
||||
# Confirm via step ssdp_confirm.
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == mock_device.name
|
||||
assert result["data"] == {
|
||||
"st": mock_device.device_type,
|
||||
"udn": mock_device.udn,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_user(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through user."""
|
||||
udn = "uuid:device_1"
|
||||
mock_device = MockDevice(udn)
|
||||
usn = f"{mock_device.udn}::{mock_device.device_type}"
|
||||
discovery_infos = [
|
||||
{
|
||||
DISCOVERY_USN: usn,
|
||||
DISCOVERY_ST: mock_device.device_type,
|
||||
DISCOVERY_UDN: mock_device.udn,
|
||||
DISCOVERY_LOCATION: "dummy",
|
||||
}
|
||||
]
|
||||
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", AsyncMock(return_value=discovery_infos)):
|
||||
# Discovered via step user.
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# Confirmed via step user.
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={"usn": usn},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == mock_device.name
|
||||
assert result["data"] == {
|
||||
"st": mock_device.device_type,
|
||||
"udn": mock_device.udn,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_config(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through configuration.yaml."""
|
||||
udn = "uuid:device_1"
|
||||
mock_device = MockDevice(udn)
|
||||
usn = f"{mock_device.udn}::{mock_device.device_type}"
|
||||
discovery_infos = [
|
||||
{
|
||||
DISCOVERY_USN: usn,
|
||||
DISCOVERY_ST: mock_device.device_type,
|
||||
DISCOVERY_UDN: mock_device.udn,
|
||||
DISCOVERY_LOCATION: "dummy",
|
||||
}
|
||||
]
|
||||
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", AsyncMock(return_value=discovery_infos)):
|
||||
# Discovered via step import.
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == mock_device.name
|
||||
assert result["data"] == {
|
||||
"st": mock_device.device_type,
|
||||
"udn": mock_device.udn,
|
||||
}
|
||||
@@ -3,91 +3,49 @@
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from homeassistant.components import upnp
|
||||
from homeassistant.components.upnp.const import (
|
||||
DISCOVERY_LOCATION,
|
||||
DISCOVERY_ST,
|
||||
DISCOVERY_UDN,
|
||||
)
|
||||
from homeassistant.components.upnp.device import Device
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, mock_coro
|
||||
from .mock_device import MockDevice
|
||||
|
||||
|
||||
class MockDevice(Device):
|
||||
"""Mock device for Device."""
|
||||
|
||||
def __init__(self, udn):
|
||||
"""Initialize mock device."""
|
||||
igd_device = object()
|
||||
super().__init__(igd_device)
|
||||
self._udn = udn
|
||||
self.added_port_mappings = []
|
||||
self.removed_port_mappings = []
|
||||
|
||||
@classmethod
|
||||
async def async_create_device(cls, hass, ssdp_description):
|
||||
"""Return self."""
|
||||
return cls("UDN")
|
||||
|
||||
@property
|
||||
def udn(self) -> str:
|
||||
"""Get the UDN."""
|
||||
return self._udn
|
||||
|
||||
@property
|
||||
def manufacturer(self) -> str:
|
||||
"""Get manufacturer."""
|
||||
return "mock-manufacturer"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Get name."""
|
||||
return "mock-name"
|
||||
|
||||
@property
|
||||
def model_name(self) -> str:
|
||||
"""Get the model name."""
|
||||
return "mock-model-name"
|
||||
|
||||
@property
|
||||
def device_type(self) -> str:
|
||||
"""Get the device type."""
|
||||
return "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
||||
|
||||
async def _async_add_port_mapping(
|
||||
self, external_port: int, local_ip: str, internal_port: int
|
||||
) -> None:
|
||||
"""Add a port mapping."""
|
||||
entry = [external_port, local_ip, internal_port]
|
||||
self.added_port_mappings.append(entry)
|
||||
|
||||
async def _async_delete_port_mapping(self, external_port: int) -> None:
|
||||
"""Remove a port mapping."""
|
||||
entry = external_port
|
||||
self.removed_port_mappings.append(entry)
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_async_setup_entry_default(hass):
|
||||
"""Test async_setup_entry."""
|
||||
udn = "uuid:device_1"
|
||||
entry = MockConfigEntry(domain=upnp.DOMAIN)
|
||||
mock_device = MockDevice(udn)
|
||||
discovery_infos = [
|
||||
{
|
||||
DISCOVERY_UDN: mock_device.udn,
|
||||
DISCOVERY_ST: mock_device.device_type,
|
||||
DISCOVERY_LOCATION: "http://192.168.1.1/desc.xml",
|
||||
}
|
||||
]
|
||||
entry = MockConfigEntry(
|
||||
domain=upnp.DOMAIN, data={"udn": mock_device.udn, "st": mock_device.device_type}
|
||||
)
|
||||
|
||||
config = {
|
||||
# no upnp
|
||||
}
|
||||
with patch.object(Device, "async_create_device") as create_device, patch.object(
|
||||
Device, "async_discover", return_value=mock_coro([])
|
||||
) as async_discover:
|
||||
async_discover = AsyncMock(return_value=[])
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", async_discover):
|
||||
# initialisation of component, no device discovered
|
||||
await async_setup_component(hass, "upnp", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# mock homeassistant.components.upnp.device.Device
|
||||
mock_device = MockDevice(udn)
|
||||
discovery_infos = [
|
||||
{"udn": udn, "ssdp_description": "http://192.168.1.1/desc.xml"}
|
||||
]
|
||||
|
||||
create_device.return_value = mock_device
|
||||
# loading of config_entry, device discovered
|
||||
async_discover.return_value = discovery_infos
|
||||
|
||||
assert await upnp.async_setup_entry(hass, entry) is True
|
||||
|
||||
# ensure device is stored/used
|
||||
@@ -105,7 +63,17 @@ async def test_async_setup_entry_port_mapping(hass):
|
||||
"""Test async_setup_entry."""
|
||||
# pylint: disable=invalid-name
|
||||
udn = "uuid:device_1"
|
||||
entry = MockConfigEntry(domain=upnp.DOMAIN)
|
||||
mock_device = MockDevice(udn)
|
||||
discovery_infos = [
|
||||
{
|
||||
DISCOVERY_UDN: mock_device.udn,
|
||||
DISCOVERY_ST: mock_device.device_type,
|
||||
DISCOVERY_LOCATION: "http://192.168.1.1/desc.xml",
|
||||
}
|
||||
]
|
||||
entry = MockConfigEntry(
|
||||
domain=upnp.DOMAIN, data={"udn": mock_device.udn, "st": mock_device.device_type}
|
||||
)
|
||||
|
||||
config = {
|
||||
"http": {},
|
||||
@@ -115,21 +83,17 @@ async def test_async_setup_entry_port_mapping(hass):
|
||||
"ports": {"hass": "hass"},
|
||||
},
|
||||
}
|
||||
with patch.object(Device, "async_create_device") as create_device, patch.object(
|
||||
Device, "async_discover", return_value=mock_coro([])
|
||||
) as async_discover:
|
||||
async_discover = AsyncMock(return_value=[])
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", async_discover):
|
||||
# initialisation of component, no device discovered
|
||||
await async_setup_component(hass, "http", config)
|
||||
await async_setup_component(hass, "upnp", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_device = MockDevice(udn)
|
||||
discovery_infos = [
|
||||
{"udn": udn, "ssdp_description": "http://192.168.1.1/desc.xml"}
|
||||
]
|
||||
|
||||
create_device.return_value = mock_device
|
||||
# loading of config_entry, device discovered
|
||||
async_discover.return_value = discovery_infos
|
||||
|
||||
assert await upnp.async_setup_entry(hass, entry) is True
|
||||
|
||||
# ensure device is stored/used
|
||||
|
||||
Reference in new issue
Block a user