2023-01-17 13:01:36 +00:00
|
|
|
"""Test the Open Thread Border Router config flow."""
|
2023-01-17 18:27:33 +00:00
|
|
|
from http import HTTPStatus
|
2023-01-17 13:01:36 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2023-01-17 18:27:33 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-01-17 13:01:36 +00:00
|
|
|
from homeassistant.components import hassio, otbr
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry, MockModule, mock_integration
|
2023-01-17 18:27:33 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2023-01-17 13:01:36 +00:00
|
|
|
|
|
|
|
HASSIO_DATA = hassio.HassioServiceInfo(
|
|
|
|
config={"host": "blah", "port": "bluh"},
|
|
|
|
name="blah",
|
|
|
|
slug="blah",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-17 18:27:33 +00:00
|
|
|
async def test_user_flow(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2023-01-17 17:50:29 +00:00
|
|
|
"""Test the user flow."""
|
2023-01-17 18:27:33 +00:00
|
|
|
url = "http://custom_url:1234"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", text="aa")
|
2023-01-17 17:50:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2023-01-17 18:27:33 +00:00
|
|
|
expected_data = {"url": url}
|
2023-01-17 17:50:29 +00:00
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
2023-01-17 18:27:33 +00:00
|
|
|
assert result["errors"] == {}
|
2023-01-17 17:50:29 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
2023-01-17 18:27:33 +00:00
|
|
|
"url": url,
|
2023-01-17 17:50:29 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
2023-01-20 13:32:41 +00:00
|
|
|
assert result["title"] == "Open Thread Border Router"
|
2023-01-17 17:50:29 +00:00
|
|
|
assert result["data"] == expected_data
|
|
|
|
assert result["options"] == {}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
|
|
|
|
assert config_entry.data == expected_data
|
|
|
|
assert config_entry.options == {}
|
2023-01-20 13:32:41 +00:00
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
2023-01-17 18:27:33 +00:00
|
|
|
assert config_entry.unique_id == otbr.DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user_flow_404(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
|
|
|
"""Test the user flow."""
|
|
|
|
url = "http://custom_url:1234"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NOT_FOUND)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"url": url,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
2023-01-17 17:50:29 +00:00
|
|
|
|
|
|
|
|
2023-01-17 13:01:36 +00:00
|
|
|
async def test_hassio_discovery_flow(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the hassio discovery flow."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
|
|
|
|
)
|
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
2023-01-20 13:32:41 +00:00
|
|
|
assert result["title"] == "Open Thread Border Router"
|
2023-01-17 13:01:36 +00:00
|
|
|
assert result["data"] == expected_data
|
|
|
|
assert result["options"] == {}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
|
|
|
|
assert config_entry.data == expected_data
|
|
|
|
assert config_entry.options == {}
|
2023-01-20 13:32:41 +00:00
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
2023-01-17 18:27:33 +00:00
|
|
|
assert config_entry.unique_id == otbr.DOMAIN
|
2023-01-17 13:01:36 +00:00
|
|
|
|
|
|
|
|
2023-01-17 18:27:33 +00:00
|
|
|
@pytest.mark.parametrize("source", ("hassio", "user"))
|
|
|
|
async def test_config_flow_single_entry(hass: HomeAssistant, source: str) -> None:
|
2023-01-17 13:01:36 +00:00
|
|
|
"""Test only a single entry is allowed."""
|
|
|
|
mock_integration(hass, MockModule("hassio"))
|
|
|
|
|
|
|
|
# Setup the config entry
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={},
|
|
|
|
domain=otbr.DOMAIN,
|
|
|
|
options={},
|
2023-01-20 13:32:41 +00:00
|
|
|
title="Open Thread Border Router",
|
2023-01-17 13:01:36 +00:00
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.homeassistant_yellow.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2023-01-17 18:27:33 +00:00
|
|
|
otbr.DOMAIN, context={"source": source}
|
2023-01-17 13:01:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "single_instance_allowed"
|
|
|
|
mock_setup_entry.assert_not_called()
|