Set ports (hass -> hass) when no ports have been specified

This commit is contained in:
Steven Looman 2018-09-14 21:30:08 +02:00
parent 6433f2e2e3
commit ca7911ec47
3 changed files with 35 additions and 15 deletions

View File

@ -43,11 +43,24 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_ENABLE_SENSORS, default=True): cv.boolean, vol.Optional(CONF_ENABLE_SENSORS, default=True): cv.boolean,
vol.Optional(CONF_LOCAL_IP): vol.All(ip_address, cv.string), vol.Optional(CONF_LOCAL_IP): vol.All(ip_address, cv.string),
vol.Optional(CONF_PORTS): vol.Optional(CONF_PORTS):
vol.Schema({vol.Any(CONF_HASS, cv.positive_int): cv.positive_int}) vol.Schema({
vol.Any(CONF_HASS, cv.positive_int):
vol.Any(CONF_HASS, cv.positive_int)
})
}), }),
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
def _substitute_hass_ports(ports, hass_port):
# substitute 'hass' for hass_port, both sides
if CONF_HASS in ports:
ports[hass_port] = ports[CONF_HASS]
del ports[CONF_HASS]
for port in ports:
if ports[port] == CONF_HASS:
ports[port] = hass_port
# config # config
async def async_setup(hass: HomeAssistantType, config: ConfigType): async def async_setup(hass: HomeAssistantType, config: ConfigType):
"""Register a port mapping for Home Assistant via UPnP.""" """Register a port mapping for Home Assistant via UPnP."""
@ -62,20 +75,17 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
_LOGGER.warning('IGD needs discovery, please enable it') _LOGGER.warning('IGD needs discovery, please enable it')
return False return False
# overridden local ip
igd_config = config[DOMAIN] igd_config = config[DOMAIN]
if CONF_LOCAL_IP in igd_config: if CONF_LOCAL_IP in igd_config:
hass.data[DOMAIN]['local_ip'] = igd_config[CONF_LOCAL_IP] hass.data[DOMAIN]['local_ip'] = igd_config[CONF_LOCAL_IP]
# determine ports # determine ports
ports = {} ports = {CONF_HASS: CONF_HASS} # default, port_forward disabled by default
if CONF_PORTS in igd_config: if CONF_PORTS in igd_config:
# copy from config
ports = igd_config[CONF_PORTS] ports = igd_config[CONF_PORTS]
if CONF_HASS in ports:
internal_port = hass.http.server_port
ports[internal_port] = ports[CONF_HASS]
del ports[CONF_HASS]
hass.data[DOMAIN]['auto_config'] = { hass.data[DOMAIN]['auto_config'] = {
'active': True, 'active': True,
'port_forward': igd_config[CONF_ENABLE_PORT_MAPPING], 'port_forward': igd_config[CONF_ENABLE_PORT_MAPPING],
@ -108,6 +118,9 @@ async def async_setup_entry(hass: HomeAssistantType,
local_ip = hass.data[DOMAIN].get('local_ip') local_ip = hass.data[DOMAIN].get('local_ip')
ports = hass.data[DOMAIN]['auto_config']['ports'] ports = hass.data[DOMAIN]['auto_config']['ports']
_LOGGER.debug('Enabling port mappings: %s', ports) _LOGGER.debug('Enabling port mappings: %s', ports)
hass_port = hass.http.server_port
_substitute_hass_ports(ports, hass_port)
await device.async_add_port_mappings(ports, local_ip=local_ip) await device.async_add_port_mappings(ports, local_ip=local_ip)
# sensors # sensors

View File

@ -21,7 +21,7 @@ def ensure_domain_data(hass):
'active': False, 'active': False,
'port_forward': False, 'port_forward': False,
'sensors': False, 'sensors': False,
'ports': {}, 'ports': {'hass': 'hass'},
}) })

View File

@ -54,7 +54,7 @@ async def test_async_setup_no_auto_config(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == { assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': False, 'active': False,
'port_forward': False, 'port_forward': False,
'ports': {}, 'ports': {'hass': 'hass'},
'sensors': False, 'sensors': False,
} }
@ -67,7 +67,7 @@ async def test_async_setup_auto_config(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == { assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True, 'active': True,
'port_forward': False, 'port_forward': False,
'ports': {}, 'ports': {'hass': 'hass'},
'sensors': True, 'sensors': True,
} }
@ -76,13 +76,16 @@ async def test_async_setup_auto_config_port_forward(hass):
"""Test async_setup.""" """Test async_setup."""
# setup component, enable auto_config # setup component, enable auto_config
await async_setup_component(hass, 'igd', { await async_setup_component(hass, 'igd', {
'igd': {'port_forward': True, 'ports': {8123: 8123}}, 'igd': {
'port_forward': True,
'ports': {'hass': 'hass'},
},
'discovery': {}}) 'discovery': {}})
assert hass.data[igd.DOMAIN]['auto_config'] == { assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True, 'active': True,
'port_forward': True, 'port_forward': True,
'ports': {8123: 8123}, 'ports': {'hass': 'hass'},
'sensors': True, 'sensors': True,
} }
@ -97,7 +100,7 @@ async def test_async_setup_auto_config_no_sensors(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == { assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True, 'active': True,
'port_forward': False, 'port_forward': False,
'ports': {}, 'ports': {'hass': 'hass'},
'sensors': False, 'sensors': False,
} }
@ -153,8 +156,12 @@ async def test_async_setup_entry_port_forward(hass):
# ensure hass.http is available # ensure hass.http is available
await async_setup_component(hass, 'igd', { await async_setup_component(hass, 'igd', {
'igd': {'port_forward': True, 'ports': {8123: 8123}}, 'igd': {
'discovery': {}}) 'port_forward': True,
'ports': {'hass': 'hass'},
},
'discovery': {},
})
mock_device = MockDevice(udn) mock_device = MockDevice(udn)
with patch.object(Device, 'async_create_device') as mock_create_device: with patch.object(Device, 'async_create_device') as mock_create_device: