Working on IGD
This commit is contained in:
1 parent
3db766e2ec
commit
20879726b0
9 files changed
+284
-142
No files matched your search
@@ -1 +1 @@
|
||||
"""Tests for the IGD component."""
|
||||
"""Tests for the IGD component."""
|
||||
@@ -1,13 +1,14 @@
|
||||
"""Tests for IGD config flow."""
|
||||
|
||||
from homeassistant.components import igd
|
||||
from homeassistant.components.igd import config_flow as igd_config_flow
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_flow_none_discovered(hass):
|
||||
"""Test no device discovered flow."""
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_user()
|
||||
@@ -17,7 +18,7 @@ async def test_flow_none_discovered(hass):
|
||||
|
||||
async def test_flow_already_configured(hass):
|
||||
"""Test device already configured flow."""
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# discovered device
|
||||
@@ -48,7 +49,7 @@ async def test_flow_already_configured(hass):
|
||||
|
||||
async def test_flow_no_sensors_no_port_forward(hass):
|
||||
"""Test single device, no sensors, no port_forward."""
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# discovered device
|
||||
@@ -79,7 +80,7 @@ async def test_flow_no_sensors_no_port_forward(hass):
|
||||
|
||||
async def test_flow_discovered_form(hass):
|
||||
"""Test single device discovered, show form flow."""
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# discovered device
|
||||
@@ -100,7 +101,7 @@ async def test_flow_discovered_form(hass):
|
||||
|
||||
async def test_flow_two_discovered_form(hass):
|
||||
"""Test single device discovered, show form flow."""
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# discovered device
|
||||
@@ -135,18 +136,18 @@ async def test_flow_two_discovered_form(hass):
|
||||
|
||||
|
||||
async def test_config_entry_created(hass):
|
||||
flow = igd.config_flow.IgdFlowHandler()
|
||||
"""Test config entry is created."""
|
||||
flow = igd_config_flow.IgdFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# discovered device
|
||||
udn = 'uuid:device_1'
|
||||
hass.data[igd.DOMAIN] = {
|
||||
'discovered': {
|
||||
udn: {
|
||||
'uuid:device_1': {
|
||||
'name': 'Test device 1',
|
||||
'host': '192.168.1.1',
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'udn': udn,
|
||||
'udn': 'uuid:device_1',
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -156,6 +157,7 @@ async def test_config_entry_created(hass):
|
||||
'sensors': True,
|
||||
'port_forward': False,
|
||||
})
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['data'] == {
|
||||
'port_forward': False,
|
||||
'sensors': True,
|
||||
@@ -163,3 +165,67 @@ async def test_config_entry_created(hass):
|
||||
'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'
|
||||
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_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'
|
||||
@@ -10,9 +10,96 @@ from tests.common import MockConfigEntry
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
async def test_async_setup_entry_port_forward_created(hass):
|
||||
"""Test async_setup_entry."""
|
||||
async def test_async_setup_no_auto_config(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
await async_setup_component(hass, 'igd')
|
||||
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': False,
|
||||
'port_forward': False,
|
||||
'sensors': False,
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
await async_setup_component(hass, 'igd', {'igd': {}, 'discovery': {}})
|
||||
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': False,
|
||||
'sensors': True,
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config_port_forward(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
await async_setup_component(hass, 'igd', {
|
||||
'igd': {'port_forward': True},
|
||||
'discovery': {}})
|
||||
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': True,
|
||||
'sensors': True,
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config_no_sensors(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
await async_setup_component(hass, 'igd', {
|
||||
'igd': {'sensors': False},
|
||||
'discovery': {}})
|
||||
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': False,
|
||||
'sensors': False,
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_entry_default(hass):
|
||||
"""Test async_setup_entry."""
|
||||
udn = 'uuid:device_1'
|
||||
entry = MockConfigEntry(domain=igd.DOMAIN, data={
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'udn': udn,
|
||||
'sensors': True,
|
||||
'port_forward': False,
|
||||
})
|
||||
|
||||
# ensure hass.http is available
|
||||
await async_setup_component(hass, 'igd')
|
||||
|
||||
# mock async_upnp_client.igd.IgdDevice
|
||||
mock_igd_device = MagicMock()
|
||||
mock_igd_device.udn = udn
|
||||
mock_igd_device.async_add_port_mapping.return_value = mock_coro()
|
||||
mock_igd_device.async_delete_port_mapping.return_value = mock_coro()
|
||||
with patch.object(igd, '_async_create_igd_device') as mock_create_device:
|
||||
mock_create_device.return_value = mock_coro(
|
||||
return_value=mock_igd_device)
|
||||
with patch('homeassistant.components.igd.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
assert await igd.async_setup_entry(hass, entry) is True
|
||||
|
||||
# ensure device is stored/used
|
||||
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_igd_device
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[igd.DOMAIN]['devices'][udn] is None
|
||||
assert len(mock_igd_device.async_add_port_mapping.mock_calls) == 0
|
||||
assert len(mock_igd_device.async_delete_port_mapping.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_async_setup_entry_port_forward(hass):
|
||||
"""Test async_setup_entry."""
|
||||
udn = 'uuid:device_1'
|
||||
entry = MockConfigEntry(domain=igd.DOMAIN, data={
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
@@ -27,15 +114,20 @@ async def test_async_setup_entry_port_forward_created(hass):
|
||||
mock_igd_device = MagicMock()
|
||||
mock_igd_device.udn = udn
|
||||
mock_igd_device.async_add_port_mapping.return_value = mock_coro()
|
||||
mock_igd_device.async_remove_port_mapping.return_value = mock_coro()
|
||||
mock_igd_device.async_delete_port_mapping.return_value = mock_coro()
|
||||
with patch.object(igd, '_async_create_igd_device') as mock_create_device:
|
||||
mock_create_device.return_value = mock_coro(return_value=mock_igd_device)
|
||||
with patch('homeassistant.components.igd.get_local_ip', return_value='192.168.1.10'):
|
||||
mock_create_device.return_value = mock_coro(
|
||||
return_value=mock_igd_device)
|
||||
with patch('homeassistant.components.igd.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
assert await igd.async_setup_entry(hass, entry) is True
|
||||
|
||||
# ensure device is stored/used
|
||||
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_igd_device
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_igd_device
|
||||
assert hass.data[igd.DOMAIN]['devices'][udn] is None
|
||||
assert len(mock_igd_device.async_add_port_mapping.mock_calls) > 0
|
||||
assert len(mock_igd_device.async_delete_port_mapping.mock_calls) > 0
|
||||
Reference in new issue
Block a user