Skip to content

Commit 12ace95

Browse files
Improve error handling in Niko Home Control config flow (home-assistant#154565)
Co-authored-by: Joostlek <[email protected]>
1 parent babe197 commit 12ace95

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

homeassistant/components/niko_home_control/config_flow.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ async def test_connection(host: str) -> str | None:
2828
controller = NHCController(host, 8000)
2929
try:
3030
await controller.connect()
31-
except Exception:
32-
_LOGGER.exception("Unexpected exception")
31+
except TimeoutError:
32+
return "timeout_connect"
33+
except OSError:
3334
return "cannot_connect"
35+
except Exception:
36+
_LOGGER.exception("Unexpected exception during connection")
37+
return "unknown"
3438
return None
3539

3640

homeassistant/components/niko_home_control/strings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
66
},
77
"error": {
8-
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
8+
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
9+
"timeout_connect": "[%key:common::config_flow::error::timeout_connect%]",
10+
"unknown": "[%key:common::config_flow::error::unknown%]"
911
},
1012
"step": {
1113
"reconfigure": {

tests/components/niko_home_control/test_config_flow.py

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Test niko_home_control config flow."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock
4+
5+
import pytest
46

57
from homeassistant.components.niko_home_control.const import DOMAIN
68
from homeassistant.config_entries import SOURCE_USER
@@ -36,34 +38,45 @@ async def test_full_flow(
3638
assert len(mock_setup_entry.mock_calls) == 1
3739

3840

39-
async def test_cannot_connect(hass: HomeAssistant) -> None:
40-
"""Test the cannot connect error."""
41+
@pytest.mark.parametrize(
42+
("exception", "error"),
43+
[
44+
(TimeoutError, "timeout_connect"),
45+
(OSError, "cannot_connect"),
46+
(Exception, "unknown"),
47+
],
48+
)
49+
async def test_flow_errors(
50+
hass: HomeAssistant,
51+
mock_niko_home_control_connection: AsyncMock,
52+
mock_setup_entry: AsyncMock,
53+
exception: Exception,
54+
error: str,
55+
) -> None:
56+
"""Test the timeout error."""
4157

4258
result = await hass.config_entries.flow.async_init(
4359
DOMAIN, context={"source": SOURCE_USER}
4460
)
4561
assert result["type"] is FlowResultType.FORM
4662
assert result["errors"] == {}
4763

48-
with patch(
49-
"homeassistant.components.niko_home_control.config_flow.NHCController.connect",
50-
side_effect=Exception,
51-
):
52-
result = await hass.config_entries.flow.async_configure(
53-
result["flow_id"],
54-
{CONF_HOST: "192.168.0.123"},
55-
)
64+
mock_niko_home_control_connection.connect.side_effect = exception
65+
66+
result = await hass.config_entries.flow.async_configure(
67+
result["flow_id"],
68+
{CONF_HOST: "192.168.0.123"},
69+
)
5670

5771
assert result["type"] is FlowResultType.FORM
58-
assert result["errors"] == {"base": "cannot_connect"}
72+
assert result["errors"] == {"base": error}
5973

60-
with patch(
61-
"homeassistant.components.niko_home_control.config_flow.NHCController.connect",
62-
):
63-
result = await hass.config_entries.flow.async_configure(
64-
result["flow_id"],
65-
{CONF_HOST: "192.168.0.123"},
66-
)
74+
mock_niko_home_control_connection.connect.side_effect = None
75+
76+
result = await hass.config_entries.flow.async_configure(
77+
result["flow_id"],
78+
{CONF_HOST: "192.168.0.123"},
79+
)
6780

6881
assert result["type"] is FlowResultType.CREATE_ENTRY
6982

@@ -119,6 +132,7 @@ async def test_reconfigure(
119132
hass: HomeAssistant,
120133
mock_niko_home_control_connection: AsyncMock,
121134
mock_config_entry: MockConfigEntry,
135+
mock_setup_entry: AsyncMock,
122136
) -> None:
123137
"""Test the reconfigure flow."""
124138
mock_config_entry.add_to_hass(hass)
@@ -136,28 +150,39 @@ async def test_reconfigure(
136150
assert result["reason"] == "reconfigure_successful"
137151

138152

139-
async def test_reconfigure_cannot_connect(
153+
@pytest.mark.parametrize(
154+
("exception", "error"),
155+
[
156+
(TimeoutError, "timeout_connect"),
157+
(OSError, "cannot_connect"),
158+
(Exception, "unknown"),
159+
],
160+
)
161+
async def test_reconfigure_errors(
140162
hass: HomeAssistant,
141163
mock_niko_home_control_connection: AsyncMock,
142164
mock_config_entry: MockConfigEntry,
165+
mock_setup_entry: AsyncMock,
166+
exception: Exception,
167+
error: str,
143168
) -> None:
144169
"""Test reconfiguration with connection error."""
145170
mock_config_entry.add_to_hass(hass)
146171

147-
mock_niko_home_control_connection.connect.side_effect = Exception("cannot_connect")
148-
149172
result = await mock_config_entry.start_reconfigure_flow(hass)
150173

151174
assert result["type"] is FlowResultType.FORM
152175
assert result["errors"] == {}
153176

177+
mock_niko_home_control_connection.connect.side_effect = exception
178+
154179
result = await hass.config_entries.flow.async_configure(
155180
result["flow_id"],
156181
{CONF_HOST: "192.168.0.122"},
157182
)
158183

159184
assert result["type"] is FlowResultType.FORM
160-
assert result["errors"] == {"base": "cannot_connect"}
185+
assert result["errors"] == {"base": error}
161186

162187
mock_niko_home_control_connection.connect.side_effect = None
163188

0 commit comments

Comments
 (0)