mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Set ports (hass -> hass) when no ports have been specified
This commit is contained in:
parent
6433f2e2e3
commit
ca7911ec47
@ -43,11 +43,24 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_ENABLE_SENSORS, default=True): cv.boolean,
|
||||
vol.Optional(CONF_LOCAL_IP): vol.All(ip_address, cv.string),
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
async def async_setup(hass: HomeAssistantType, config: ConfigType):
|
||||
"""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')
|
||||
return False
|
||||
|
||||
# overridden local ip
|
||||
igd_config = config[DOMAIN]
|
||||
if CONF_LOCAL_IP in igd_config:
|
||||
hass.data[DOMAIN]['local_ip'] = igd_config[CONF_LOCAL_IP]
|
||||
|
||||
# determine ports
|
||||
ports = {}
|
||||
ports = {CONF_HASS: CONF_HASS} # default, port_forward disabled by default
|
||||
if CONF_PORTS in igd_config:
|
||||
# copy from config
|
||||
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'] = {
|
||||
'active': True,
|
||||
'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')
|
||||
ports = hass.data[DOMAIN]['auto_config']['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)
|
||||
|
||||
# sensors
|
||||
|
@ -21,7 +21,7 @@ def ensure_domain_data(hass):
|
||||
'active': False,
|
||||
'port_forward': False,
|
||||
'sensors': False,
|
||||
'ports': {},
|
||||
'ports': {'hass': 'hass'},
|
||||
})
|
||||
|
||||
|
||||
|
@ -54,7 +54,7 @@ async def test_async_setup_no_auto_config(hass):
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': False,
|
||||
'port_forward': False,
|
||||
'ports': {},
|
||||
'ports': {'hass': 'hass'},
|
||||
'sensors': False,
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ async def test_async_setup_auto_config(hass):
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': False,
|
||||
'ports': {},
|
||||
'ports': {'hass': 'hass'},
|
||||
'sensors': True,
|
||||
}
|
||||
|
||||
@ -76,13 +76,16 @@ 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, 'ports': {8123: 8123}},
|
||||
'igd': {
|
||||
'port_forward': True,
|
||||
'ports': {'hass': 'hass'},
|
||||
},
|
||||
'discovery': {}})
|
||||
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': True,
|
||||
'ports': {8123: 8123},
|
||||
'ports': {'hass': 'hass'},
|
||||
'sensors': True,
|
||||
}
|
||||
|
||||
@ -97,7 +100,7 @@ async def test_async_setup_auto_config_no_sensors(hass):
|
||||
assert hass.data[igd.DOMAIN]['auto_config'] == {
|
||||
'active': True,
|
||||
'port_forward': False,
|
||||
'ports': {},
|
||||
'ports': {'hass': 'hass'},
|
||||
'sensors': False,
|
||||
}
|
||||
|
||||
@ -153,8 +156,12 @@ async def test_async_setup_entry_port_forward(hass):
|
||||
|
||||
# ensure hass.http is available
|
||||
await async_setup_component(hass, 'igd', {
|
||||
'igd': {'port_forward': True, 'ports': {8123: 8123}},
|
||||
'discovery': {}})
|
||||
'igd': {
|
||||
'port_forward': True,
|
||||
'ports': {'hass': 'hass'},
|
||||
},
|
||||
'discovery': {},
|
||||
})
|
||||
|
||||
mock_device = MockDevice(udn)
|
||||
with patch.object(Device, 'async_create_device') as mock_create_device:
|
||||
|
Loading…
x
Reference in New Issue
Block a user