core/tests/components/omnilogic/test_config_flow.py

146 lines
4.5 KiB
Python

"""Test the Omnilogic config flow."""
from unittest.mock import patch
from omnilogic import LoginException, OmniLogicException
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.omnilogic.const import DOMAIN
from tests.common import MockConfigEntry
DATA = {"username": "test-username", "password": "test-password"}
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
with patch(
"homeassistant.components.omnilogic.config_flow.OmniLogic.connect",
return_value=True,
), patch(
"homeassistant.components.omnilogic.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["title"] == "Omnilogic"
assert result2["data"] == DATA
assert len(mock_setup_entry.mock_calls) == 1
async def test_already_configured(hass):
"""Test config flow when Omnilogic component is already setup."""
MockConfigEntry(domain="omnilogic", data=DATA).add_to_hass(hass)
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "abort"
assert result["reason"] == "single_instance_allowed"
async def test_with_invalid_credentials(hass):
"""Test with invalid credentials."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.omnilogic.OmniLogic.connect",
side_effect=LoginException,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
assert result["type"] == "form"
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass):
"""Test if invalid response or no connection returned from Hayward."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.omnilogic.OmniLogic.connect",
side_effect=OmniLogicException,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
assert result["type"] == "form"
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
async def test_with_unknown_error(hass):
"""Test with unknown error response from Hayward."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.omnilogic.OmniLogic.connect",
side_effect=Exception,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
assert result["type"] == "form"
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}
async def test_option_flow(hass):
"""Test option flow."""
entry = MockConfigEntry(domain=DOMAIN, data=DATA)
entry.add_to_hass(hass)
assert not entry.options
with patch(
"homeassistant.components.omnilogic.async_setup_entry", return_value=True
):
result = await hass.config_entries.options.async_init(
entry.entry_id,
data=None,
)
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={"polling_interval": 9},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == ""
assert result["data"]["polling_interval"] == 9