Allow specifying multiple ports for UPNP component (#9560)
* Update UPNP component * Bump dep * Fix flakiness in test
This commit is contained in:
1 parent
1b91218a60
commit
350b8e09e6
4 files changed
+197
-33
No files matched your search
@@ -4,16 +4,18 @@ Will open a port in your router for Home Assistant and provide statistics.
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/upnp/
|
||||
"""
|
||||
from ipaddress import ip_address
|
||||
import logging
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.util import get_local_ip
|
||||
|
||||
REQUIREMENTS = ['miniupnpc==1.9']
|
||||
REQUIREMENTS = ['miniupnpc==2.0.2']
|
||||
DEPENDENCIES = ['http']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,9 +24,11 @@ DOMAIN = 'upnp'
|
||||
|
||||
DATA_UPNP = 'UPNP'
|
||||
|
||||
CONF_LOCAL_IP = 'local_ip'
|
||||
CONF_ENABLE_PORT_MAPPING = 'port_mapping'
|
||||
CONF_EXTERNAL_PORT = 'external_port'
|
||||
CONF_PORTS = 'ports'
|
||||
CONF_UNITS = 'unit'
|
||||
CONF_HASS = 'hass'
|
||||
|
||||
NOTIFICATION_ID = 'upnp_notification'
|
||||
NOTIFICATION_TITLE = 'UPnP Setup'
|
||||
@@ -39,8 +43,10 @@ UNITS = {
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_ENABLE_PORT_MAPPING, default=True): cv.boolean,
|
||||
vol.Optional(CONF_EXTERNAL_PORT, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_UNITS, default="MBytes"): vol.In(UNITS),
|
||||
vol.Optional(CONF_LOCAL_IP): ip_address,
|
||||
vol.Optional(CONF_PORTS):
|
||||
vol.Schema({vol.Any(CONF_HASS, cv.positive_int): cv.positive_int})
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
@@ -48,6 +54,19 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
# pylint: disable=import-error, no-member, broad-except
|
||||
def setup(hass, config):
|
||||
"""Register a port mapping for Home Assistant via UPnP."""
|
||||
config = config[DOMAIN]
|
||||
host = config.get(CONF_LOCAL_IP)
|
||||
|
||||
if host is not None:
|
||||
host = str(host)
|
||||
else:
|
||||
host = get_local_ip()
|
||||
|
||||
if host == '127.0.0.1':
|
||||
_LOGGER.error(
|
||||
'Unable to determine local IP. Add it to your configuration.')
|
||||
return False
|
||||
|
||||
import miniupnpc
|
||||
|
||||
upnp = miniupnpc.UPnP()
|
||||
@@ -61,40 +80,44 @@ def setup(hass, config):
|
||||
_LOGGER.exception("Error when attempting to discover an UPnP IGD")
|
||||
return False
|
||||
|
||||
unit = config[DOMAIN].get(CONF_UNITS)
|
||||
unit = config.get(CONF_UNITS)
|
||||
discovery.load_platform(hass, 'sensor', DOMAIN, {'unit': unit}, config)
|
||||
|
||||
port_mapping = config[DOMAIN].get(CONF_ENABLE_PORT_MAPPING)
|
||||
port_mapping = config.get(CONF_ENABLE_PORT_MAPPING)
|
||||
if not port_mapping:
|
||||
return True
|
||||
|
||||
base_url = urlsplit(hass.config.api.base_url)
|
||||
host = base_url.hostname
|
||||
internal_port = base_url.port
|
||||
external_port = int(config[DOMAIN].get(CONF_EXTERNAL_PORT))
|
||||
internal_port = hass.http.server_port
|
||||
|
||||
if external_port == 0:
|
||||
external_port = internal_port
|
||||
ports = config.get(CONF_PORTS)
|
||||
if ports is None:
|
||||
ports = {CONF_HASS: internal_port}
|
||||
|
||||
try:
|
||||
upnp.addportmapping(
|
||||
external_port, 'TCP', host, internal_port, 'Home Assistant', '')
|
||||
registered = []
|
||||
for internal, external in ports.items():
|
||||
if internal == CONF_HASS:
|
||||
internal = internal_port
|
||||
try:
|
||||
upnp.addportmapping(
|
||||
external, 'TCP', host, internal, 'Home Assistant', '')
|
||||
registered.append(external)
|
||||
except Exception:
|
||||
_LOGGER.exception("UPnP failed to configure port mapping for %s",
|
||||
external)
|
||||
hass.components.persistent_notification.create(
|
||||
'<b>ERROR: tcp port {} is already mapped in your router.'
|
||||
'</b><br />Please disable port_mapping in the <i>upnp</i> '
|
||||
'configuration section.<br />'
|
||||
'You will need to restart hass after fixing.'
|
||||
''.format(external),
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID)
|
||||
|
||||
def deregister_port(event):
|
||||
"""De-register the UPnP port mapping."""
|
||||
upnp.deleteportmapping(external_port, 'TCP')
|
||||
def deregister_port(event):
|
||||
"""De-register the UPnP port mapping."""
|
||||
for external in registered:
|
||||
upnp.deleteportmapping(external, 'TCP')
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, deregister_port)
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, deregister_port)
|
||||
|
||||
except Exception as ex:
|
||||
_LOGGER.error("UPnP failed to configure port mapping: %s", str(ex))
|
||||
hass.components.persistent_notification.create(
|
||||
'<b>ERROR: tcp port {} is already mapped in your router.'
|
||||
'</b><br />Please disable port_mapping in the <i>upnp</i> '
|
||||
'configuration section.<br />'
|
||||
'You will need to restart hass after fixing.'
|
||||
''.format(external_port),
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID)
|
||||
return False
|
||||
return True
|
||||
Reference in new issue
Block a user