2019-07-29 09:21:26 +02:00
|
|
|
"""Tests for the Velbus config flow."""
|
2021-09-13 08:22:46 +02:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-01-01 22:31:56 +01:00
|
|
|
|
2019-07-29 09:21:26 +02:00
|
|
|
import pytest
|
2021-09-13 08:22:46 +02:00
|
|
|
from velbusaio.exceptions import VelbusConnectionFailed
|
2019-07-29 09:21:26 +02:00
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.velbus import config_flow
|
2019-12-09 09:36:42 +01:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_PORT
|
2021-11-08 17:26:00 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-07-29 09:21:26 +02:00
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
from .const import PORT_SERIAL, PORT_TCP
|
2019-07-29 09:21:26 +02:00
|
|
|
|
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def override_async_setup_entry() -> AsyncMock:
|
|
|
|
"""Override async_setup_entry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.velbus.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
yield mock_setup_entry
|
2019-07-29 09:21:26 +02:00
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
|
|
|
|
@pytest.fixture(name="controller_connection_failed")
|
|
|
|
def mock_controller_connection_failed():
|
2019-07-29 09:21:26 +02:00
|
|
|
"""Mock the velbus controller with an assert."""
|
2021-09-13 08:22:46 +02:00
|
|
|
with patch("velbusaio.controller.Velbus", side_effect=VelbusConnectionFailed()):
|
2019-07-29 09:21:26 +02:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
def init_config_flow(hass: HomeAssistant):
|
2019-07-29 09:21:26 +02:00
|
|
|
"""Init a configuration flow."""
|
|
|
|
flow = config_flow.VelbusConfigFlow()
|
|
|
|
flow.hass = hass
|
|
|
|
return flow
|
|
|
|
|
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
@pytest.mark.usefixtures("controller")
|
|
|
|
async def test_user(hass: HomeAssistant):
|
2019-07-29 09:21:26 +02:00
|
|
|
"""Test user config."""
|
|
|
|
flow = init_config_flow(hass)
|
|
|
|
|
|
|
|
result = await flow.async_step_user()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "user"
|
2019-07-29 09:21:26 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(
|
|
|
|
{CONF_NAME: "Velbus Test Serial", CONF_PORT: PORT_SERIAL}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "velbus_test_serial"
|
|
|
|
assert result["data"][CONF_PORT] == PORT_SERIAL
|
2019-07-29 09:21:26 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(
|
|
|
|
{CONF_NAME: "Velbus Test TCP", CONF_PORT: PORT_TCP}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "velbus_test_tcp"
|
|
|
|
assert result["data"][CONF_PORT] == PORT_TCP
|
2019-07-29 09:21:26 +02:00
|
|
|
|
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
@pytest.mark.usefixtures("controller_connection_failed")
|
|
|
|
async def test_user_fail(hass: HomeAssistant):
|
2019-07-29 09:21:26 +02:00
|
|
|
"""Test user config."""
|
|
|
|
flow = init_config_flow(hass)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(
|
|
|
|
{CONF_NAME: "Velbus Test Serial", CONF_PORT: PORT_SERIAL}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-30 13:53:03 +02:00
|
|
|
assert result["errors"] == {CONF_PORT: "cannot_connect"}
|
2019-07-29 09:21:26 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(
|
|
|
|
{CONF_NAME: "Velbus Test TCP", CONF_PORT: PORT_TCP}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-30 13:53:03 +02:00
|
|
|
assert result["errors"] == {CONF_PORT: "cannot_connect"}
|
2019-07-29 09:21:26 +02:00
|
|
|
|
|
|
|
|
2021-11-08 17:26:00 +01:00
|
|
|
@pytest.mark.usefixtures("config_entry")
|
|
|
|
async def test_abort_if_already_setup(hass: HomeAssistant):
|
2021-11-29 16:33:26 +01:00
|
|
|
"""Test we abort if Velbus is already setup."""
|
2019-07-29 09:21:26 +02:00
|
|
|
flow = init_config_flow(hass)
|
|
|
|
|
2021-11-29 16:33:26 +01:00
|
|
|
result = await flow.async_step_user({CONF_PORT: PORT_TCP, CONF_NAME: "velbus test"})
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-30 13:53:03 +02:00
|
|
|
assert result["errors"] == {"port": "already_configured"}
|