|
| 1 | +"""Common fixtures for the Control4 tests.""" |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator, Generator |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from homeassistant.components.control4.const import DOMAIN |
| 9 | +from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME |
| 10 | + |
| 11 | +from tests.common import MockConfigEntry, load_fixture |
| 12 | + |
| 13 | +MOCK_HOST = "192.168.1.100" |
| 14 | +MOCK_USERNAME = "test-username" |
| 15 | +MOCK_PASSWORD = "test-password" |
| 16 | +MOCK_CONTROLLER_UNIQUE_ID = "control4_test_123" |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_config_entry() -> MockConfigEntry: |
| 21 | + """Return the default mocked config entry.""" |
| 22 | + return MockConfigEntry( |
| 23 | + domain=DOMAIN, |
| 24 | + data={ |
| 25 | + CONF_HOST: MOCK_HOST, |
| 26 | + CONF_USERNAME: MOCK_USERNAME, |
| 27 | + CONF_PASSWORD: MOCK_PASSWORD, |
| 28 | + "controller_unique_id": MOCK_CONTROLLER_UNIQUE_ID, |
| 29 | + }, |
| 30 | + unique_id="00:aa:00:aa:00:aa", |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def mock_c4_account() -> Generator[MagicMock]: |
| 36 | + """Mock a Control4 Account client.""" |
| 37 | + with patch( |
| 38 | + "homeassistant.components.control4.C4Account", autospec=True |
| 39 | + ) as mock_account_class: |
| 40 | + mock_account = mock_account_class.return_value |
| 41 | + mock_account.getAccountBearerToken = AsyncMock() |
| 42 | + mock_account.getAccountControllers = AsyncMock( |
| 43 | + return_value={"href": "https://example.com"} |
| 44 | + ) |
| 45 | + mock_account.getDirectorBearerToken = AsyncMock(return_value={"token": "test"}) |
| 46 | + mock_account.getControllerOSVersion = AsyncMock(return_value="3.2.0") |
| 47 | + yield mock_account |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def mock_c4_director() -> Generator[MagicMock]: |
| 52 | + """Mock a Control4 Director client.""" |
| 53 | + with patch( |
| 54 | + "homeassistant.components.control4.C4Director", autospec=True |
| 55 | + ) as mock_director_class: |
| 56 | + mock_director = mock_director_class.return_value |
| 57 | + # Default: Multi-room setup (room with sources, room without sources) |
| 58 | + # Note: The API returns JSON strings, so we load fixtures as strings |
| 59 | + mock_director.getAllItemInfo = AsyncMock( |
| 60 | + return_value=load_fixture("director_all_items.json", DOMAIN) |
| 61 | + ) |
| 62 | + mock_director.getUiConfiguration = AsyncMock( |
| 63 | + return_value=load_fixture("ui_configuration.json", DOMAIN) |
| 64 | + ) |
| 65 | + yield mock_director |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def mock_update_variables() -> Generator[AsyncMock]: |
| 70 | + """Mock the update_variables_for_config_entry function.""" |
| 71 | + |
| 72 | + async def _mock_update_variables(*args, **kwargs): |
| 73 | + return { |
| 74 | + 1: { |
| 75 | + "POWER_STATE": True, |
| 76 | + "CURRENT_VOLUME": 50, |
| 77 | + "IS_MUTED": False, |
| 78 | + "CURRENT_VIDEO_DEVICE": 100, |
| 79 | + "CURRENT MEDIA INFO": {}, |
| 80 | + "PLAYING": False, |
| 81 | + "PAUSED": False, |
| 82 | + "STOPPED": False, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + with patch( |
| 87 | + "homeassistant.components.control4.media_player.update_variables_for_config_entry", |
| 88 | + new=_mock_update_variables, |
| 89 | + ) as mock_update: |
| 90 | + yield mock_update |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def platforms() -> list[str]: |
| 95 | + """Platforms which should be loaded during the test.""" |
| 96 | + return ["media_player"] |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture(autouse=True) |
| 100 | +async def mock_patch_platforms(platforms: list[str]) -> AsyncGenerator[None]: |
| 101 | + """Fixture to set up platforms for tests.""" |
| 102 | + with patch("homeassistant.components.control4.PLATFORMS", platforms): |
| 103 | + yield |
0 commit comments