|
1 | 1 | """Provide common smhi fixtures."""
|
2 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import AsyncGenerator, Generator |
| 6 | +import json |
| 7 | +from typing import Any |
| 8 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 9 | + |
| 10 | +from pysmhi.smhi_forecast import SMHIForecast, SMHIPointForecast |
3 | 11 | import pytest
|
4 | 12 |
|
| 13 | +from homeassistant.components.smhi import PLATFORMS |
5 | 14 | from homeassistant.components.smhi.const import DOMAIN
|
| 15 | +from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE, Platform |
| 16 | +from homeassistant.core import HomeAssistant |
| 17 | + |
| 18 | +from . import TEST_CONFIG |
| 19 | + |
| 20 | +from tests.common import MockConfigEntry, load_fixture |
| 21 | +from tests.test_util.aiohttp import AiohttpClientMocker |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_setup_entry() -> Generator[AsyncMock]: |
| 26 | + """Override async_setup_entry.""" |
| 27 | + with patch( |
| 28 | + "homeassistant.components.smhi.async_setup_entry", return_value=True |
| 29 | + ) as mock_setup_entry: |
| 30 | + yield mock_setup_entry |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(name="load_platforms") |
| 34 | +async def patch_platform_constant() -> list[Platform]: |
| 35 | + """Return list of platforms to load.""" |
| 36 | + return PLATFORMS |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +async def load_int( |
| 41 | + hass: HomeAssistant, |
| 42 | + mock_client: SMHIPointForecast, |
| 43 | + load_platforms: list[Platform], |
| 44 | +) -> MockConfigEntry: |
| 45 | + """Set up the SMHI integration.""" |
| 46 | + hass.config.latitude = "59.32624" |
| 47 | + hass.config.longitude = "17.84197" |
| 48 | + config_entry = MockConfigEntry( |
| 49 | + domain=DOMAIN, |
| 50 | + data=TEST_CONFIG, |
| 51 | + entry_id="01JMZDH8N5PFHGJNYKKYCSCWER", |
| 52 | + unique_id="59.32624-17.84197", |
| 53 | + version=3, |
| 54 | + title="Test", |
| 55 | + ) |
| 56 | + |
| 57 | + config_entry.add_to_hass(hass) |
| 58 | + |
| 59 | + with patch("homeassistant.components.smhi.PLATFORMS", load_platforms): |
| 60 | + await hass.config_entries.async_setup(config_entry.entry_id) |
| 61 | + await hass.async_block_till_done() |
| 62 | + |
| 63 | + return config_entry |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture(name="mock_client") |
| 67 | +async def get_client( |
| 68 | + hass: HomeAssistant, |
| 69 | + get_data: tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]], |
| 70 | +) -> AsyncGenerator[MagicMock]: |
| 71 | + """Mock SMHIPointForecast client.""" |
| 72 | + |
| 73 | + with ( |
| 74 | + patch( |
| 75 | + "homeassistant.components.smhi.coordinator.SMHIPointForecast", |
| 76 | + autospec=True, |
| 77 | + ) as mock_client, |
| 78 | + patch( |
| 79 | + "homeassistant.components.smhi.config_flow.SMHIPointForecast", |
| 80 | + return_value=mock_client.return_value, |
| 81 | + ), |
| 82 | + ): |
| 83 | + client = mock_client.return_value |
| 84 | + client.async_get_daily_forecast.return_value = get_data[0] |
| 85 | + client.async_get_twice_daily_forecast.return_value = get_data[1] |
| 86 | + client.async_get_hourly_forecast.return_value = get_data[2] |
| 87 | + yield client |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture(name="get_data") |
| 91 | +async def get_data_from_library( |
| 92 | + hass: HomeAssistant, |
| 93 | + aioclient_mock: AiohttpClientMocker, |
| 94 | + load_json: dict[str, Any], |
| 95 | +) -> AsyncGenerator[tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]]]: |
| 96 | + """Get data from api.""" |
| 97 | + client = SMHIPointForecast( |
| 98 | + TEST_CONFIG[CONF_LOCATION][CONF_LONGITUDE], |
| 99 | + TEST_CONFIG[CONF_LOCATION][CONF_LATITUDE], |
| 100 | + aioclient_mock.create_session(hass.loop), |
| 101 | + ) |
| 102 | + with patch.object( |
| 103 | + client._api, |
| 104 | + "async_get_data", |
| 105 | + return_value=load_json, |
| 106 | + ): |
| 107 | + data_daily = await client.async_get_daily_forecast() |
| 108 | + data_twice_daily = await client.async_get_twice_daily_forecast() |
| 109 | + data_hourly = await client.async_get_hourly_forecast() |
6 | 110 |
|
7 |
| -from tests.common import load_fixture |
| 111 | + yield (data_daily, data_twice_daily, data_hourly) |
| 112 | + await client._api._session.close() |
8 | 113 |
|
9 | 114 |
|
10 |
| -@pytest.fixture(scope="package") |
11 |
| -def api_response(): |
12 |
| - """Return an API response.""" |
13 |
| - return load_fixture("smhi.json", DOMAIN) |
| 115 | +@pytest.fixture(name="load_json") |
| 116 | +def load_json_from_fixture( |
| 117 | + load_data: tuple[str, str, str], |
| 118 | + to_load: int, |
| 119 | +) -> dict[str, Any]: |
| 120 | + """Load fixture with json data and return.""" |
| 121 | + return json.loads(load_data[to_load]) |
14 | 122 |
|
15 | 123 |
|
16 |
| -@pytest.fixture(scope="package") |
17 |
| -def api_response_night(): |
18 |
| - """Return an API response for night only.""" |
19 |
| - return load_fixture("smhi_night.json", DOMAIN) |
| 124 | +@pytest.fixture(name="load_data", scope="package") |
| 125 | +def load_data_from_fixture() -> tuple[str, str, str]: |
| 126 | + """Load fixture with fixture data and return.""" |
| 127 | + return ( |
| 128 | + load_fixture("smhi.json", "smhi"), |
| 129 | + load_fixture("smhi_night.json", "smhi"), |
| 130 | + load_fixture("smhi_short.json", "smhi"), |
| 131 | + ) |
20 | 132 |
|
21 | 133 |
|
22 |
| -@pytest.fixture(scope="package") |
23 |
| -def api_response_lack_data(): |
24 |
| - """Return an API response.""" |
25 |
| - return load_fixture("smhi_short.json", DOMAIN) |
| 134 | +@pytest.fixture |
| 135 | +def to_load() -> int: |
| 136 | + """Fixture to load.""" |
| 137 | + return 0 |
0 commit comments