Skip to content

Commit 1fc6ce3

Browse files
authored
Fix yamaha legacy receivers (home-assistant#122985)
1 parent 78d1cd7 commit 1fc6ce3

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

homeassistant/components/yamaha/media_player.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import contextlib
56
import logging
67
from typing import Any
78

@@ -129,11 +130,15 @@ def _discovery(config_info):
129130
else:
130131
_LOGGER.debug("Config Zones")
131132
zones = None
132-
for recv in rxv.find():
133-
if recv.ctrl_url == config_info.ctrl_url:
134-
_LOGGER.debug("Config Zones Matched %s", config_info.ctrl_url)
135-
zones = recv.zone_controllers()
136-
break
133+
134+
# Fix for upstream issues in rxv.find() with some hardware.
135+
with contextlib.suppress(AttributeError):
136+
for recv in rxv.find():
137+
if recv.ctrl_url == config_info.ctrl_url:
138+
_LOGGER.debug("Config Zones Matched %s", config_info.ctrl_url)
139+
zones = recv.zone_controllers()
140+
break
141+
137142
if not zones:
138143
_LOGGER.debug("Config Zones Fallback")
139144
zones = rxv.RXV(config_info.ctrl_url, config_info.name).zone_controllers()

tests/components/yamaha/test_media_player.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,20 @@ def device_fixture(main_zone):
5353
yield device
5454

5555

56-
async def test_setup_host(hass: HomeAssistant, device, main_zone) -> None:
56+
@pytest.fixture(name="device2")
57+
def device2_fixture(main_zone):
58+
"""Mock the yamaha device."""
59+
device = FakeYamahaDevice(
60+
"http://127.0.0.1:80/YamahaRemoteControl/ctrl", "Receiver 2", zones=[main_zone]
61+
)
62+
with (
63+
patch("rxv.RXV", return_value=device),
64+
patch("rxv.find", return_value=[device]),
65+
):
66+
yield device
67+
68+
69+
async def test_setup_host(hass: HomeAssistant, device, device2, main_zone) -> None:
5770
"""Test set up integration with host."""
5871
assert await async_setup_component(hass, MP_DOMAIN, CONFIG)
5972
await hass.async_block_till_done()
@@ -63,6 +76,28 @@ async def test_setup_host(hass: HomeAssistant, device, main_zone) -> None:
6376
assert state is not None
6477
assert state.state == "off"
6578

79+
with patch("rxv.find", return_value=[device2]):
80+
assert await async_setup_component(hass, MP_DOMAIN, CONFIG)
81+
await hass.async_block_till_done()
82+
83+
state = hass.states.get("media_player.yamaha_receiver_main_zone")
84+
85+
assert state is not None
86+
assert state.state == "off"
87+
88+
89+
async def test_setup_attribute_error(hass: HomeAssistant, device, main_zone) -> None:
90+
"""Test set up integration encountering an Attribute Error."""
91+
92+
with patch("rxv.find", side_effect=AttributeError):
93+
assert await async_setup_component(hass, MP_DOMAIN, CONFIG)
94+
await hass.async_block_till_done()
95+
96+
state = hass.states.get("media_player.yamaha_receiver_main_zone")
97+
98+
assert state is not None
99+
assert state.state == "off"
100+
66101

67102
async def test_setup_no_host(hass: HomeAssistant, device, main_zone) -> None:
68103
"""Test set up integration without host."""

0 commit comments

Comments
 (0)