Disable creating port mappings from UI, add discovery from component (#18565)
* Disable creating port mappings from UI, add discovery from component * Remove unused constant * Upgrade to async_upnp_client==0.13.6 and use manufacturer from device * Upgrade to async_upnp_client==0.13.7
This commit is contained in:
parent
5efc61feaf
commit
501b3f9927
12 changed files with 195 additions and 653 deletions
|
@ -1,261 +0,0 @@
|
|||
"""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)',
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': False,
|
||||
})
|
||||
assert result['type'] == 'abort'
|
||||
assert result['reason'] == 'already_configured'
|
||||
|
||||
|
||||
async def test_flow_no_sensors_no_port_mapping(hass):
|
||||
"""Test single device, no sensors, no port_mapping."""
|
||||
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)',
|
||||
'enable_sensors': False,
|
||||
'enable_port_mapping': False,
|
||||
})
|
||||
assert result['type'] == 'abort'
|
||||
assert result['reason'] == 'no_sensors_or_port_mapping'
|
||||
|
||||
|
||||
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 two devices discovered, show form flow with two devices."""
|
||||
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)',
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': False,
|
||||
})
|
||||
assert result['data_schema']({
|
||||
'name': '192.168.2.1 (Test device)',
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': 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)',
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': False,
|
||||
})
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['data'] == {
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'udn': 'uuid:device_1',
|
||||
'port_mapping': False,
|
||||
'sensors': True,
|
||||
}
|
||||
assert result['title'] == 'Test device 1'
|
||||
|
||||
|
||||
async def test_flow_discovery_no_data(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,
|
||||
'enable_port_mapping': False,
|
||||
'enable_sensors': True,
|
||||
},
|
||||
}
|
||||
|
||||
# discovered device
|
||||
result = await flow.async_step_discovery({})
|
||||
|
||||
assert result['type'] == 'abort'
|
||||
assert result['reason'] == 'incomplete_device'
|
||||
|
||||
|
||||
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,
|
||||
'enable_port_mapping': False,
|
||||
'enable_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'] == {
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'udn': 'uuid:device_1',
|
||||
'sensors': True,
|
||||
'port_mapping': False,
|
||||
}
|
||||
assert result['title'] == 'Test device 1'
|
||||
|
||||
|
||||
async def test_flow_discovery_auto_config_sensors_port_mapping(hass):
|
||||
"""Test creation of device with auto_config, with port mapping."""
|
||||
flow = upnp_config_flow.UpnpFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
# auto_config active, with port_mapping
|
||||
hass.data[upnp.DOMAIN] = {
|
||||
'auto_config': {
|
||||
'active': True,
|
||||
'enable_port_mapping': True,
|
||||
'enable_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'] == {
|
||||
'udn': 'uuid:device_1',
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'sensors': True,
|
||||
'port_mapping': True,
|
||||
}
|
||||
assert result['title'] == 'Test device 1'
|
|
@ -26,7 +26,7 @@ class MockDevice(Device):
|
|||
@classmethod
|
||||
async def async_create_device(cls, hass, ssdp_description):
|
||||
"""Return self."""
|
||||
return cls()
|
||||
return cls('UDN')
|
||||
|
||||
@property
|
||||
def udn(self):
|
||||
|
@ -47,102 +47,10 @@ class MockDevice(Device):
|
|||
self.removed_port_mappings.append(entry)
|
||||
|
||||
|
||||
async def test_async_setup_no_auto_config(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
config = {
|
||||
'discovery': {},
|
||||
# no upnp
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[upnp.DOMAIN]['auto_config'] == {
|
||||
'active': False,
|
||||
'enable_sensors': False,
|
||||
'enable_port_mapping': False,
|
||||
'ports': {'hass': 'hass'},
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
config = {
|
||||
'discovery': {},
|
||||
'upnp': {},
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[upnp.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': False,
|
||||
'ports': {'hass': 'hass'},
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config_port_mapping(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
config = {
|
||||
'discovery': {},
|
||||
'upnp': {
|
||||
'port_mapping': True,
|
||||
'ports': {'hass': 'hass'},
|
||||
},
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[upnp.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'enable_sensors': True,
|
||||
'enable_port_mapping': True,
|
||||
'ports': {'hass': 'hass'},
|
||||
}
|
||||
|
||||
|
||||
async def test_async_setup_auto_config_no_sensors(hass):
|
||||
"""Test async_setup."""
|
||||
# setup component, enable auto_config
|
||||
config = {
|
||||
'discovery': {},
|
||||
'upnp': {'sensors': False},
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[upnp.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'enable_sensors': False,
|
||||
'enable_port_mapping': False,
|
||||
'ports': {'hass': 'hass'},
|
||||
}
|
||||
|
||||
|
||||
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_mapping': False,
|
||||
})
|
||||
entry = MockConfigEntry(domain=upnp.DOMAIN)
|
||||
|
||||
config = {
|
||||
'http': {},
|
||||
|
@ -150,7 +58,7 @@ async def test_async_setup_entry_default(hass):
|
|||
# no upnp
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
patch('homeassistant.components.upnp.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'http', config)
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
|
@ -158,17 +66,23 @@ async def test_async_setup_entry_default(hass):
|
|||
|
||||
# mock homeassistant.components.upnp.device.Device
|
||||
mock_device = MockDevice(udn)
|
||||
with patch.object(Device, 'async_create_device') as create_device:
|
||||
discovery_infos = [{
|
||||
'udn': udn,
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
}]
|
||||
with patch.object(Device, 'async_create_device') as create_device, \
|
||||
patch.object(Device, 'async_discover') as async_discover: # noqa:E125
|
||||
|
||||
create_device.return_value = mock_coro(return_value=mock_device)
|
||||
with patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
assert await upnp.async_setup_entry(hass, entry) is True
|
||||
async_discover.return_value = mock_coro(return_value=discovery_infos)
|
||||
|
||||
# ensure device is stored/used
|
||||
assert hass.data[upnp.DOMAIN]['devices'][udn] == mock_device
|
||||
assert await upnp.async_setup_entry(hass, entry) is True
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
# 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 no port-mappings created or removed
|
||||
assert not mock_device.added_port_mappings
|
||||
|
@ -177,13 +91,9 @@ async def test_async_setup_entry_default(hass):
|
|||
|
||||
async def test_async_setup_entry_port_mapping(hass):
|
||||
"""Test async_setup_entry."""
|
||||
# pylint: disable=invalid-name
|
||||
udn = 'uuid:device_1'
|
||||
entry = MockConfigEntry(domain=upnp.DOMAIN, data={
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
'udn': udn,
|
||||
'sensors': False,
|
||||
'port_mapping': True,
|
||||
})
|
||||
entry = MockConfigEntry(domain=upnp.DOMAIN)
|
||||
|
||||
config = {
|
||||
'http': {},
|
||||
|
@ -194,15 +104,22 @@ async def test_async_setup_entry_port_mapping(hass):
|
|||
},
|
||||
}
|
||||
with MockDependency('netdisco.discovery'), \
|
||||
patch('homeassistant.components.upnp.config_flow.get_local_ip',
|
||||
patch('homeassistant.components.upnp.get_local_ip',
|
||||
return_value='192.168.1.10'):
|
||||
await async_setup_component(hass, 'http', config)
|
||||
await async_setup_component(hass, 'upnp', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_device = MockDevice(udn)
|
||||
with patch.object(Device, 'async_create_device') as create_device:
|
||||
discovery_infos = [{
|
||||
'udn': udn,
|
||||
'ssdp_description': 'http://192.168.1.1/desc.xml',
|
||||
}]
|
||||
with patch.object(Device, 'async_create_device') as create_device, \
|
||||
patch.object(Device, 'async_discover') as async_discover: # noqa:E125
|
||||
|
||||
create_device.return_value = mock_coro(return_value=mock_device)
|
||||
async_discover.return_value = mock_coro(return_value=discovery_infos)
|
||||
|
||||
assert await upnp.async_setup_entry(hass, entry) is True
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue