Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sonos setup fails with unhandled exceptions on discovery messages #90648

Merged
merged 22 commits into from
May 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 43 additions & 5 deletions tests/components/sonos/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import sonos, zeroconf
from homeassistant.components.sonos import SonosDiscoveryManager
from homeassistant.components.sonos.const import DATA_SONOS_DISCOVERY_MANAGER
from homeassistant.components.sonos.const import (
DATA_SONOS_DISCOVERY_MANAGER,
SONOS_SPEAKER_ACTIVITY,
)
from homeassistant.components.sonos.exception import SonosUpdateError
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.setup import async_setup_component

from .conftest import MockSoCo, SoCoMockFactory
Expand Down Expand Up @@ -274,6 +278,29 @@
)


class SpeakerActivity:
"""Unit test class to track speaker activity messages."""

def __init__(self, hass: HomeAssistant, soco: MockSoCo) -> None:
"""Create the object from soco."""
self.soco = soco
self.hass = hass
self.call_count: int = 0
self.event = asyncio.Event()
async_dispatcher_connect(
self.hass,
f"{SONOS_SPEAKER_ACTIVITY}-{self.soco.uid}",
self.speaker_activity,
)

@callback
def speaker_activity(self, source: str) -> None:
"""Track the last activity on this speaker, set availability and resubscribe."""
if source == "manual zone scan":
self.event.set()
self.call_count += 1


async def test_async_poll_manual_hosts_5(
hass: HomeAssistant,
soco_factory: SoCoMockFactory,
Expand All @@ -284,9 +311,11 @@
soco_1 = soco_factory.cache_mock(MockSoCo(), "10.10.10.1", "Living Room")
soco_1.renderingControl = Mock()
soco_1.renderingControl.GetVolume = Mock()
speaker_1_activity = SpeakerActivity(hass, soco_1)
soco_2 = soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom")
soco_2.renderingControl = Mock()
soco_2.renderingControl.GetVolume = Mock()
speaker_2_activity = SpeakerActivity(hass, soco_2)
with patch(
"homeassistant.components.sonos.DISCOVERY_INTERVAL"
) as mock_discovery_interval:
Expand All @@ -300,8 +329,12 @@

with caplog.at_level(logging.DEBUG):
caplog.clear()
await asyncio.sleep(1.0)
await asyncio.wait(
[speaker_1_activity.event.wait(), speaker_2_activity.event.wait()]
)
await hass.async_block_till_done()
assert speaker_1_activity.call_count == 1
assert speaker_2_activity.call_count == 1
assert "Activity on Living Room" in caplog.text
assert "Activity on Bedroom" in caplog.text

Expand All @@ -318,10 +351,12 @@
soco_1.renderingControl = Mock()
soco_1.renderingControl.GetVolume = Mock()
soco_1.renderingControl.GetVolume.side_effect = SonosUpdateError()
speaker_1_activity = SpeakerActivity(hass, soco_1)
soco_2 = soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom")
soco_2.renderingControl = Mock()
soco_2.renderingControl.GetVolume = Mock()
soco_2.renderingControl.GetVolume.side_effect = SonosUpdateError()
speaker_2_activity = SpeakerActivity(hass, soco_2)

with patch(
"homeassistant.components.sonos.DISCOVERY_INTERVAL"
Expand All @@ -335,11 +370,14 @@

with caplog.at_level(logging.DEBUG):
caplog.clear()
# Delay for second iteration and other tasks
await asyncio.sleep(1.0)
# The discovery events should not fire, wait with a timeout.
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(speaker_1_activity.event.wait(), 1.0)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
await hass.async_block_till_done()
assert "Activity on Living Room" not in caplog.text
assert "Activity on Bedroom" not in caplog.text
assert speaker_1_activity.call_count == 0
assert speaker_2_activity.call_count == 0


async def test_async_poll_manual_hosts_7(
Expand Down Expand Up @@ -374,7 +412,7 @@
soco_factory: SoCoMockFactory,
entity_registry: er.EntityRegistry,
) -> None:
"""Test both succeed, speaker do not exist, invisible zone."""

Check failure on line 415 in tests/components/sonos/test_init.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.11 (sonos)

test_async_poll_manual_hosts_5 TypeError: Passing coroutines is forbidden, use tasks explicitly.
soco_1 = soco_factory.cache_mock(
_MockSoCoVisibleZones(), "10.10.10.1", "Living Room"
)
Expand Down