Removed assumptions about provided upnp data (#17604)
* Removed assumptions about incomplete UPnP devices * Removed assumptions about incomplete UPnP devices
This commit is contained in:
parent
9b47af68ae
commit
82edea6077
3 changed files with 41 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
||||||
"""Config flow for UPNP."""
|
"""Config flow for UPNP."""
|
||||||
|
import logging
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
@ -14,6 +15,9 @@ from .const import (
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_ensure_domain_data(hass):
|
async def async_ensure_domain_data(hass):
|
||||||
"""Ensure hass.data is filled properly."""
|
"""Ensure hass.data is filled properly."""
|
||||||
hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
|
hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
|
||||||
|
@ -70,14 +74,25 @@ class UpnpFlowHandler(data_entry_flow.FlowHandler):
|
||||||
"""
|
"""
|
||||||
await async_ensure_domain_data(self.hass)
|
await async_ensure_domain_data(self.hass)
|
||||||
|
|
||||||
|
if not discovery_info.get('udn') or not discovery_info.get('host'):
|
||||||
|
# Silently ignore incomplete/broken devices to prevent constant
|
||||||
|
# errors/warnings
|
||||||
|
_LOGGER.debug('UPnP device is missing the udn. Provided info: %r',
|
||||||
|
discovery_info)
|
||||||
|
return self.async_abort(reason='incomplete_device')
|
||||||
|
|
||||||
# store discovered device
|
# store discovered device
|
||||||
discovery_info['friendly_name'] = \
|
discovery_info['friendly_name'] = discovery_info.get('host', '')
|
||||||
'{} ({})'.format(discovery_info['host'], discovery_info['name'])
|
|
||||||
|
# add name if available
|
||||||
|
if discovery_info.get('name'):
|
||||||
|
discovery_info['friendly_name'] += ' ({name})'.format(
|
||||||
|
**discovery_info)
|
||||||
|
|
||||||
self._store_discovery_info(discovery_info)
|
self._store_discovery_info(discovery_info)
|
||||||
|
|
||||||
# ensure not already discovered/configured
|
# ensure not already discovered/configured
|
||||||
udn = discovery_info['udn']
|
if discovery_info.get('udn') in self._configured_upnp_igds:
|
||||||
if udn in self._configured_upnp_igds:
|
|
||||||
return self.async_abort(reason='already_configured')
|
return self.async_abort(reason='already_configured')
|
||||||
|
|
||||||
# auto config?
|
# auto config?
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"no_devices_discovered": "No UPnP/IGDs discovered",
|
"no_devices_discovered": "No UPnP/IGDs discovered",
|
||||||
|
"incomplete_device": "Ignoring incomplete UPnP device",
|
||||||
"already_configured": "UPnP/IGD is already configured",
|
"already_configured": "UPnP/IGD is already configured",
|
||||||
"no_sensors_or_port_mapping": "Enable at least sensors or port mapping"
|
"no_sensors_or_port_mapping": "Enable at least sensors or port mapping"
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,6 +176,27 @@ async def test_config_entry_created(hass):
|
||||||
assert result['title'] == 'Test device 1'
|
assert result['title'] == 'Test device 1'
|
||||||
|
|
||||||
|
|
||||||
|
async def test_flow_discovery_no_data(hass):
|
||||||
|
"""Test creation of device with auto_config."""
|
||||||
|
flow = upnp_config_flow.UpnpFlowHandler()
|
||||||
|
flow.hass = hass
|
||||||
|
|
||||||
|
# auto_config active
|
||||||
|
hass.data[upnp.DOMAIN] = {
|
||||||
|
'auto_config': {
|
||||||
|
'active': True,
|
||||||
|
'enable_port_mapping': False,
|
||||||
|
'enable_sensors': True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
# discovered device
|
||||||
|
result = await flow.async_step_discovery({})
|
||||||
|
|
||||||
|
assert result['type'] == 'abort'
|
||||||
|
assert result['reason'] == 'incomplete_device'
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_discovery_auto_config_sensors(hass):
|
async def test_flow_discovery_auto_config_sensors(hass):
|
||||||
"""Test creation of device with auto_config."""
|
"""Test creation of device with auto_config."""
|
||||||
flow = upnp_config_flow.UpnpFlowHandler()
|
flow = upnp_config_flow.UpnpFlowHandler()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue