2020-05-03 01:03:54 +00:00
|
|
|
"""Test UPnP/IGD config flow."""
|
|
|
|
|
2020-05-11 18:03:12 +00:00
|
|
|
from datetime import timedelta
|
2021-08-13 16:13:25 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2020-05-10 02:52:08 +00:00
|
|
|
|
2020-05-03 01:03:54 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
|
|
|
from homeassistant.components import ssdp
|
|
|
|
from homeassistant.components.upnp.const import (
|
2021-02-21 02:26:17 +00:00
|
|
|
CONFIG_ENTRY_HOSTNAME,
|
2020-05-10 02:52:08 +00:00
|
|
|
CONFIG_ENTRY_SCAN_INTERVAL,
|
|
|
|
CONFIG_ENTRY_ST,
|
|
|
|
CONFIG_ENTRY_UDN,
|
|
|
|
DEFAULT_SCAN_INTERVAL,
|
2020-05-03 01:03:54 +00:00
|
|
|
DOMAIN,
|
|
|
|
)
|
2021-08-13 16:13:25 +00:00
|
|
|
from homeassistant.core import CoreState, HomeAssistant
|
2020-05-11 18:03:12 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-04-14 21:39:44 +00:00
|
|
|
from homeassistant.util import dt
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
from .common import (
|
|
|
|
TEST_DISCOVERY,
|
|
|
|
TEST_FRIENDLY_NAME,
|
|
|
|
TEST_HOSTNAME,
|
|
|
|
TEST_LOCATION,
|
|
|
|
TEST_ST,
|
|
|
|
TEST_UDN,
|
|
|
|
TEST_USN,
|
|
|
|
)
|
|
|
|
from .mock_ssdp_scanner import mock_ssdp_scanner # noqa: F401
|
|
|
|
from .mock_upnp_device import mock_upnp_device # noqa: F401
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-04-14 21:39:44 +00:00
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_upnp_device", "mock_get_source_ip")
|
2021-08-13 16:13:25 +00:00
|
|
|
async def test_flow_ssdp_discovery(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
):
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Test config flow: discovered + configured through ssdp."""
|
2021-08-13 16:13:25 +00:00
|
|
|
# Ensure we have a ssdp Scanner.
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
ssdp_scanner: ssdp.Scanner = hass.data[ssdp.DOMAIN]
|
|
|
|
ssdp_scanner.cache[(TEST_UDN, TEST_ST)] = TEST_DISCOVERY
|
|
|
|
# Speed up callback in ssdp.async_register_callback.
|
|
|
|
hass.state = CoreState.not_running
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# Discovered via step ssdp.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
|
|
data=TEST_DISCOVERY,
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "ssdp_confirm"
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# 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"] == TEST_FRIENDLY_NAME
|
|
|
|
assert result["data"] == {
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
|
|
|
}
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_flow_ssdp_incomplete_discovery(hass: HomeAssistant):
|
2020-05-14 20:58:41 +00:00
|
|
|
"""Test config flow: incomplete discovery through ssdp."""
|
2021-01-29 09:23:34 +00:00
|
|
|
# Discovered via step ssdp.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
|
|
data={
|
2021-08-13 16:13:25 +00:00
|
|
|
ssdp.ATTR_SSDP_LOCATION: TEST_LOCATION,
|
|
|
|
ssdp.ATTR_SSDP_ST: TEST_ST,
|
|
|
|
ssdp.ATTR_SSDP_USN: TEST_USN,
|
|
|
|
# ssdp.ATTR_UPNP_UDN: TEST_UDN, # Not provided.
|
2021-01-29 09:23:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "incomplete_discovery"
|
2020-05-14 20:58:41 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_flow_ssdp_discovery_ignored(hass: HomeAssistant):
|
2021-07-21 01:46:33 +00:00
|
|
|
"""Test config flow: discovery through ssdp, but ignored, as hostname is used by existing config entry."""
|
2021-02-21 02:26:17 +00:00
|
|
|
# Existing entry.
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
2021-08-13 16:13:25 +00:00
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN + "2",
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
2021-02-21 02:26:17 +00:00
|
|
|
},
|
|
|
|
options={CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2021-07-21 01:46:33 +00:00
|
|
|
# Discovered via step ssdp, but ignored.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
2021-08-13 16:13:25 +00:00
|
|
|
data=TEST_DISCOVERY,
|
2021-07-21 01:46:33 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "discovery_ignored"
|
2021-02-21 02:26:17 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_upnp_device", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_flow_user(hass: HomeAssistant):
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Test config flow: discovered + configured through user."""
|
2021-08-13 16:13:25 +00:00
|
|
|
# Ensure we have a ssdp Scanner.
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
ssdp_scanner: ssdp.Scanner = hass.data[ssdp.DOMAIN]
|
|
|
|
ssdp_scanner.cache[(TEST_UDN, TEST_ST)] = TEST_DISCOVERY
|
|
|
|
# Speed up callback in ssdp.async_register_callback.
|
|
|
|
hass.state = CoreState.not_running
|
|
|
|
|
|
|
|
# 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"
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# Confirmed via step user.
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={"unique_id": TEST_USN},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == TEST_FRIENDLY_NAME
|
|
|
|
assert result["data"] == {
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
|
|
|
}
|
2020-05-10 02:52:08 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_upnp_device", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_flow_import(hass: HomeAssistant):
|
2021-08-13 16:13:25 +00:00
|
|
|
"""Test config flow: configured through configuration.yaml."""
|
|
|
|
# Ensure we have a ssdp Scanner.
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
ssdp_scanner: ssdp.Scanner = hass.data[ssdp.DOMAIN]
|
|
|
|
ssdp_scanner.cache[(TEST_UDN, TEST_ST)] = TEST_DISCOVERY
|
|
|
|
# Speed up callback in ssdp.async_register_callback.
|
|
|
|
hass.state = CoreState.not_running
|
2020-05-10 02:52:08 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# 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"] == TEST_FRIENDLY_NAME
|
|
|
|
assert result["data"] == {
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
|
|
|
}
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_flow_import_already_configured(hass: HomeAssistant):
|
2021-08-13 16:13:25 +00:00
|
|
|
"""Test config flow: configured through configuration.yaml, but existing config entry."""
|
2020-10-06 11:57:36 +00:00
|
|
|
# Existing entry.
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
2021-08-13 16:13:25 +00:00
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN,
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
2020-10-06 11:57:36 +00:00
|
|
|
},
|
|
|
|
options={CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
# Discovered via step import.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
2020-10-06 11:57:36 +00:00
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
2020-10-06 11:57:36 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_get_source_ip")
|
2021-06-08 20:32:06 +00:00
|
|
|
async def test_flow_import_no_devices_found(hass: HomeAssistant):
|
|
|
|
"""Test config flow: no devices found, configured through configuration.yaml."""
|
2021-08-13 16:13:25 +00:00
|
|
|
# Ensure we have a ssdp Scanner.
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
ssdp_scanner: ssdp.Scanner = hass.data[ssdp.DOMAIN]
|
|
|
|
ssdp_scanner.cache.clear()
|
|
|
|
|
|
|
|
# Discovered via step import.
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.upnp.config_flow.SSDP_SEARCH_TIMEOUT", new=0.0
|
2021-06-08 20:32:06 +00:00
|
|
|
):
|
2020-10-06 11:57:36 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2021-06-08 20:32:06 +00:00
|
|
|
assert result["reason"] == "no_devices_found"
|
2020-10-06 11:57:36 +00:00
|
|
|
|
|
|
|
|
2021-09-02 18:44:50 +00:00
|
|
|
@pytest.mark.usefixtures("mock_ssdp_scanner", "mock_upnp_device", "mock_get_source_ip")
|
2021-04-22 14:53:57 +00:00
|
|
|
async def test_options_flow(hass: HomeAssistant):
|
2020-05-11 18:03:12 +00:00
|
|
|
"""Test options flow."""
|
2021-08-13 16:13:25 +00:00
|
|
|
# Ensure we have a ssdp Scanner.
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
ssdp_scanner: ssdp.Scanner = hass.data[ssdp.DOMAIN]
|
|
|
|
ssdp_scanner.cache[(TEST_UDN, TEST_ST)] = TEST_DISCOVERY
|
|
|
|
# Speed up callback in ssdp.async_register_callback.
|
|
|
|
hass.state = CoreState.not_running
|
|
|
|
|
2020-05-11 18:03:12 +00:00
|
|
|
# Set up config entry.
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
2021-08-13 16:13:25 +00:00
|
|
|
CONFIG_ENTRY_UDN: TEST_UDN,
|
|
|
|
CONFIG_ENTRY_ST: TEST_ST,
|
|
|
|
CONFIG_ENTRY_HOSTNAME: TEST_HOSTNAME,
|
2020-05-11 18:03:12 +00:00
|
|
|
},
|
|
|
|
options={CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
2021-08-13 16:13:25 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
|
|
|
await hass.async_block_till_done()
|
2021-08-17 18:23:41 +00:00
|
|
|
mock_device = hass.data[DOMAIN][config_entry.entry_id].device
|
2021-08-13 16:13:25 +00:00
|
|
|
|
|
|
|
# Reset.
|
2021-08-17 18:23:41 +00:00
|
|
|
mock_device.traffic_times_polled = 0
|
|
|
|
mock_device.status_times_polled = 0
|
2021-08-13 16:13:25 +00:00
|
|
|
|
|
|
|
# Forward time, ensure single poll after 30 (default) seconds.
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=31))
|
|
|
|
await hass.async_block_till_done()
|
2021-08-17 18:23:41 +00:00
|
|
|
assert mock_device.traffic_times_polled == 1
|
|
|
|
assert mock_device.status_times_polled == 1
|
2020-05-11 18:03:12 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# Options flow with no input results in form.
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
config_entry.entry_id,
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Options flow with input results in update to entry.
|
|
|
|
result2 = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONFIG_ENTRY_SCAN_INTERVAL: 60},
|
|
|
|
)
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert config_entry.options == {
|
|
|
|
CONFIG_ENTRY_SCAN_INTERVAL: 60,
|
2020-05-11 18:03:12 +00:00
|
|
|
}
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# Forward time, ensure single poll after 60 seconds, still from original setting.
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=61))
|
|
|
|
await hass.async_block_till_done()
|
2021-08-17 18:23:41 +00:00
|
|
|
assert mock_device.traffic_times_polled == 2
|
|
|
|
assert mock_device.status_times_polled == 2
|
2021-08-13 16:13:25 +00:00
|
|
|
|
|
|
|
# Now the updated interval takes effect.
|
|
|
|
# Forward time, ensure single poll after 120 seconds.
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=121))
|
|
|
|
await hass.async_block_till_done()
|
2021-08-17 18:23:41 +00:00
|
|
|
assert mock_device.traffic_times_polled == 3
|
|
|
|
assert mock_device.status_times_polled == 3
|
2021-08-13 16:13:25 +00:00
|
|
|
|
|
|
|
# Forward time, ensure single poll after 180 seconds.
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=181))
|
|
|
|
await hass.async_block_till_done()
|
2021-08-17 18:23:41 +00:00
|
|
|
assert mock_device.traffic_times_polled == 4
|
|
|
|
assert mock_device.status_times_polled == 4
|