diff --git a/tests/components/squeezebox/conftest.py b/tests/components/squeezebox/conftest.py index 2dd9403d53f..516a574658d 100644 --- a/tests/components/squeezebox/conftest.py +++ b/tests/components/squeezebox/conftest.py @@ -368,70 +368,39 @@ async def configure_squeezebox_media_player_button_platform( await hass.async_block_till_done(wait_background_tasks=True) -async def configure_squeezebox_switch_platform( - hass: HomeAssistant, - config_entry: MockConfigEntry, - lms: MagicMock, -) -> None: - """Configure a squeezebox config entry with appropriate mocks for switch.""" - with ( - patch( - "homeassistant.components.squeezebox.PLATFORMS", - [Platform.SWITCH], - ), - patch("homeassistant.components.squeezebox.Server", return_value=lms), - ): - # Set up the switch platform. - await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done(wait_background_tasks=True) - - @pytest.fixture -async def mock_alarms_player( - hass: HomeAssistant, - config_entry: MockConfigEntry, - lms: MagicMock, -) -> MagicMock: - """Mock the alarms of a configured player.""" - players = await lms.async_get_players() - players[0].alarms = [ - { - "id": TEST_ALARM_ID, - "enabled": True, - "time": "07:00", - "dow": [0, 1, 2, 3, 4, 5, 6], - "repeat": False, - "url": "CURRENT_PLAYLIST", - "volume": 50, - }, - ] - await configure_squeezebox_switch_platform(hass, config_entry, lms) - return players[0] +async def setup_squeezebox( + hass: HomeAssistant, config_entry: MockConfigEntry, lms: MagicMock +) -> MockConfigEntry: + """Fixture setting up a squeezebox config entry with one player.""" + with patch("homeassistant.components.squeezebox.Server", return_value=lms): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + return config_entry @pytest.fixture async def configured_player( - hass: HomeAssistant, config_entry: MockConfigEntry, lms: MagicMock + hass: HomeAssistant, + setup_squeezebox: MockConfigEntry, # depend on your setup fixture + lms: MagicMock, ) -> MagicMock: """Fixture mocking calls to pysqueezebox Player from a configured squeezebox.""" - await configure_squeezebox_media_player_platform(hass, config_entry, lms) - return (await lms.async_get_players())[0] - - -@pytest.fixture -async def configured_player_with_button( - hass: HomeAssistant, config_entry: MockConfigEntry, lms: MagicMock -) -> MagicMock: - """Fixture mocking calls to pysqueezebox Player from a configured squeezebox.""" - await configure_squeezebox_media_player_button_platform(hass, config_entry, lms) + # At this point, setup_squeezebox has already patched Server and set up the entry return (await lms.async_get_players())[0] @pytest.fixture async def configured_players( - hass: HomeAssistant, config_entry: MockConfigEntry, lms_factory: MagicMock + hass: HomeAssistant, + config_entry: MockConfigEntry, + lms_factory: MagicMock, ) -> list[MagicMock]: - """Fixture mocking calls to two pysqueezebox Players from a configured squeezebox.""" + """Fixture mocking calls to multiple pysqueezebox Players from a configured squeezebox.""" lms = lms_factory(3, uuid=SERVER_UUIDS[0]) - await configure_squeezebox_media_player_platform(hass, config_entry, lms) + + with patch("homeassistant.components.squeezebox.Server", return_value=lms): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + return await lms.async_get_players() diff --git a/tests/components/squeezebox/test_button.py b/tests/components/squeezebox/test_button.py index 53c4e9ef626..b1df528c283 100644 --- a/tests/components/squeezebox/test_button.py +++ b/tests/components/squeezebox/test_button.py @@ -1,14 +1,23 @@ """Tests for the squeezebox button component.""" -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch + +import pytest from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant +@pytest.fixture(autouse=True) +def squeezebox_button_platform(): + """Only set up the media_player platform for squeezebox tests.""" + with patch("homeassistant.components.squeezebox.PLATFORMS", [Platform.BUTTON]): + yield + + async def test_squeezebox_press( - hass: HomeAssistant, configured_player_with_button: MagicMock + hass: HomeAssistant, configured_player: MagicMock ) -> None: """Test press service call.""" await hass.services.async_call( @@ -18,6 +27,4 @@ async def test_squeezebox_press( blocking=True, ) - configured_player_with_button.async_query.assert_called_with( - "button", "preset_1.single" - ) + configured_player.async_query.assert_called_with("button", "preset_1.single") diff --git a/tests/components/squeezebox/test_init.py b/tests/components/squeezebox/test_init.py index 5cb7e19abb5..a39a3020038 100644 --- a/tests/components/squeezebox/test_init.py +++ b/tests/components/squeezebox/test_init.py @@ -3,10 +3,12 @@ from http import HTTPStatus from unittest.mock import MagicMock, patch +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.squeezebox.const import DOMAIN from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceRegistry @@ -15,6 +17,15 @@ from .conftest import TEST_MAC from tests.common import MockConfigEntry +@pytest.fixture(autouse=True) +def squeezebox_media_player_platform(): + """Only set up the media_player platform for squeezebox tests.""" + with patch( + "homeassistant.components.squeezebox.PLATFORMS", [Platform.MEDIA_PLAYER] + ): + yield + + async def test_init_api_fail( hass: HomeAssistant, config_entry: MockConfigEntry, diff --git a/tests/components/squeezebox/test_media_player.py b/tests/components/squeezebox/test_media_player.py index 6e3e5be0459..d04e68f2518 100644 --- a/tests/components/squeezebox/test_media_player.py +++ b/tests/components/squeezebox/test_media_player.py @@ -65,22 +65,27 @@ from homeassistant.const import ( SERVICE_VOLUME_UP, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util.dt import utcnow -from .conftest import ( - FAKE_VALID_ITEM_ID, - TEST_MAC, - TEST_VOLUME_STEP, - configure_squeezebox_media_player_platform, -) +from .conftest import FAKE_VALID_ITEM_ID, TEST_MAC, TEST_VOLUME_STEP from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform +@pytest.fixture(autouse=True) +def squeezebox_media_player_platform(): + """Only set up the media_player platform for squeezebox tests.""" + with patch( + "homeassistant.components.squeezebox.PLATFORMS", [Platform.MEDIA_PLAYER] + ): + yield + + async def test_entity_registry( hass: HomeAssistant, entity_registry: EntityRegistry, @@ -98,10 +103,11 @@ async def test_squeezebox_new_player_discovery( lms: MagicMock, player_factory: MagicMock, freezer: FrozenDateTimeFactory, + setup_squeezebox: MockConfigEntry, ) -> None: """Test discovery of a new squeezebox player.""" # Initial setup with one player (from the 'lms' fixture) - await configure_squeezebox_media_player_platform(hass, config_entry, lms) + # await setup_squeezebox await hass.async_block_till_done(wait_background_tasks=True) assert hass.states.get("media_player.test_player") is not None assert hass.states.get("media_player.test_player_2") is None diff --git a/tests/components/squeezebox/test_switch.py b/tests/components/squeezebox/test_switch.py index 2e6e9bafeb0..368fa1bf84a 100644 --- a/tests/components/squeezebox/test_switch.py +++ b/tests/components/squeezebox/test_switch.py @@ -1,14 +1,20 @@ """Tests for the Squeezebox alarm switch platform.""" from datetime import timedelta -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from freezegun.api import FrozenDateTimeFactory +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.squeezebox.const import PLAYER_UPDATE_INTERVAL from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN -from homeassistant.const import CONF_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.const import ( + CONF_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + Platform, +) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_registry import EntityRegistry @@ -17,6 +23,40 @@ from .conftest import TEST_ALARM_ID from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform +@pytest.fixture(autouse=True) +def squeezebox_alarm_platform(): + """Only set up the media_player platform for squeezebox tests.""" + with patch("homeassistant.components.squeezebox.PLATFORMS", [Platform.SWITCH]): + yield + + +@pytest.fixture +async def mock_alarms_player( + hass: HomeAssistant, + config_entry: MockConfigEntry, + lms: MagicMock, +) -> MagicMock: + """Mock the alarms of a configured player.""" + players = await lms.async_get_players() + players[0].alarms = [ + { + "id": TEST_ALARM_ID, + "enabled": True, + "time": "07:00", + "dow": [0, 1, 2, 3, 4, 5, 6], + "repeat": False, + "url": "CURRENT_PLAYLIST", + "volume": 50, + }, + ] + + with patch("homeassistant.components.squeezebox.Server", return_value=lms): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + return players[0] + + async def test_entity_registry( hass: HomeAssistant, entity_registry: EntityRegistry,