Move upnp imports at top-level (#29083)
This commit is contained in:
parent
29e2201446
commit
5015993f30
3 changed files with 12 additions and 19 deletions
|
@ -7,11 +7,12 @@ import voluptuous as vol
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import (
|
||||||
from homeassistant.helpers import device_registry as dr
|
config_validation as cv,
|
||||||
from homeassistant.helpers import dispatcher
|
device_registry as dr,
|
||||||
from homeassistant.helpers.typing import ConfigType
|
dispatcher,
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
)
|
||||||
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
from homeassistant.util import get_local_ip
|
from homeassistant.util import get_local_ip
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
@ -20,10 +21,10 @@ from .const import (
|
||||||
CONF_HASS,
|
CONF_HASS,
|
||||||
CONF_LOCAL_IP,
|
CONF_LOCAL_IP,
|
||||||
CONF_PORTS,
|
CONF_PORTS,
|
||||||
|
DOMAIN,
|
||||||
|
LOGGER as _LOGGER,
|
||||||
SIGNAL_REMOVE_SENSOR,
|
SIGNAL_REMOVE_SENSOR,
|
||||||
)
|
)
|
||||||
from .const import DOMAIN
|
|
||||||
from .const import LOGGER as _LOGGER
|
|
||||||
from .device import Device
|
from .device import Device
|
||||||
|
|
||||||
NOTIFICATION_ID = "upnp_notification"
|
NOTIFICATION_ID = "upnp_notification"
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
"""Config flow for UPNP."""
|
"""Config flow for UPNP."""
|
||||||
from homeassistant.helpers import config_entry_flow
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.helpers import config_entry_flow
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .device import Device
|
from .device import Device
|
||||||
|
|
||||||
|
|
||||||
config_entry_flow.register_discovery_flow(
|
config_entry_flow.register_discovery_flow(
|
||||||
DOMAIN, "UPnP/IGD", Device.async_discover, config_entries.CONN_CLASS_LOCAL_POLL
|
DOMAIN, "UPnP/IGD", Device.async_discover, config_entries.CONN_CLASS_LOCAL_POLL
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,13 +3,14 @@ import asyncio
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
|
|
||||||
import aiohttp
|
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 async_upnp_client.profiles.igd import IgdDevice
|
||||||
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
|
|
||||||
from .const import LOGGER as _LOGGER
|
from .const import CONF_LOCAL_IP, DOMAIN, LOGGER as _LOGGER
|
||||||
from .const import DOMAIN, CONF_LOCAL_IP
|
|
||||||
|
|
||||||
|
|
||||||
class Device:
|
class Device:
|
||||||
|
@ -48,14 +49,10 @@ class Device:
|
||||||
async def async_create_device(cls, hass: HomeAssistantType, ssdp_description: str):
|
async def async_create_device(cls, hass: HomeAssistantType, ssdp_description: str):
|
||||||
"""Create UPnP/IGD device."""
|
"""Create UPnP/IGD device."""
|
||||||
# build async_upnp_client requester
|
# build async_upnp_client requester
|
||||||
from async_upnp_client.aiohttp import AiohttpSessionRequester
|
|
||||||
|
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
requester = AiohttpSessionRequester(session, True)
|
requester = AiohttpSessionRequester(session, True)
|
||||||
|
|
||||||
# create async_upnp_client device
|
# create async_upnp_client device
|
||||||
from async_upnp_client import UpnpFactory
|
|
||||||
|
|
||||||
factory = UpnpFactory(requester, disable_state_variable_validation=True)
|
factory = UpnpFactory(requester, disable_state_variable_validation=True)
|
||||||
upnp_device = await factory.async_create_device(ssdp_description)
|
upnp_device = await factory.async_create_device(ssdp_description)
|
||||||
|
|
||||||
|
@ -99,8 +96,6 @@ class Device:
|
||||||
async def _async_add_port_mapping(self, external_port, local_ip, internal_port):
|
async def _async_add_port_mapping(self, external_port, local_ip, internal_port):
|
||||||
"""Add a port mapping."""
|
"""Add a port mapping."""
|
||||||
# create port mapping
|
# create port mapping
|
||||||
from async_upnp_client import UpnpError
|
|
||||||
|
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
"Creating port mapping %s:%s:%s (TCP)",
|
"Creating port mapping %s:%s:%s (TCP)",
|
||||||
external_port,
|
external_port,
|
||||||
|
@ -135,8 +130,6 @@ class Device:
|
||||||
|
|
||||||
async def _async_delete_port_mapping(self, external_port):
|
async def _async_delete_port_mapping(self, external_port):
|
||||||
"""Remove a port mapping."""
|
"""Remove a port mapping."""
|
||||||
from async_upnp_client import UpnpError
|
|
||||||
|
|
||||||
_LOGGER.info("Deleting port mapping %s (TCP)", external_port)
|
_LOGGER.info("Deleting port mapping %s (TCP)", external_port)
|
||||||
try:
|
try:
|
||||||
await self._igd_device.async_delete_port_mapping(
|
await self._igd_device.async_delete_port_mapping(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue