hass-core/tests/components/igd/test_config_flow.py

232 lines
6 KiB
Python
Raw Normal View History

2018-08-29 21:19:04 +02:00
"""Tests for IGD config flow."""
from homeassistant.components import igd
2018-08-30 16:38:43 +02:00
from homeassistant.components.igd import config_flow as igd_config_flow
2018-08-29 21:19:04 +02:00
from tests.common import MockConfigEntry
async def test_flow_none_discovered(hass):
"""Test no device discovered flow."""
2018-08-30 16:38:43 +02:00
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
result = await flow.async_step_user()
assert result['type'] == 'abort'
assert result['reason'] == 'no_devices_discovered'
async def test_flow_already_configured(hass):
"""Test device already configured flow."""
2018-08-30 16:38:43 +02:00
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[igd.DOMAIN] = {
'discovered': {
udn: {
'host': '192.168.1.1',
'udn': udn,
},
},
}
# configured entry
MockConfigEntry(domain=igd.DOMAIN, data={
'udn': udn,
'host': '192.168.1.1',
}).add_to_hass(hass)
result = await flow.async_step_user({
'igd_host': '192.168.1.1',
'sensors': True,
'port_forward': False,
})
assert result['type'] == 'abort'
assert result['reason'] == 'already_configured'
async def test_flow_no_sensors_no_port_forward(hass):
"""Test single device, no sensors, no port_forward."""
2018-08-30 16:38:43 +02:00
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[igd.DOMAIN] = {
'discovered': {
udn: {
'host': '192.168.1.1',
'udn': udn,
},
},
}
# configured entry
MockConfigEntry(domain=igd.DOMAIN, data={
'udn': udn,
'host': '192.168.1.1',
}).add_to_hass(hass)
result = await flow.async_step_user({
'igd_host': '192.168.1.1',
'sensors': False,
'port_forward': False,
})
assert result['type'] == 'abort'
assert result['reason'] == 'no_sensors_or_port_forward'
async def test_flow_discovered_form(hass):
"""Test single device discovered, show form flow."""
2018-08-30 16:38:43 +02:00
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[igd.DOMAIN] = {
'discovered': {
udn: {
'host': '192.168.1.1',
'udn': udn,
},
},
}
result = await flow.async_step_user()
assert result['type'] == 'form'
assert result['step_id'] == 'user'
async def test_flow_two_discovered_form(hass):
"""Test single device discovered, show form flow."""
2018-08-30 16:38:43 +02:00
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
# discovered device
udn_1 = 'uuid:device_1'
udn_2 = 'uuid:device_2'
hass.data[igd.DOMAIN] = {
'discovered': {
udn_1: {
'host': '192.168.1.1',
'udn': udn_1,
},
udn_2: {
'host': '192.168.2.1',
'udn': udn_2,
},
},
}
result = await flow.async_step_user()
assert result['type'] == 'form'
assert result['step_id'] == 'user'
assert result['data_schema']({
'igd_host': '192.168.1.1',
'sensors': True,
'port_forward': False,
})
assert result['data_schema']({
'igd_host': '192.168.2.1',
'sensors': True,
'port_forward': False,
})
async def test_config_entry_created(hass):
2018-08-30 16:38:43 +02:00
"""Test config entry is created."""
flow = igd_config_flow.IgdFlowHandler()
2018-08-29 21:19:04 +02:00
flow.hass = hass
# discovered device
hass.data[igd.DOMAIN] = {
'discovered': {
2018-08-30 16:38:43 +02:00
'uuid:device_1': {
2018-08-29 21:19:04 +02:00
'name': 'Test device 1',
'host': '192.168.1.1',
'ssdp_description': 'http://192.168.1.1/desc.xml',
2018-08-30 16:38:43 +02:00
'udn': 'uuid:device_1',
2018-08-29 21:19:04 +02:00
},
},
}
result = await flow.async_step_user({
'igd_host': '192.168.1.1',
'sensors': True,
'port_forward': False,
})
2018-08-30 16:38:43 +02:00
assert result['type'] == 'create_entry'
assert result['data'] == {
'port_forward': False,
'sensors': True,
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
}
assert result['title'] == 'Test device 1'
async def test_flow_discovery_auto_config_sensors(hass):
"""Test creation of device with auto_config."""
flow = igd_config_flow.IgdFlowHandler()
flow.hass = hass
# auto_config active
hass.data[igd.DOMAIN] = {
'auto_config': {
'active': True,
'port_forward': False,
'sensors': True,
},
}
# discovered device
result = await flow.async_step_discovery({
'name': 'Test device 1',
'host': '192.168.1.1',
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
})
assert result['type'] == 'create_entry'
2018-08-29 21:19:04 +02:00
assert result['data'] == {
'port_forward': False,
'sensors': True,
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
}
assert result['title'] == 'Test device 1'
2018-08-30 16:38:43 +02:00
async def test_flow_discovery_auto_config_sensors_port_forward(hass):
"""Test creation of device with auto_config, with port forward."""
flow = igd_config_flow.IgdFlowHandler()
flow.hass = hass
# auto_config active, with port_forward
hass.data[igd.DOMAIN] = {
'auto_config': {
'active': True,
'port_forward': True,
'sensors': True,
},
}
# discovered device
result = await flow.async_step_discovery({
'name': 'Test device 1',
'host': '192.168.1.1',
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
})
assert result['type'] == 'create_entry'
assert result['data'] == {
'port_forward': True,
'sensors': True,
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
}
assert result['title'] == 'Test device 1'