Skip to content

Commit 3c0cfd5

Browse files
authored
Display error when forming new ZHA network fails (home-assistant#157863)
1 parent 69f66ff commit 3c0cfd5

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

homeassistant/components/zha/config_flow.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@ async def async_step_form_new_network(
680680

681681
try:
682682
await self._form_network_task
683+
except Exception as exc:
684+
_LOGGER.exception("Failed to form new network")
685+
self._progress_error = AbortFlow(
686+
reason="cannot_form_network",
687+
description_placeholders={"error": str(exc)},
688+
)
689+
return self.async_show_progress_done(next_step_id="progress_failed")
683690
finally:
684691
self._form_network_task = None
685692

homeassistant/components/zha/strings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"config": {
33
"abort": {
4+
"cannot_form_network": "Could not form a new Zigbee network.\n\nError: {error}",
45
"cannot_resolve_path": "Could not resolve device path: {path}",
56
"cannot_restore_backup": "The adapter you are restoring to does not properly support backup restoration. Please upgrade the firmware.\n\nError: {error}",
67
"cannot_restore_backup_no_ieee_confirm": "The adapter you are restoring to has outdated firmware and cannot write the adapter IEEE address multiple times. Please upgrade the firmware or confirm permanent overwrite in the previous step.",
@@ -1923,6 +1924,7 @@
19231924
},
19241925
"options": {
19251926
"abort": {
1927+
"cannot_form_network": "[%key:component::zha::config::abort::cannot_form_network%]",
19261928
"cannot_resolve_path": "[%key:component::zha::config::abort::cannot_resolve_path%]",
19271929
"cannot_restore_backup": "[%key:component::zha::config::abort::cannot_restore_backup%]",
19281930
"cannot_restore_backup_no_ieee_confirm": "[%key:component::zha::config::abort::cannot_restore_backup_no_ieee_confirm%]",

tests/components/zha/test_config_flow.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,36 @@ async def form_network_side_effect(*args, **kwargs):
18131813
assert result2["type"] is FlowResultType.CREATE_ENTRY
18141814

18151815

1816+
async def test_formation_strategy_form_initial_network_failure(
1817+
advanced_pick_radio: RadioPicker, mock_app: AsyncMock, hass: HomeAssistant
1818+
) -> None:
1819+
"""Test forming a new network that fails with an exception."""
1820+
# Mock form_network to raise an exception
1821+
mock_app.form_network.side_effect = DelayedAsyncMock(
1822+
side_effect=Exception("Network formation failed")
1823+
)
1824+
1825+
result = await advanced_pick_radio(RadioType.ezsp)
1826+
result_form = await hass.config_entries.flow.async_configure(
1827+
result["flow_id"],
1828+
user_input={"next_step_id": config_flow.FORMATION_FORM_NEW_NETWORK},
1829+
)
1830+
1831+
result2 = await consume_progress_flow(
1832+
hass,
1833+
flow_id=result_form["flow_id"],
1834+
valid_step_ids=("form_new_network",),
1835+
)
1836+
await hass.async_block_till_done()
1837+
1838+
assert result2["type"] is FlowResultType.ABORT
1839+
assert result2["reason"] == "cannot_form_network"
1840+
assert "Network formation failed" in result2["description_placeholders"]["error"]
1841+
1842+
# Verify form_network was called
1843+
mock_app.form_network.assert_called_once()
1844+
1845+
18161846
@patch(f"zigpy_znp.{PROBE_FUNCTION_PATH}", AsyncMock(return_value=True))
18171847
@patch("homeassistant.components.zha.async_setup_entry", AsyncMock(return_value=True))
18181848
async def test_onboarding_auto_formation_new_hardware(

0 commit comments

Comments
 (0)