Only allow channel 15 during configuration of OTBR (#88874)

* Only allow channel 15 during automatic configuration of OTBR

* Also force channel 15 when creating a new network
This commit is contained in:
Erik Montnemery 2023-02-28 17:08:45 +01:00 committed by GitHub
parent e74613f8be
commit f93bd8ef2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 17 deletions

View file

@ -11,6 +11,8 @@ from homeassistant.components import hassio, otbr
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import DATASET_CH15, DATASET_CH16
from tests.common import MockConfigEntry, MockModule, mock_integration
from tests.test_util.aiohttp import AiohttpClientMocker
@ -94,7 +96,10 @@ async def test_user_flow_router_not_setup(
# Check we create a dataset and enable the router
assert aioclient_mock.mock_calls[-2][0] == "POST"
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
assert aioclient_mock.mock_calls[-2][2] == {"NetworkName": "home-assistant"}
assert aioclient_mock.mock_calls[-2][2] == {
"Channel": 15,
"NetworkName": "home-assistant",
}
assert aioclient_mock.mock_calls[-1][0] == "POST"
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
@ -226,7 +231,10 @@ async def test_hassio_discovery_flow_router_not_setup(
# Check we create a dataset and enable the router
assert aioclient_mock.mock_calls[-2][0] == "POST"
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
assert aioclient_mock.mock_calls[-2][2] == {"NetworkName": "home-assistant"}
assert aioclient_mock.mock_calls[-2][2] == {
"Channel": 15,
"NetworkName": "home-assistant",
}
assert aioclient_mock.mock_calls[-1][0] == "POST"
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"
@ -263,7 +271,7 @@ async def test_hassio_discovery_flow_router_not_setup_has_preferred(
with patch(
"homeassistant.components.otbr.config_flow.async_get_preferred_dataset",
return_value="aa",
return_value=DATASET_CH15.hex(),
), patch(
"homeassistant.components.otbr.async_setup_entry",
return_value=True,
@ -275,7 +283,60 @@ async def test_hassio_discovery_flow_router_not_setup_has_preferred(
# 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"
assert aioclient_mock.mock_calls[-2][2] == "aa"
assert aioclient_mock.mock_calls[-2][2] == DATASET_CH15.hex()
assert aioclient_mock.mock_calls[-1][0] == "POST"
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"
assert config_entry.unique_id == otbr.DOMAIN
async def test_hassio_discovery_flow_router_not_setup_has_preferred_2(
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, 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)
aioclient_mock.post(f"{url}/node/dataset/active", status=HTTPStatus.ACCEPTED)
aioclient_mock.post(f"{url}/node/state", status=HTTPStatus.OK)
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,
) 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] == "POST"
assert aioclient_mock.mock_calls[-2][1].path == "/node/dataset/active"
assert aioclient_mock.mock_calls[-2][2] == {
"Channel": 15,
"NetworkName": "home-assistant",
}
assert aioclient_mock.mock_calls[-1][0] == "POST"
assert aioclient_mock.mock_calls[-1][1].path == "/node/state"