diff --git a/homeassistant/components/yamaha_musiccast/__init__.py b/homeassistant/components/yamaha_musiccast/__init__.py index 06a242c6070..194168b2eee 100644 --- a/homeassistant/components/yamaha_musiccast/__init__.py +++ b/homeassistant/components/yamaha_musiccast/__init__.py @@ -39,11 +39,10 @@ SCAN_INTERVAL = timedelta(seconds=60) async def get_upnp_desc(hass: HomeAssistant, host: str): """Get the upnp description URL for a given host, using the SSPD scanner.""" ssdp_entries = await ssdp.async_get_discovery_info_by_st(hass, "upnp:rootdevice") - matches = [w for w in ssdp_entries if w.get("_host", "") == host] + matches = [w for w in ssdp_entries if w.ssdp_headers.get("_host", "") == host] upnp_desc = None for match in matches: - if match.get(ssdp.ATTR_SSDP_LOCATION): - upnp_desc = match[ssdp.ATTR_SSDP_LOCATION] + if upnp_desc := match.ssdp_location: break if not upnp_desc: diff --git a/homeassistant/components/yamaha_musiccast/config_flow.py b/homeassistant/components/yamaha_musiccast/config_flow.py index 2d4e723d745..4049a5d6a37 100644 --- a/homeassistant/components/yamaha_musiccast/config_flow.py +++ b/homeassistant/components/yamaha_musiccast/config_flow.py @@ -92,8 +92,13 @@ class MusicCastFlowHandler(ConfigFlow, domain=DOMAIN): return self.async_abort(reason="yxc_control_url_missing") self.serial_number = discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL] - self.host = urlparse(discovery_info.ssdp_location or "").hostname or "" self.upnp_description = discovery_info.ssdp_location + + # ssdp_location and hostname have been checked in check_yamaha_ssdp so it is safe to ignore type assignment + self.host = urlparse( + discovery_info.ssdp_location + ).hostname # type: ignore[assignment] + await self.async_set_unique_id(self.serial_number) self._abort_if_unique_id_configured( { diff --git a/tests/components/yamaha_musiccast/test_config_flow.py b/tests/components/yamaha_musiccast/test_config_flow.py index 6229f25f908..bddc350cff8 100644 --- a/tests/components/yamaha_musiccast/test_config_flow.py +++ b/tests/components/yamaha_musiccast/test_config_flow.py @@ -94,10 +94,15 @@ def mock_valid_discovery_information(): with patch( "homeassistant.components.ssdp.async_get_discovery_info_by_st", return_value=[ - { - "ssdp_location": "http://127.0.0.1:9000/MediaRenderer/desc.xml", - "_host": "127.0.0.1", - } + ssdp.SsdpServiceInfo( + ssdp_usn="mock_usn", + ssdp_st="mock_st", + ssdp_location="http://127.0.0.1:9000/MediaRenderer/desc.xml", + ssdp_headers={ + "_host": "127.0.0.1", + }, + upnp={}, + ) ], ): yield