Fix OTBR reset (#94157)

pull/94171/head
Erik Montnemery 2023-06-07 18:42:40 +02:00 committed by GitHub
parent 6af1beb6bf
commit 1fa2fb4639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 6 deletions

View File

@ -8,5 +8,5 @@
"documentation": "https://www.home-assistant.io/integrations/otbr",
"integration_type": "service",
"iot_class": "local_polling",
"requirements": ["python-otbr-api==2.1.0"]
"requirements": ["python-otbr-api==2.2.0"]
}

View File

@ -95,6 +95,11 @@ class OTBRData:
"""Create an active operational dataset."""
return await self.api.create_active_dataset(dataset)
@_handle_otbr_error
async def delete_active_dataset(self) -> None:
"""Delete the active operational dataset."""
return await self.api.delete_active_dataset()
@_handle_otbr_error
async def set_active_dataset_tlvs(self, dataset: bytes) -> None:
"""Set current active operational dataset in TLVS format."""

View File

@ -81,6 +81,12 @@ async def websocket_create_network(
connection.send_error(msg["id"], "set_enabled_failed", str(exc))
return
try:
await data.delete_active_dataset()
except HomeAssistantError as exc:
connection.send_error(msg["id"], "delete_active_dataset_failed", str(exc))
return
try:
await data.create_active_dataset(
python_otbr_api.ActiveDataSet(

View File

@ -7,6 +7,6 @@
"documentation": "https://www.home-assistant.io/integrations/thread",
"integration_type": "service",
"iot_class": "local_polling",
"requirements": ["python-otbr-api==2.1.0", "pyroute2==0.7.5"],
"requirements": ["python-otbr-api==2.2.0", "pyroute2==0.7.5"],
"zeroconf": ["_meshcop._udp.local."]
}

View File

@ -2128,7 +2128,7 @@ python-opensky==0.0.8
# homeassistant.components.otbr
# homeassistant.components.thread
python-otbr-api==2.1.0
python-otbr-api==2.2.0
# homeassistant.components.picnic
python-picnic-api==1.1.0

View File

@ -1554,7 +1554,7 @@ python-nest==4.2.0
# homeassistant.components.otbr
# homeassistant.components.thread
python-otbr-api==2.1.0
python-otbr-api==2.2.0
# homeassistant.components.picnic
python-picnic-api==1.1.0

View File

@ -84,6 +84,8 @@ async def test_create_network(
with patch(
"python_otbr_api.OTBR.create_active_dataset"
) as create_dataset_mock, patch(
"python_otbr_api.OTBR.delete_active_dataset"
) as delete_dataset_mock, patch(
"python_otbr_api.OTBR.set_enabled"
) as set_enabled_mock, patch(
"python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16
@ -99,6 +101,7 @@ async def test_create_network(
create_dataset_mock.assert_called_once_with(
python_otbr_api.models.ActiveDataSet(channel=15, network_name="home-assistant")
)
delete_dataset_mock.assert_called_once_with()
assert len(set_enabled_mock.mock_calls) == 2
assert set_enabled_mock.mock_calls[0][1][0] is False
assert set_enabled_mock.mock_calls[1][1][0] is True
@ -151,7 +154,7 @@ async def test_create_network_fails_2(
), patch(
"python_otbr_api.OTBR.create_active_dataset",
side_effect=python_otbr_api.OTBRError,
):
), patch("python_otbr_api.OTBR.delete_active_dataset"):
await websocket_client.send_json_auto_id({"type": "otbr/create_network"})
msg = await websocket_client.receive_json()
@ -171,6 +174,8 @@ async def test_create_network_fails_3(
side_effect=[None, python_otbr_api.OTBRError],
), patch(
"python_otbr_api.OTBR.create_active_dataset",
), patch(
"python_otbr_api.OTBR.delete_active_dataset"
):
await websocket_client.send_json_auto_id({"type": "otbr/create_network"})
msg = await websocket_client.receive_json()
@ -191,6 +196,8 @@ async def test_create_network_fails_4(
), patch(
"python_otbr_api.OTBR.get_active_dataset_tlvs",
side_effect=python_otbr_api.OTBRError,
), patch(
"python_otbr_api.OTBR.delete_active_dataset"
):
await websocket_client.send_json_auto_id({"type": "otbr/create_network"})
msg = await websocket_client.receive_json()
@ -208,7 +215,9 @@ async def test_create_network_fails_5(
"""Test create network."""
with patch("python_otbr_api.OTBR.set_enabled"), patch(
"python_otbr_api.OTBR.create_active_dataset"
), patch("python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=None):
), patch("python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=None), patch(
"python_otbr_api.OTBR.delete_active_dataset"
):
await websocket_client.send_json_auto_id({"type": "otbr/create_network"})
msg = await websocket_client.receive_json()
@ -216,6 +225,26 @@ async def test_create_network_fails_5(
assert msg["error"]["code"] == "get_active_dataset_tlvs_empty"
async def test_create_network_fails_6(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
otbr_config_entry,
websocket_client,
) -> None:
"""Test create network."""
with patch("python_otbr_api.OTBR.set_enabled"), patch(
"python_otbr_api.OTBR.create_active_dataset"
), patch("python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=None), patch(
"python_otbr_api.OTBR.delete_active_dataset",
side_effect=python_otbr_api.OTBRError,
):
await websocket_client.send_json_auto_id({"type": "otbr/create_network"})
msg = await websocket_client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == "delete_active_dataset_failed"
async def test_set_network(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,