Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move upnp imports at top-level #29083

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
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
15 changes: 8 additions & 7 deletions homeassistant/components/upnp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import dispatcher
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
dispatcher,
)
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util import get_local_ip

from .const import (
Expand All @@ -20,10 +21,10 @@
CONF_HASS,
CONF_LOCAL_IP,
CONF_PORTS,
DOMAIN,
LOGGER as _LOGGER,
SIGNAL_REMOVE_SENSOR,
)
from .const import DOMAIN
from .const import LOGGER as _LOGGER
from .device import Device

NOTIFICATION_ID = "upnp_notification"
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/upnp/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Config flow for UPNP."""
from homeassistant.helpers import config_entry_flow
from homeassistant import config_entries
from homeassistant.helpers import config_entry_flow

from .const import DOMAIN
from .device import Device


config_entry_flow.register_discovery_flow(
DOMAIN, "UPnP/IGD", Device.async_discover, config_entries.CONN_CLASS_LOCAL_POLL
)
13 changes: 3 additions & 10 deletions homeassistant/components/upnp/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from ipaddress import IPv4Address

import aiohttp
from async_upnp_client import UpnpError, UpnpFactory
from async_upnp_client.aiohttp import AiohttpSessionRequester
from async_upnp_client.profiles.igd import IgdDevice

from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import HomeAssistantType

from .const import LOGGER as _LOGGER
from .const import DOMAIN, CONF_LOCAL_IP
from .const import CONF_LOCAL_IP, DOMAIN, LOGGER as _LOGGER


class Device:
Expand Down Expand Up @@ -48,14 +49,10 @@ async def async_discover(cls, hass: HomeAssistantType):
async def async_create_device(cls, hass: HomeAssistantType, ssdp_description: str):
"""Create UPnP/IGD device."""
# build async_upnp_client requester
from async_upnp_client.aiohttp import AiohttpSessionRequester

session = async_get_clientsession(hass)
requester = AiohttpSessionRequester(session, True)

# create async_upnp_client device
from async_upnp_client import UpnpFactory

factory = UpnpFactory(requester, disable_state_variable_validation=True)
upnp_device = await factory.async_create_device(ssdp_description)

Expand Down Expand Up @@ -99,8 +96,6 @@ async def async_add_port_mappings(self, ports, local_ip):
async def _async_add_port_mapping(self, external_port, local_ip, internal_port):
"""Add a port mapping."""
# create port mapping
from async_upnp_client import UpnpError

_LOGGER.info(
"Creating port mapping %s:%s:%s (TCP)",
external_port,
Expand Down Expand Up @@ -135,8 +130,6 @@ async def async_delete_port_mappings(self):

async def _async_delete_port_mapping(self, external_port):
"""Remove a port mapping."""
from async_upnp_client import UpnpError

_LOGGER.info("Deleting port mapping %s (TCP)", external_port)
try:
await self._igd_device.async_delete_port_mapping(
Expand Down