Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions homeassistant/components/upnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import voluptuous as vol

import homeassistant.loader as loader

from homeassistant.const import (EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import discovery
Expand All @@ -25,6 +27,9 @@
CONF_ENABLE_PORT_MAPPING = 'port_mapping'
CONF_UNITS = 'unit'

NOTIFICATION_ID = 'upnp_notification'
NOTIFICATION_TITLE = 'UPnP Setup'

UNITS = {
"Bytes": 1,
"KBytes": 1024,
Expand Down Expand Up @@ -67,13 +72,26 @@ def setup(hass, config):
host = base_url.hostname
external_port = internal_port = base_url.port

upnp.addportmapping(
external_port, 'TCP', host, internal_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)
persistent_notification = loader.get_component('persistent_notification')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'loader'


try:
upnp.addportmapping(
external_port, 'TCP', host, internal_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)

except Exception as ex:
_LOGGER.error("UPnP failed to configure port mapping: %s", str(ex))
persistent_notification.create(
hass, 'Port {} is already mapped,'
'please disable port_mapping in upnp 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