From 3cb20c7b4d074c39ad04fb01fb7f07f0eb77faea Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Wed, 3 Oct 2018 11:07:25 +0200 Subject: [PATCH] Changes after review by @MartinHjelmare --- homeassistant/components/sensor/upnp.py | 5 +++-- homeassistant/components/upnp/__init__.py | 16 +++++++++++----- homeassistant/components/upnp/config_flow.py | 4 ++-- homeassistant/components/upnp/const.py | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/sensor/upnp.py b/homeassistant/components/sensor/upnp.py index e511b2947e5..c05e2ce0ade 100644 --- a/homeassistant/components/sensor/upnp.py +++ b/homeassistant/components/sensor/upnp.py @@ -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: diff --git a/homeassistant/components/upnp/__init__.py b/homeassistant/components/upnp/__init__.py index 4ccb07af44b..265f68eaac0 100644 --- a/homeassistant/components/upnp/__init__.py +++ b/homeassistant/components/upnp/__init__.py @@ -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] diff --git a/homeassistant/components/upnp/config_flow.py b/homeassistant/components/upnp/config_flow.py index 65e2283115c..178faafb5f7 100644 --- a/homeassistant/components/upnp/config_flow.py +++ b/homeassistant/components/upnp/config_flow.py @@ -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), ]) )) diff --git a/homeassistant/components/upnp/const.py b/homeassistant/components/upnp/const.py index ad57bc7d7f4..7a906ae02be 100644 --- a/homeassistant/components/upnp/const.py +++ b/homeassistant/components/upnp/const.py @@ -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'