diff --git a/homeassistant/components/yamaha/media_player.py b/homeassistant/components/yamaha/media_player.py index 507f485fcc7..a8200ea3373 100644 --- a/homeassistant/components/yamaha/media_player.py +++ b/homeassistant/components/yamaha/media_player.py @@ -2,6 +2,7 @@ from __future__ import annotations +import contextlib import logging from typing import Any @@ -129,11 +130,15 @@ def _discovery(config_info): else: _LOGGER.debug("Config Zones") zones = None - for recv in rxv.find(): - if recv.ctrl_url == config_info.ctrl_url: - _LOGGER.debug("Config Zones Matched %s", config_info.ctrl_url) - zones = recv.zone_controllers() - break + + # Fix for upstream issues in rxv.find() with some hardware. + with contextlib.suppress(AttributeError): + for recv in rxv.find(): + if recv.ctrl_url == config_info.ctrl_url: + _LOGGER.debug("Config Zones Matched %s", config_info.ctrl_url) + zones = recv.zone_controllers() + break + if not zones: _LOGGER.debug("Config Zones Fallback") zones = rxv.RXV(config_info.ctrl_url, config_info.name).zone_controllers() diff --git a/tests/components/yamaha/test_media_player.py b/tests/components/yamaha/test_media_player.py index 66d0a42f256..804b800aaef 100644 --- a/tests/components/yamaha/test_media_player.py +++ b/tests/components/yamaha/test_media_player.py @@ -53,7 +53,20 @@ def device_fixture(main_zone): yield device -async def test_setup_host(hass: HomeAssistant, device, main_zone) -> None: +@pytest.fixture(name="device2") +def device2_fixture(main_zone): + """Mock the yamaha device.""" + device = FakeYamahaDevice( + "http://127.0.0.1:80/YamahaRemoteControl/ctrl", "Receiver 2", zones=[main_zone] + ) + with ( + patch("rxv.RXV", return_value=device), + patch("rxv.find", return_value=[device]), + ): + yield device + + +async def test_setup_host(hass: HomeAssistant, device, device2, main_zone) -> None: """Test set up integration with host.""" assert await async_setup_component(hass, MP_DOMAIN, CONFIG) await hass.async_block_till_done() @@ -63,6 +76,28 @@ async def test_setup_host(hass: HomeAssistant, device, main_zone) -> None: assert state is not None assert state.state == "off" + with patch("rxv.find", return_value=[device2]): + assert await async_setup_component(hass, MP_DOMAIN, CONFIG) + await hass.async_block_till_done() + + state = hass.states.get("media_player.yamaha_receiver_main_zone") + + assert state is not None + assert state.state == "off" + + +async def test_setup_attribute_error(hass: HomeAssistant, device, main_zone) -> None: + """Test set up integration encountering an Attribute Error.""" + + with patch("rxv.find", side_effect=AttributeError): + assert await async_setup_component(hass, MP_DOMAIN, CONFIG) + await hass.async_block_till_done() + + state = hass.states.get("media_player.yamaha_receiver_main_zone") + + assert state is not None + assert state.state == "off" + async def test_setup_no_host(hass: HomeAssistant, device, main_zone) -> None: """Test set up integration without host."""