2023-01-17 13:01:36 +00:00
|
|
|
"""Test the Open Thread Border Router config flow."""
|
2023-02-22 19:58:11 +00:00
|
|
|
import asyncio
|
2023-01-17 18:27:33 +00:00
|
|
|
from http import HTTPStatus
|
2023-03-22 13:03:39 +00:00
|
|
|
from typing import Any
|
2023-06-01 10:32:14 +00:00
|
|
|
from unittest.mock import patch
|
2023-01-17 13:01:36 +00:00
|
|
|
|
2023-02-22 19:58:11 +00:00
|
|
|
import aiohttp
|
2023-01-17 18:27:33 +00:00
|
|
|
import pytest
|
2023-02-22 19:58:11 +00:00
|
|
|
import python_otbr_api
|
2023-01-17 18:27:33 +00:00
|
|
|
|
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
|
|
|
|
|
2023-02-28 16:08:45 +00:00
|
|
|
from . import DATASET_CH15, DATASET_CH16
|
|
|
|
|
2023-01-17 13:01:36 +00:00
|
|
|
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(
|
2023-02-08 18:32:19 +00:00
|
|
|
config={"host": "core-silabs-multiprotocol", "port": 8081},
|
|
|
|
name="Silicon Labs Multiprotocol",
|
|
|
|
slug="otbr",
|
2023-04-25 07:48:47 +00:00
|
|
|
uuid="12345",
|
2023-01-17 13:01:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-02-08 18:32:19 +00:00
|
|
|
async def test_user_flow_router_not_setup(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
|
|
|
"""Test the user flow when the border router has no dataset.
|
|
|
|
|
|
|
|
This tests the behavior when the thread integration has no preferred dataset.
|
|
|
|
"""
|
|
|
|
url = "http://custom_url:1234"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NO_CONTENT)
|
2023-05-30 08:11:21 +00:00
|
|
|
aioclient_mock.put(f"{url}/node/dataset/active", status=HTTPStatus.CREATED)
|
|
|
|
aioclient_mock.put(f"{url}/node/state", status=HTTPStatus.OK)
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.config_flow.async_get_preferred_dataset",
|
|
|
|
return_value=None,
|
|
|
|
), 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"],
|
|
|
|
{
|
|
|
|
"url": url,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we create a dataset and enable the router
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][0] == "PUT"
|
2023-02-08 18:32:19 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
|
2023-02-28 16:08:45 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][2] == {
|
|
|
|
"Channel": 15,
|
|
|
|
"NetworkName": "home-assistant",
|
|
|
|
}
|
2023-02-08 18:32:19 +00:00
|
|
|
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][0] == "PUT"
|
2023-02-08 18:32:19 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
|
2023-02-22 20:31:02 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][2] == "enable"
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": "http://custom_url:1234",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "Open Thread Border Router"
|
|
|
|
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 == {}
|
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
|
|
|
assert config_entry.unique_id == otbr.DOMAIN
|
|
|
|
|
|
|
|
|
2023-01-17 18:27:33 +00:00
|
|
|
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-02-22 19:58:11 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"error",
|
|
|
|
[
|
|
|
|
asyncio.TimeoutError,
|
|
|
|
python_otbr_api.OTBRError,
|
|
|
|
aiohttp.ClientError,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_user_flow_connect_error(hass: HomeAssistant, error) -> None:
|
|
|
|
"""Test the user flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch("python_otbr_api.OTBR.get_active_dataset_tlvs", side_effect=error):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"url": "http://custom_url:1234",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 18:32:19 +00:00
|
|
|
async def test_hassio_discovery_flow(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2023-01-17 13:01:36 +00:00
|
|
|
"""Test the hassio discovery flow."""
|
2023-02-08 18:32:19 +00:00
|
|
|
url = "http://core-silabs-multiprotocol:8081"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", text="aa")
|
|
|
|
|
2023-01-17 13:01:36 +00:00
|
|
|
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-04-25 07:48:47 +00:00
|
|
|
assert config_entry.unique_id == HASSIO_DATA.uuid
|
2023-01-17 13:01:36 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:32:19 +00:00
|
|
|
async def test_hassio_discovery_flow_router_not_setup(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
|
|
|
"""Test the hassio discovery flow when the border router has no dataset.
|
|
|
|
|
|
|
|
This tests the behavior when the thread integration has no preferred dataset.
|
|
|
|
"""
|
|
|
|
url = "http://core-silabs-multiprotocol:8081"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NO_CONTENT)
|
2023-05-30 08:11:21 +00:00
|
|
|
aioclient_mock.put(f"{url}/node/dataset/active", status=HTTPStatus.CREATED)
|
|
|
|
aioclient_mock.put(f"{url}/node/state", status=HTTPStatus.OK)
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.config_flow.async_get_preferred_dataset",
|
|
|
|
return_value=None,
|
|
|
|
), 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
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we create a dataset and enable the router
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][0] == "PUT"
|
2023-02-08 18:32:19 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
|
2023-02-28 16:08:45 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][2] == {
|
|
|
|
"Channel": 15,
|
|
|
|
"NetworkName": "home-assistant",
|
|
|
|
}
|
2023-02-08 18:32:19 +00:00
|
|
|
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][0] == "PUT"
|
2023-02-08 18:32:19 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
|
2023-02-22 20:31:02 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][2] == "enable"
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "Open Thread Border Router"
|
|
|
|
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 == {}
|
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
2023-04-25 07:48:47 +00:00
|
|
|
assert config_entry.unique_id == HASSIO_DATA.uuid
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_hassio_discovery_flow_router_not_setup_has_preferred(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
|
|
|
"""Test the hassio discovery flow when the border router has no dataset.
|
|
|
|
|
|
|
|
This tests the behavior when the thread integration has a preferred dataset.
|
|
|
|
"""
|
|
|
|
url = "http://core-silabs-multiprotocol:8081"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NO_CONTENT)
|
2023-05-30 08:11:21 +00:00
|
|
|
aioclient_mock.put(f"{url}/node/dataset/active", status=HTTPStatus.CREATED)
|
|
|
|
aioclient_mock.put(f"{url}/node/state", status=HTTPStatus.OK)
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.config_flow.async_get_preferred_dataset",
|
2023-02-28 16:08:45 +00:00
|
|
|
return_value=DATASET_CH15.hex(),
|
2023-02-08 18:32:19 +00:00
|
|
|
), 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
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we create a dataset and enable the router
|
|
|
|
assert aioclient_mock.mock_calls[-2][0] == "PUT"
|
|
|
|
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
|
2023-02-28 16:08:45 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][2] == DATASET_CH15.hex()
|
|
|
|
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][0] == "PUT"
|
2023-02-28 16:08:45 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
|
|
|
|
assert aioclient_mock.mock_calls[-1][2] == "enable"
|
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "Open Thread Border Router"
|
|
|
|
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 == {}
|
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
2023-04-25 07:48:47 +00:00
|
|
|
assert config_entry.unique_id == HASSIO_DATA.uuid
|
2023-02-28 16:08:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_hassio_discovery_flow_router_not_setup_has_preferred_2(
|
2023-06-01 10:32:14 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
|
|
|
multiprotocol_addon_manager_mock,
|
2023-02-28 16:08:45 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the hassio discovery flow when the border router has no dataset.
|
|
|
|
|
|
|
|
This tests the behavior when the thread integration has a preferred dataset, but
|
|
|
|
the preferred dataset is not using channel 15.
|
|
|
|
"""
|
|
|
|
url = "http://core-silabs-multiprotocol:8081"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NO_CONTENT)
|
2023-05-30 08:11:21 +00:00
|
|
|
aioclient_mock.put(f"{url}/node/dataset/active", status=HTTPStatus.CREATED)
|
|
|
|
aioclient_mock.put(f"{url}/node/state", status=HTTPStatus.OK)
|
2023-02-28 16:08:45 +00:00
|
|
|
|
2023-06-01 10:32:14 +00:00
|
|
|
multiprotocol_addon_manager_mock.async_get_channel.return_value = 15
|
2023-03-28 10:34:25 +00:00
|
|
|
|
2023-02-28 16:08:45 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.otbr.config_flow.async_get_preferred_dataset",
|
|
|
|
return_value=DATASET_CH16.hex(),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.otbr.async_setup_entry",
|
|
|
|
return_value=True,
|
2023-06-01 10:32:14 +00:00
|
|
|
) as mock_setup_entry:
|
2023-02-28 16:08:45 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we create a dataset and enable the router
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][0] == "PUT"
|
2023-02-28 16:08:45 +00:00
|
|
|
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
|
|
|
|
assert aioclient_mock.mock_calls[-2][2] == {
|
|
|
|
"Channel": 15,
|
|
|
|
"NetworkName": "home-assistant",
|
|
|
|
}
|
2023-02-08 18:32:19 +00:00
|
|
|
|
2023-05-30 08:11:21 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][0] == "PUT"
|
2023-02-08 18:32:19 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
|
2023-02-22 20:31:02 +00:00
|
|
|
assert aioclient_mock.mock_calls[-1][2] == "enable"
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == "Open Thread Border Router"
|
|
|
|
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 == {}
|
|
|
|
assert config_entry.title == "Open Thread Border Router"
|
2023-04-25 07:48:47 +00:00
|
|
|
assert config_entry.unique_id == HASSIO_DATA.uuid
|
2023-02-08 18:32:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_hassio_discovery_flow_404(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
|
|
|
"""Test the user and discovery flows."""
|
|
|
|
url = "http://core-silabs-multiprotocol:8081"
|
|
|
|
aioclient_mock.get(f"{url}/node/dataset/active", status=HTTPStatus.NOT_FOUND)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "unknown"
|
|
|
|
|
|
|
|
|
2023-03-22 13:03:39 +00:00
|
|
|
async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the port can be updated."""
|
|
|
|
mock_integration(hass, MockModule("hassio"))
|
|
|
|
|
|
|
|
# Setup the config entry
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']+1}"
|
|
|
|
},
|
|
|
|
domain=otbr.DOMAIN,
|
|
|
|
options={},
|
|
|
|
source="hassio",
|
|
|
|
title="Open Thread Border Router",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "single_instance_allowed"
|
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}",
|
|
|
|
}
|
|
|
|
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
|
|
|
|
assert config_entry.data == expected_data
|
|
|
|
|
|
|
|
|
|
|
|
async def test_hassio_discovery_flow_new_port_other_addon(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the port is not updated if we get data for another addon hosting OTBR."""
|
|
|
|
mock_integration(hass, MockModule("hassio"))
|
|
|
|
|
|
|
|
# Setup the config entry
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
data={"url": f"http://openthread_border_router:{HASSIO_DATA.config['port']+1}"},
|
|
|
|
domain=otbr.DOMAIN,
|
|
|
|
options={},
|
|
|
|
source="hassio",
|
|
|
|
title="Open Thread Border Router",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "single_instance_allowed"
|
|
|
|
|
|
|
|
# Make sure the data was not updated
|
|
|
|
expected_data = {
|
|
|
|
"url": f"http://openthread_border_router:{HASSIO_DATA.config['port']+1}",
|
|
|
|
}
|
|
|
|
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
|
|
|
|
assert config_entry.data == expected_data
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("source", "data"), [("hassio", HASSIO_DATA), ("user", None)])
|
|
|
|
async def test_config_flow_single_entry(
|
|
|
|
hass: HomeAssistant, source: str, data: Any
|
|
|
|
) -> 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-03-22 13:03:39 +00:00
|
|
|
otbr.DOMAIN, context={"source": source}, data=data
|
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()
|