|
| 1 | +"""Tests for pyscript config flow.""" |
| 2 | +import logging |
| 3 | + |
| 4 | +from custom_components.pyscript import PYSCRIPT_SCHEMA |
| 5 | +from custom_components.pyscript.const import CONF_ALLOW_ALL_IMPORTS, DOMAIN |
| 6 | +import pytest |
| 7 | +from pytest_homeassistant.async_mock import patch |
| 8 | + |
| 9 | +from homeassistant import data_entry_flow |
| 10 | +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER |
| 11 | + |
| 12 | +_LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(name="pyscript_bypass_setup") |
| 16 | +def pyscript_bypass_setup_fixture(): |
| 17 | + """Mock component setup.""" |
| 18 | + with patch("custom_components.pyscript.async_setup_entry", return_value=True): |
| 19 | + yield |
| 20 | + |
| 21 | + |
| 22 | +async def test_user_flow_minimum_fields(hass, pyscript_bypass_setup): |
| 23 | + """Test user config flow with minimum fields.""" |
| 24 | + # test form shows |
| 25 | + result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_USER}) |
| 26 | + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM |
| 27 | + assert result["step_id"] == "user" |
| 28 | + |
| 29 | + result = await hass.config_entries.flow.async_configure(result["flow_id"], user_input={}) |
| 30 | + |
| 31 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 32 | + assert CONF_ALLOW_ALL_IMPORTS in result["data"] |
| 33 | + assert not result["data"][CONF_ALLOW_ALL_IMPORTS] |
| 34 | + |
| 35 | + |
| 36 | +async def test_user_flow_all_fields(hass, pyscript_bypass_setup,) -> None: |
| 37 | + """Test user config flow with all fields.""" |
| 38 | + # test form shows |
| 39 | + result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_USER}) |
| 40 | + |
| 41 | + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM |
| 42 | + assert result["step_id"] == "user" |
| 43 | + |
| 44 | + result = await hass.config_entries.flow.async_configure( |
| 45 | + result["flow_id"], user_input={CONF_ALLOW_ALL_IMPORTS: True} |
| 46 | + ) |
| 47 | + |
| 48 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 49 | + assert CONF_ALLOW_ALL_IMPORTS in result["data"] |
| 50 | + assert result["data"][CONF_ALLOW_ALL_IMPORTS] |
| 51 | + |
| 52 | + |
| 53 | +async def test_user_already_configured(hass, pyscript_bypass_setup,) -> None: |
| 54 | + """Test service is already configured during user setup.""" |
| 55 | + result = await hass.config_entries.flow.async_init( |
| 56 | + DOMAIN, context={"source": SOURCE_USER}, data={CONF_ALLOW_ALL_IMPORTS: True} |
| 57 | + ) |
| 58 | + |
| 59 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 60 | + |
| 61 | + result = await hass.config_entries.flow.async_init( |
| 62 | + DOMAIN, context={"source": SOURCE_USER}, data={CONF_ALLOW_ALL_IMPORTS: True} |
| 63 | + ) |
| 64 | + |
| 65 | + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT |
| 66 | + assert result["reason"] == "single_instance_allowed" |
| 67 | + |
| 68 | + |
| 69 | +async def test_import_flow(hass, pyscript_bypass_setup): |
| 70 | + """Test import config flow works.""" |
| 71 | + result = await hass.config_entries.flow.async_init( |
| 72 | + DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({}) |
| 73 | + ) |
| 74 | + |
| 75 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 76 | + |
| 77 | + |
| 78 | +async def test_import_flow_update_entry(hass, pyscript_bypass_setup): |
| 79 | + """Test import config flow updates existing entry.""" |
| 80 | + result = await hass.config_entries.flow.async_init( |
| 81 | + DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({}) |
| 82 | + ) |
| 83 | + |
| 84 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 85 | + |
| 86 | + result = await hass.config_entries.flow.async_init( |
| 87 | + DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_ALLOW_ALL_IMPORTS: True} |
| 88 | + ) |
| 89 | + |
| 90 | + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT |
| 91 | + assert result["reason"] == "updated_entry" |
| 92 | + |
| 93 | + |
| 94 | +async def test_import_flow_no_update(hass, pyscript_bypass_setup): |
| 95 | + """Test import config flow doesn't update existing entry when data is same.""" |
| 96 | + result = await hass.config_entries.flow.async_init( |
| 97 | + DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({}) |
| 98 | + ) |
| 99 | + |
| 100 | + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY |
| 101 | + |
| 102 | + result = await hass.config_entries.flow.async_init( |
| 103 | + DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({}) |
| 104 | + ) |
| 105 | + |
| 106 | + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT |
| 107 | + assert result["reason"] == "already_configured_service" |
0 commit comments