Skip to content
Closed
Changes from 17 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
32 changes: 24 additions & 8 deletions homeassistant/components/upnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 +70,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(

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 'persistent_notification'

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

return True