Move upnp update_interval options flow (#35474)
This commit is contained in:
parent
d2502f649f
commit
48899c7a1c
4 changed files with 113 additions and 96 deletions
|
@ -1,7 +1,6 @@
|
|||
"""Test UPnP/IGD config flow."""
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.components import ssdp
|
||||
|
@ -18,10 +17,12 @@ from homeassistant.components.upnp.const import (
|
|||
)
|
||||
from homeassistant.components.upnp.device import Device
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .mock_device import MockDevice
|
||||
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_flow_ssdp_discovery(hass: HomeAssistantType):
|
||||
|
@ -61,7 +62,6 @@ async def test_flow_ssdp_discovery(hass: HomeAssistantType):
|
|||
assert result["data"] == {
|
||||
CONFIG_ENTRY_ST: mock_device.device_type,
|
||||
CONFIG_ENTRY_UDN: mock_device.udn,
|
||||
CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
|
||||
}
|
||||
|
||||
|
||||
|
@ -99,83 +99,9 @@ async def test_flow_user(hass: HomeAssistantType):
|
|||
assert result["data"] == {
|
||||
CONFIG_ENTRY_ST: mock_device.device_type,
|
||||
CONFIG_ENTRY_UDN: mock_device.udn,
|
||||
CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_user_update_interval(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through user with non-default scan_interval."""
|
||||
udn = "uuid:device_1"
|
||||
mock_device = MockDevice(udn)
|
||||
usn = f"{mock_device.udn}::{mock_device.device_type}"
|
||||
scan_interval = 60
|
||||
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, CONFIG_ENTRY_SCAN_INTERVAL: scan_interval},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == mock_device.name
|
||||
assert result["data"] == {
|
||||
CONFIG_ENTRY_ST: mock_device.device_type,
|
||||
CONFIG_ENTRY_UDN: mock_device.udn,
|
||||
CONFIG_ENTRY_SCAN_INTERVAL: scan_interval,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_user_update_interval_min_30(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through user with non-default scan_interval."""
|
||||
udn = "uuid:device_1"
|
||||
mock_device = MockDevice(udn)
|
||||
usn = f"{mock_device.udn}::{mock_device.device_type}"
|
||||
scan_interval = 15
|
||||
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.
|
||||
with pytest.raises(vol.error.MultipleInvalid):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"usn": usn, CONFIG_ENTRY_SCAN_INTERVAL: scan_interval},
|
||||
)
|
||||
|
||||
|
||||
async def test_flow_config(hass: HomeAssistantType):
|
||||
"""Test config flow: discovered + configured through configuration.yaml."""
|
||||
udn = "uuid:device_1"
|
||||
|
@ -203,5 +129,58 @@ async def test_flow_config(hass: HomeAssistantType):
|
|||
assert result["data"] == {
|
||||
CONFIG_ENTRY_ST: mock_device.device_type,
|
||||
CONFIG_ENTRY_UDN: mock_device.udn,
|
||||
CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow(hass: HomeAssistantType):
|
||||
"""Test options flow."""
|
||||
# Set up config entry.
|
||||
udn = "uuid:device_1"
|
||||
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",
|
||||
}
|
||||
]
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONFIG_ENTRY_UDN: mock_device.udn,
|
||||
CONFIG_ENTRY_ST: mock_device.device_type,
|
||||
},
|
||||
options={CONFIG_ENTRY_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
config = {
|
||||
# no upnp, ensures no import-flow is started.
|
||||
}
|
||||
async_discover = AsyncMock(return_value=discovery_infos)
|
||||
with patch.object(
|
||||
Device, "async_create_device", AsyncMock(return_value=mock_device)
|
||||
), patch.object(Device, "async_discover", async_discover):
|
||||
# Initialisation of component.
|
||||
await async_setup_component(hass, "upnp", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# DataUpdateCoordinator gets a default of 30 seconds for updates.
|
||||
coordinator = hass.data[DOMAIN]["coordinators"][mock_device.udn]
|
||||
assert coordinator.update_interval == timedelta(seconds=DEFAULT_SCAN_INTERVAL)
|
||||
|
||||
# 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,
|
||||
}
|
||||
|
||||
# Also updates DataUpdateCoordinator.
|
||||
assert coordinator.update_interval == timedelta(seconds=60)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue