mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Merge pull request #1790 from robbiet480/upnp
UPnP port mapping component
This commit is contained in:
commit
5d8cd6d49d
@ -65,6 +65,7 @@ omit =
|
|||||||
homeassistant/components/scsgate.py
|
homeassistant/components/scsgate.py
|
||||||
homeassistant/components/*/scsgate.py
|
homeassistant/components/*/scsgate.py
|
||||||
|
|
||||||
|
homeassistant/components/upnp.py
|
||||||
homeassistant/components/zeroconf.py
|
homeassistant/components/zeroconf.py
|
||||||
|
|
||||||
homeassistant/components/binary_sensor/arest.py
|
homeassistant/components/binary_sensor/arest.py
|
||||||
|
45
homeassistant/components/upnp.py
Normal file
45
homeassistant/components/upnp.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
"""
|
||||||
|
This module will attempt to open a port in your router for Home Assistant.
|
||||||
|
|
||||||
|
For more details about UPnP, please refer to the documentation at
|
||||||
|
https://home-assistant.io/components/upnp/
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP)
|
||||||
|
|
||||||
|
DEPENDENCIES = ["api"]
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DOMAIN = "upnp"
|
||||||
|
|
||||||
|
|
||||||
|
def setup(hass, config):
|
||||||
|
"""Register a port mapping for Home Assistant via UPnP."""
|
||||||
|
# pylint: disable=import-error
|
||||||
|
import miniupnpc
|
||||||
|
|
||||||
|
# pylint: disable=no-member
|
||||||
|
upnp = miniupnpc.UPnP()
|
||||||
|
|
||||||
|
upnp.discoverdelay = 200
|
||||||
|
upnp.discover()
|
||||||
|
try:
|
||||||
|
upnp.selectigd()
|
||||||
|
# pylint: disable=broad-except
|
||||||
|
except Exception:
|
||||||
|
_LOGGER.exception("Error when attempting to discover a UPnP IGD")
|
||||||
|
return False
|
||||||
|
|
||||||
|
upnp.addportmapping(hass.config.api.port, "TCP",
|
||||||
|
hass.config.api.host, hass.config.api.port,
|
||||||
|
"Home Assistant", "")
|
||||||
|
|
||||||
|
def deregister_port(event):
|
||||||
|
"""De-register the UPnP port mapping."""
|
||||||
|
upnp.deleteportmapping(hass.config.api.port, "TCP")
|
||||||
|
|
||||||
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, deregister_port)
|
||||||
|
|
||||||
|
return True
|
Loading…
x
Reference in New Issue
Block a user