2022-01-22 23:52:25 +00:00
|
|
|
"""Define test fixtures for OpenUV."""
|
2022-01-23 00:38:38 +00:00
|
|
|
import json
|
2023-01-06 09:51:46 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2022-01-22 23:52:25 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.openuv import CONF_FROM_WINDOW, CONF_TO_WINDOW, DOMAIN
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_ELEVATION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
)
|
|
|
|
|
2022-01-23 00:38:38 +00:00
|
|
|
from tests.common import MockConfigEntry, load_fixture
|
2022-01-22 23:52:25 +00:00
|
|
|
|
2023-01-06 09:51:46 +00:00
|
|
|
TEST_API_KEY = "abcde12345"
|
|
|
|
TEST_ELEVATION = 0
|
|
|
|
TEST_LATITUDE = 51.528308
|
|
|
|
TEST_LONGITUDE = -0.3817765
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="client")
|
|
|
|
def client_fixture(data_protection_window, data_uv_index):
|
|
|
|
"""Define a mock Client object."""
|
|
|
|
return Mock(
|
|
|
|
uv_index=AsyncMock(return_value=data_uv_index),
|
|
|
|
uv_protection_window=AsyncMock(return_value=data_protection_window),
|
|
|
|
)
|
|
|
|
|
2022-01-22 23:52:25 +00:00
|
|
|
|
|
|
|
@pytest.fixture(name="config_entry")
|
2022-10-08 21:05:45 +00:00
|
|
|
def config_entry_fixture(hass, config):
|
2022-01-22 23:52:25 +00:00
|
|
|
"""Define a config entry fixture."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
2022-10-08 21:05:45 +00:00
|
|
|
unique_id=f"{config[CONF_LATITUDE]}, {config[CONF_LONGITUDE]}",
|
2022-01-22 23:52:25 +00:00
|
|
|
data=config,
|
|
|
|
options={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 3.5},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="config")
|
2023-01-06 09:51:46 +00:00
|
|
|
def config_fixture():
|
2022-01-22 23:52:25 +00:00
|
|
|
"""Define a config entry data fixture."""
|
|
|
|
return {
|
2023-01-06 09:51:46 +00:00
|
|
|
CONF_API_KEY: TEST_API_KEY,
|
|
|
|
CONF_ELEVATION: TEST_ELEVATION,
|
|
|
|
CONF_LATITUDE: TEST_LATITUDE,
|
|
|
|
CONF_LONGITUDE: TEST_LONGITUDE,
|
2022-01-22 23:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-18 16:06:23 +00:00
|
|
|
@pytest.fixture(name="data_protection_window", scope="package")
|
2022-01-23 00:38:38 +00:00
|
|
|
def data_protection_window_fixture():
|
|
|
|
"""Define a fixture to return UV protection window data."""
|
|
|
|
return json.loads(load_fixture("protection_window_data.json", "openuv"))
|
|
|
|
|
|
|
|
|
2022-10-18 16:06:23 +00:00
|
|
|
@pytest.fixture(name="data_uv_index", scope="package")
|
2022-01-23 00:38:38 +00:00
|
|
|
def data_uv_index_fixture():
|
|
|
|
"""Define a fixture to return UV index data."""
|
|
|
|
return json.loads(load_fixture("uv_index_data.json", "openuv"))
|
|
|
|
|
|
|
|
|
2023-01-06 09:51:46 +00:00
|
|
|
@pytest.fixture(name="mock_pyopenuv")
|
|
|
|
async def mock_pyopenuv_fixture(client):
|
|
|
|
"""Define a fixture to patch pyopenuv."""
|
2022-01-23 00:38:38 +00:00
|
|
|
with patch(
|
2023-01-06 09:51:46 +00:00
|
|
|
"homeassistant.components.openuv.config_flow.Client", return_value=client
|
|
|
|
), patch("homeassistant.components.openuv.Client", return_value=client):
|
2022-01-22 23:52:25 +00:00
|
|
|
yield
|
2023-01-06 09:51:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="setup_config_entry")
|
|
|
|
async def setup_config_entry_fixture(hass, config_entry, mock_pyopenuv):
|
|
|
|
"""Define a fixture to set up openuv."""
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|