Rename igd to upnp

This commit is contained in:
Steven Looman committed 2018-09-17 22:10:18 +02:00
1 parent 6588dceadd
commit c19665fed4
13 files changed
+115 -119

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the IGD component."""
+240
View File
@@ -0,0 +1,240 @@
"""Tests for UPnP/IGD config flow."""
from homeassistant.components import upnp
from homeassistant.components.upnp import config_flow as upnp_config_flow
from tests.common import MockConfigEntry
async def test_flow_none_discovered(hass):
"""Test no device discovered flow."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
hass.data[upnp.DOMAIN] = {
'discovered': {}
}
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."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[upnp.DOMAIN] = {
'discovered': {
udn: {
'friendly_name': '192.168.1.1 (Test device)',
'host': '192.168.1.1',
'udn': udn,
},
},
}
# configured entry
MockConfigEntry(domain=upnp.DOMAIN, data={
'udn': udn,
'host': '192.168.1.1',
}).add_to_hass(hass)
result = await flow.async_step_user({
'name': '192.168.1.1 (Test device)',
'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."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[upnp.DOMAIN] = {
'discovered': {
udn: {
'friendly_name': '192.168.1.1 (Test device)',
'host': '192.168.1.1',
'udn': udn,
},
},
}
# configured entry
MockConfigEntry(domain=upnp.DOMAIN, data={
'udn': udn,
'host': '192.168.1.1',
}).add_to_hass(hass)
result = await flow.async_step_user({
'name': '192.168.1.1 (Test device)',
'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."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# discovered device
udn = 'uuid:device_1'
hass.data[upnp.DOMAIN] = {
'discovered': {
udn: {
'friendly_name': '192.168.1.1 (Test device)',
'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."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# discovered device
udn_1 = 'uuid:device_1'
udn_2 = 'uuid:device_2'
hass.data[upnp.DOMAIN] = {
'discovered': {
udn_1: {
'friendly_name': '192.168.1.1 (Test device)',
'host': '192.168.1.1',
'udn': udn_1,
},
udn_2: {
'friendly_name': '192.168.2.1 (Test device)',
'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']({
'name': '192.168.1.1 (Test device)',
'sensors': True,
'port_forward': False,
})
assert result['data_schema']({
'name': '192.168.1.1 (Test device)',
'sensors': True,
'port_forward': False,
})
async def test_config_entry_created(hass):
"""Test config entry is created."""
flow = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# discovered device
hass.data[upnp.DOMAIN] = {
'discovered': {
'uuid:device_1': {
'friendly_name': '192.168.1.1 (Test device)',
'name': 'Test device 1',
'host': '192.168.1.1',
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': 'uuid:device_1',
},
},
}
result = await flow.async_step_user({
'name': '192.168.1.1 (Test device)',
'sensors': True,
'port_forward': False,
})
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 = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# auto_config active
hass.data[upnp.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 = upnp_config_flow.UpnpFlowHandler()
flow.hass = hass
# auto_config active, with port_forward
hass.data[upnp.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'
+188
View File
@@ -0,0 +1,188 @@
"""Test UPnP/IGD setup process."""
from ipaddress import ip_address
from unittest.mock import patch, MagicMock
from homeassistant.setup import async_setup_component
from homeassistant.components import upnp
from homeassistant.components.upnp.device import Device
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from tests.common import MockConfigEntry
from tests.common import mock_coro
class MockDevice(Device):
"""Mock device for Device."""
def __init__(self, udn):
"""Initializer."""
super().__init__(None)
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()
@property
def udn(self):
"""Get the UDN."""
return self._udn
async def _async_add_port_mapping(self,
external_port,
local_ip,
internal_port):
"""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):
"""Remove a port mapping."""
entry = external_port
self.removed_port_mappings.append(entry)
async def test_async_setup_no_auto_config(hass):
"""Test async_setup."""
# setup component, enable auto_config
await async_setup_component(hass, 'upnp')
assert hass.data[upnp.DOMAIN]['auto_config'] == {
'active': False,
'port_forward': False,
'ports': {'hass': 'hass'},
'sensors': False,
}
async def test_async_setup_auto_config(hass):
"""Test async_setup."""
# setup component, enable auto_config
await async_setup_component(hass, 'upnp', {'upnp': {}, 'discovery': {}})
assert hass.data[upnp.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': False,
'ports': {'hass': 'hass'},
'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, 'upnp', {
'upnp': {
'port_forward': True,
'ports': {'hass': 'hass'},
},
'discovery': {}})
assert hass.data[upnp.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': True,
'ports': {'hass': 'hass'},
'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, 'upnp', {
'upnp': {'sensors': False},
'discovery': {}})
assert hass.data[upnp.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': False,
'ports': {'hass': 'hass'},
'sensors': False,
}
async def test_async_setup_entry_default(hass):
"""Test async_setup_entry."""
udn = 'uuid:device_1'
entry = MockConfigEntry(domain=upnp.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, 'upnp')
# mock homeassistant.components.upnp.device.Device
mock_device = MagicMock()
mock_device.udn = udn
mock_device.async_add_port_mappings.return_value = mock_coro()
mock_device.async_delete_port_mappings.return_value = mock_coro()
with patch.object(Device, 'async_create_device') as mock_create_device:
mock_create_device.return_value = mock_coro(
return_value=mock_device)
with patch('homeassistant.components.upnp.device.get_local_ip',
return_value='192.168.1.10'):
assert await upnp.async_setup_entry(hass, entry) is True
# ensure device is stored/used
assert hass.data[upnp.DOMAIN]['devices'][udn] == mock_device
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
# ensure cleaned up
assert udn not in hass.data[upnp.DOMAIN]['devices']
# ensure no port-mapping-methods called
assert len(mock_device.async_add_port_mappings.mock_calls) == 0
assert len(mock_device.async_delete_port_mappings.mock_calls) == 0
async def test_async_setup_entry_port_forward(hass):
"""Test async_setup_entry."""
udn = 'uuid:device_1'
entry = MockConfigEntry(domain=upnp.DOMAIN, data={
'ssdp_description': 'http://192.168.1.1/desc.xml',
'udn': udn,
'sensors': False,
'port_forward': True,
})
# ensure hass.http is available
await async_setup_component(hass, 'upnp', {
'upnp': {
'port_forward': True,
'ports': {'hass': 'hass'},
},
'discovery': {},
})
mock_device = MockDevice(udn)
with patch.object(Device, 'async_create_device') as mock_create_device:
mock_create_device.return_value = mock_coro(return_value=mock_device)
with patch('homeassistant.components.upnp.device.get_local_ip',
return_value='192.168.1.10'):
assert await upnp.async_setup_entry(hass, entry) is True
# ensure device is stored/used
assert hass.data[upnp.DOMAIN]['devices'][udn] == mock_device
# ensure add-port-mapping-methods called
assert mock_device.added_port_mappings == [
[8123, ip_address('192.168.1.10'), 8123]
]
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
# ensure cleaned up
assert udn not in hass.data[upnp.DOMAIN]['devices']
# ensure delete-port-mapping-methods called
assert mock_device.removed_port_mappings == [8123]