Changes after review by @MartinHjelmare

This commit is contained in:
Steven Looman 2018-10-03 11:07:25 +02:00
parent d732f8eca2
commit 3cb20c7b4d
4 changed files with 17 additions and 9 deletions

View File

@ -4,7 +4,6 @@ Support for UPnP/IGD Sensors.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.upnp/ https://home-assistant.io/components/sensor.upnp/
""" """
# pylint: disable=invalid-name
from datetime import datetime from datetime import datetime
import logging import logging
@ -12,6 +11,7 @@ from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.components.upnp.const import DOMAIN as DATA_UPNP from homeassistant.components.upnp.const import DOMAIN as DATA_UPNP
from homeassistant.components.upnp.const import SIGNAL_REMOVE_SENSOR
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -88,9 +88,10 @@ class UpnpSensor(Entity):
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Subscribe to sensors events.""" """Subscribe to sensors events."""
async_dispatcher_connect(self.hass, async_dispatcher_connect(self.hass,
'upnp_remove_sensor', SIGNAL_REMOVE_SENSOR,
self._upnp_remove_sensor) self._upnp_remove_sensor)
@callback
def _upnp_remove_sensor(self, device): def _upnp_remove_sensor(self, device):
"""Remove sensor.""" """Remove sensor."""
if self._device != device: if self._device != device:

View File

@ -4,7 +4,6 @@ Will open a port in your router for Home Assistant and provide statistics.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/upnp/ https://home-assistant.io/components/upnp/
""" """
# pylint: disable=invalid-name
import asyncio import asyncio
from ipaddress import ip_address from ipaddress import ip_address
@ -23,6 +22,7 @@ from .const import (
CONF_ENABLE_PORT_MAPPING, CONF_ENABLE_SENSORS, CONF_ENABLE_PORT_MAPPING, CONF_ENABLE_SENSORS,
CONF_HASS, CONF_LOCAL_IP, CONF_PORTS, CONF_HASS, CONF_LOCAL_IP, CONF_PORTS,
CONF_UDN, CONF_SSDP_DESCRIPTION, CONF_UDN, CONF_SSDP_DESCRIPTION,
SIGNAL_REMOVE_SENSOR,
) )
from .const import DOMAIN from .const import DOMAIN
from .const import LOGGER as _LOGGER from .const import LOGGER as _LOGGER
@ -51,14 +51,20 @@ CONFIG_SCHEMA = vol.Schema({
def _substitute_hass_ports(ports, hass_port): def _substitute_hass_ports(ports, hass_port):
# substitute 'hass' for hass_port, both sides """Substitute 'hass' for the hass_port."""
ports = ports.copy()
# substitute 'hass' for hass_port, both keys and values
if CONF_HASS in ports: if CONF_HASS in ports:
ports[hass_port] = ports[CONF_HASS] ports[hass_port] = ports[CONF_HASS]
del ports[CONF_HASS] del ports[CONF_HASS]
for port in ports: for port in ports:
if ports[port] == CONF_HASS: if ports[port] == CONF_HASS:
ports[port] = hass_port ports[port] = hass_port
return ports
# config # config
async def async_setup(hass: HomeAssistantType, config: ConfigType): async def async_setup(hass: HomeAssistantType, config: ConfigType):
@ -107,7 +113,7 @@ async def async_setup_entry(hass: HomeAssistantType,
device = await Device.async_create_device(hass, ssdp_description) device = await Device.async_create_device(hass, ssdp_description)
except (asyncio.TimeoutError, aiohttp.ClientError): except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error('Unable to create upnp-device') _LOGGER.error('Unable to create upnp-device')
return return False
hass.data[DOMAIN]['devices'][device.udn] = device hass.data[DOMAIN]['devices'][device.udn] = device
@ -118,7 +124,7 @@ async def async_setup_entry(hass: HomeAssistantType,
_LOGGER.debug('Enabling port mappings: %s', ports) _LOGGER.debug('Enabling port mappings: %s', ports)
hass_port = hass.http.server_port hass_port = hass.http.server_port
_substitute_hass_ports(ports, hass_port) ports = _substitute_hass_ports(ports, hass_port)
await device.async_add_port_mappings(ports, local_ip=local_ip) await device.async_add_port_mappings(ports, local_ip=local_ip)
# sensors # sensors
@ -155,7 +161,7 @@ async def async_unload_entry(hass: HomeAssistantType,
# sensors # sensors
if data.get(CONF_ENABLE_SENSORS): if data.get(CONF_ENABLE_SENSORS):
_LOGGER.debug('Deleting sensors') _LOGGER.debug('Deleting sensors')
dispatcher.async_dispatcher_send(hass, 'upnp_remove_sensor', device) dispatcher.async_dispatcher_send(hass, SIGNAL_REMOVE_SENSOR, device)
# clear stored device # clear stored device
del hass.data[DOMAIN]['devices'][udn] del hass.data[DOMAIN]['devices'][udn]

View File

@ -125,8 +125,8 @@ class UpnpFlowHandler(data_entry_flow.FlowHandler):
data_schema=vol.Schema( data_schema=vol.Schema(
OrderedDict([ OrderedDict([
(vol.Required('name'), vol.In(names)), (vol.Required('name'), vol.In(names)),
(vol.Optional('enable_sensors', default=False), bool), (vol.Optional('enable_sensors'), bool),
(vol.Optional('enable_port_mapping', default=False), bool), (vol.Optional('enable_port_mapping'), bool),
]) ])
)) ))

View File

@ -11,3 +11,4 @@ CONF_SSDP_DESCRIPTION = 'ssdp_description'
CONF_UDN = 'udn' CONF_UDN = 'udn'
DOMAIN = 'upnp' DOMAIN = 'upnp'
LOGGER = logging.getLogger('homeassistant.components.upnp') LOGGER = logging.getLogger('homeassistant.components.upnp')
SIGNAL_REMOVE_SENSOR = 'upnp_remove_sensor'