Skip to content

Commit

Permalink
Fix upnp first discovered device is used (home-assistant#44151)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
2 people authored and balloob committed Dec 12, 2020
1 parent 8507b62 commit f858d6f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
14 changes: 12 additions & 2 deletions homeassistant/components/upnp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ async def async_discover_and_construct(
) -> Device:
"""Discovery devices and construct a Device for one."""
# pylint: disable=invalid-name
_LOGGER.debug("Constructing device: %s::%s", udn, st)

discovery_infos = await Device.async_discover(hass)
_LOGGER.debug("Discovered devices: %s", discovery_infos)
if not discovery_infos:
Expand All @@ -53,7 +55,7 @@ async def async_discover_and_construct(
# Get the discovery info with specified UDN/ST.
filtered = [di for di in discovery_infos if di[DISCOVERY_UDN] == udn]
if st:
filtered = [di for di in discovery_infos if di[DISCOVERY_ST] == st]
filtered = [di for di in filtered if di[DISCOVERY_ST] == st]
if not filtered:
_LOGGER.warning(
'Wanted UPnP/IGD device with UDN/ST "%s"/"%s" not found, aborting',
Expand All @@ -74,6 +76,7 @@ async def async_discover_and_construct(
)
_LOGGER.info("Detected multiple UPnP/IGD devices, using: %s", device_name)

_LOGGER.debug("Constructing from discovery_info: %s", discovery_info)
location = discovery_info[DISCOVERY_LOCATION]
return await Device.async_create_device(hass, location)

Expand Down Expand Up @@ -104,7 +107,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):

async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry) -> bool:
"""Set up UPnP/IGD device from a config entry."""
_LOGGER.debug("async_setup_entry, config_entry: %s", config_entry.data)
_LOGGER.debug("Setting up config entry: %s", config_entry.unique_id)

# Discover and construct.
udn = config_entry.data.get(CONFIG_ENTRY_UDN)
Expand All @@ -123,6 +126,11 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry)

# Ensure entry has a unique_id.
if not config_entry.unique_id:
_LOGGER.debug(
"Setting unique_id: %s, for config_entry: %s",
device.unique_id,
config_entry,
)
hass.config_entries.async_update_entry(
entry=config_entry,
unique_id=device.unique_id,
Expand Down Expand Up @@ -152,6 +160,8 @@ async def async_unload_entry(
hass: HomeAssistantType, config_entry: ConfigEntry
) -> bool:
"""Unload a UPnP/IGD device from a config entry."""
_LOGGER.debug("Unloading config entry: %s", config_entry.unique_id)

udn = config_entry.data.get(CONFIG_ENTRY_UDN)
if udn in hass.data[DOMAIN][DOMAIN_DEVICES]:
del hass.data[DOMAIN][DOMAIN_DEVICES][udn]
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/upnp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ async def async_step_ssdp(self, discovery_info: Mapping):
self._abort_if_unique_id_configured()

# Store discovery.
_LOGGER.debug("New discovery, continuing")
name = discovery_info.get("friendlyName", "")
discovery = {
DISCOVERY_UDN: udn,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/upnp/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def unique_id(self) -> str:

def __str__(self) -> str:
"""Get string representation."""
return f"IGD Device: {self.name}/{self.udn}"
return f"IGD Device: {self.name}/{self.udn}::{self.device_type}"

async def async_get_traffic_data(self) -> Mapping[str, any]:
"""
Expand Down

0 comments on commit f858d6f

Please sign in to comment.