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
https://home-assistant.io/components/sensor.upnp/
"""
# pylint: disable=invalid-name
from datetime import datetime
import logging
@ -12,6 +11,7 @@ from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.components.upnp.const import DOMAIN as DATA_UPNP
from homeassistant.components.upnp.const import SIGNAL_REMOVE_SENSOR
_LOGGER = logging.getLogger(__name__)
@ -88,9 +88,10 @@ class UpnpSensor(Entity):
async def async_added_to_hass(self):
"""Subscribe to sensors events."""
async_dispatcher_connect(self.hass,
'upnp_remove_sensor',
SIGNAL_REMOVE_SENSOR,
self._upnp_remove_sensor)
@callback
def _upnp_remove_sensor(self, device):
"""Remove sensor."""
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
https://home-assistant.io/components/upnp/
"""
# pylint: disable=invalid-name
import asyncio
from ipaddress import ip_address
@ -23,6 +22,7 @@ from .const import (
CONF_ENABLE_PORT_MAPPING, CONF_ENABLE_SENSORS,
CONF_HASS, CONF_LOCAL_IP, CONF_PORTS,
CONF_UDN, CONF_SSDP_DESCRIPTION,
SIGNAL_REMOVE_SENSOR,
)
from .const import DOMAIN
from .const import LOGGER as _LOGGER
@ -51,14 +51,20 @@ CONFIG_SCHEMA = vol.Schema({
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:
ports[hass_port] = ports[CONF_HASS]
del ports[CONF_HASS]
for port in ports:
if ports[port] == CONF_HASS:
ports[port] = hass_port
return ports
# config
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)
except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error('Unable to create upnp-device')
return
return False
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)
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)
# sensors
@ -155,7 +161,7 @@ async def async_unload_entry(hass: HomeAssistantType,
# sensors
if data.get(CONF_ENABLE_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
del hass.data[DOMAIN]['devices'][udn]

View File

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

View File

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