Skip to content

Commit

Permalink
Fix OTBR reset (#94157)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Jun 7, 2023
1 parent 6af1beb commit 1fa2fb4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/otbr/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
5 changes: 5 additions & 0 deletions homeassistant/components/otbr/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ async def create_active_dataset(
"""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."""
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/otbr/websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/thread/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."]
}
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions tests/components/otbr/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -208,14 +215,36 @@ 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()

assert not msg["success"]
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,
Expand Down

0 comments on commit 1fa2fb4

Please sign in to comment.