diff --git a/tests/components/openuv/conftest.py b/tests/components/openuv/conftest.py new file mode 100644 index 00000000000..f97878da9db --- /dev/null +++ b/tests/components/openuv/conftest.py @@ -0,0 +1,56 @@ +"""Define test fixtures for OpenUV.""" +from unittest.mock import patch + +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, +) +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +@pytest.fixture(name="config_entry") +def config_entry_fixture(hass, config, unique_id): + """Define a config entry fixture.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=unique_id, + data=config, + options={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 3.5}, + ) + entry.add_to_hass(hass) + return entry + + +@pytest.fixture(name="config") +def config_fixture(hass): + """Define a config entry data fixture.""" + return { + CONF_API_KEY: "abcde12345", + CONF_ELEVATION: 0, + CONF_LATITUDE: 51.528308, + CONF_LONGITUDE: -0.3817765, + } + + +@pytest.fixture(name="setup_openuv") +async def setup_openuv_fixture(hass, config): + """Define a fixture to set up OpenUV.""" + with patch("homeassistant.components.openuv.Client.uv_index"), patch( + "homeassistant.components.openuv.Client.uv_protection_window" + ), patch("homeassistant.components.openuv.PLATFORMS", []): + assert await async_setup_component(hass, DOMAIN, config) + await hass.async_block_till_done() + yield + + +@pytest.fixture(name="unique_id") +def unique_id_fixture(hass): + """Define a config entry unique ID fixture.""" + return "51.528308, -0.3817765" diff --git a/tests/components/openuv/test_config_flow.py b/tests/components/openuv/test_config_flow.py index 8afd9803d7c..8960d39a5b9 100644 --- a/tests/components/openuv/test_config_flow.py +++ b/tests/components/openuv/test_config_flow.py @@ -13,107 +13,60 @@ from homeassistant.const import ( CONF_LONGITUDE, ) -from tests.common import MockConfigEntry - -async def test_duplicate_error(hass): +async def test_duplicate_error(hass, config, config_entry): """Test that errors are shown when duplicates are added.""" - conf = { - CONF_API_KEY: "12345abcde", - CONF_ELEVATION: 59.1234, - CONF_LATITUDE: 39.128712, - CONF_LONGITUDE: -104.9812612, - } - - MockConfigEntry( - domain=DOMAIN, unique_id="39.128712, -104.9812612", data=conf - ).add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data=conf + DOMAIN, context={"source": SOURCE_USER}, data=config ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" -async def test_invalid_api_key(hass): +async def test_invalid_api_key(hass, config): """Test that an invalid API key throws an error.""" - conf = { - CONF_API_KEY: "12345abcde", - CONF_ELEVATION: 59.1234, - CONF_LATITUDE: 39.128712, - CONF_LONGITUDE: -104.9812612, - } - with patch( - "pyopenuv.client.Client.uv_index", + "homeassistant.components.openuv.Client.uv_index", side_effect=InvalidApiKeyError, ): result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data=conf + DOMAIN, context={"source": SOURCE_USER}, data=config ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["errors"] == {CONF_API_KEY: "invalid_api_key"} -async def test_options_flow(hass): +async def test_options_flow(hass, config_entry): """Test config flow options.""" - conf = { - CONF_API_KEY: "12345abcde", - CONF_ELEVATION: 59.1234, - CONF_LATITUDE: 39.128712, - CONF_LONGITUDE: -104.9812612, - } - - config_entry = MockConfigEntry( - domain=DOMAIN, - unique_id="abcde12345", - data=conf, - ) - config_entry.add_to_hass(hass) - with patch("homeassistant.components.openuv.async_setup_entry", return_value=True): await hass.config_entries.async_setup(config_entry.entry_id) result = await hass.config_entries.options.async_init(config_entry.entry_id) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert config_entry.options == {CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0} -async def test_step_user(hass): +async def test_step_user(hass, config, setup_openuv): """Test that the user step works.""" - conf = { - CONF_API_KEY: "12345abcde", - CONF_ELEVATION: 59.1234, - CONF_LATITUDE: 39.128712, - CONF_LONGITUDE: -104.9812612, + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=config + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "51.528308, -0.3817765" + assert result["data"] == { + CONF_API_KEY: "abcde12345", + CONF_ELEVATION: 0, + CONF_LATITUDE: 51.528308, + CONF_LONGITUDE: -0.3817765, } - - with patch( - "homeassistant.components.openuv.async_setup_entry", return_value=True - ), patch("pyopenuv.client.Client.uv_index"): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["step_id"] == "user" - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data=conf - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result["title"] == "39.128712, -104.9812612" - assert result["data"] == { - CONF_API_KEY: "12345abcde", - CONF_ELEVATION: 59.1234, - CONF_LATITUDE: 39.128712, - CONF_LONGITUDE: -104.9812612, - }