diff --git a/homeassistant/components/upnp/__init__.py b/homeassistant/components/upnp/__init__.py index 6b01d1ae7d77..2b3d1d9ea7b5 100644 --- a/homeassistant/components/upnp/__init__.py +++ b/homeassistant/components/upnp/__init__.py @@ -116,22 +116,31 @@ async def async_setup_entry(hass: HomeAssistant, entry: UpnpConfigEntry) -> bool }, ) - identifiers = {(DOMAIN, device.usn)} + identifiers = [(DOMAIN, device.usn)] if device.host: - identifiers.add((IDENTIFIER_HOST, device.host)) + identifiers.append((IDENTIFIER_HOST, device.host)) if device.serial_number: - identifiers.add((IDENTIFIER_SERIAL_NUMBER, device.serial_number)) + identifiers.append((IDENTIFIER_SERIAL_NUMBER, device.serial_number)) - connections = {(dr.CONNECTION_UPNP, discovery_info.ssdp_udn)} + connections = [(dr.CONNECTION_UPNP, discovery_info.ssdp_udn)] if discovery_info.ssdp_udn != device.udn: - connections.add((dr.CONNECTION_UPNP, device.udn)) + connections.append((dr.CONNECTION_UPNP, device.udn)) if device_mac_address: - connections.add((dr.CONNECTION_NETWORK_MAC, device_mac_address)) + connections.append((dr.CONNECTION_NETWORK_MAC, device_mac_address)) dev_registry = dr.async_get(hass) - device_entry = dev_registry.async_get_device( - identifiers=identifiers, connections=connections - ) + device_entry = None + for identifier in identifiers: + if device_entry := dev_registry.async_get_device_by_identifier( + identifier, entry.entry_id + ): + break + if device_entry is None: + for connection in connections: + if device_entry := dev_registry.async_get_device_by_connection( + connection, entry.entry_id + ): + break if device_entry: LOGGER.debug( "Found device using connections: %s, device_entry: %s", @@ -142,8 +151,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: UpnpConfigEntry) -> bool # No device found, create new device entry. device_entry = dev_registry.async_get_or_create( config_entry_id=entry.entry_id, - connections=connections, - identifiers=identifiers, + connections=set(connections), + identifiers=set(identifiers), name=device.name, manufacturer=device.manufacturer, model=device.model_name, @@ -155,7 +164,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: UpnpConfigEntry) -> bool # Update identifier. device_entry = dev_registry.async_update_device( device_entry.id, - new_identifiers=identifiers, + new_identifiers=set(identifiers), ) assert device_entry