Skip to content

Commit 52c691d

Browse files
committed
More test-improvements
1 parent cfd94bb commit 52c691d

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

tests/components/plugwise_usb/test_config_flow.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@ def com_port():
2626
return port
2727

2828

29-
async def test_form_flow_usb(
30-
hass: HomeAssistant,
31-
mock_setup_entry: AsyncMock,
32-
) -> None:
33-
"""Test we get the form for Plugwise USB product type."""
34-
result = await hass.config_entries.flow.async_init(
35-
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
36-
)
37-
assert result.get("type") is FlowResultType.FORM
38-
assert result.get("errors") == {}
39-
assert result.get("step_id") == "user"
40-
assert "flow_id" in result
41-
42-
result = await hass.config_entries.flow.async_configure(
43-
result["flow_id"],
44-
)
45-
assert result.get("type") is FlowResultType.FORM
46-
assert result.get("errors") == {}
47-
assert result.get("step_id") == "user"
48-
49-
5029
@patch("serial.tools.list_ports.comports", MagicMock(return_value=[com_port()]))
5130
async def test_user_flow_select(hass):
5231
"""Test user flow when USB-stick is selected from list."""
@@ -57,6 +36,10 @@ async def test_user_flow_select(hass):
5736
DOMAIN,
5837
context={CONF_SOURCE: SOURCE_USER},
5938
)
39+
assert result.get("type") is FlowResultType.FORM
40+
assert result.get("errors") == {}
41+
assert result.get("step_id") == "user"
42+
assert "flow_id" in result
6043

6144
with patch(
6245
"homeassistant.components.plugwise_usb.config_flow.Stick",
@@ -66,23 +49,24 @@ async def test_user_flow_select(hass):
6649
usb_mock.return_value.disconnect = AsyncMock(return_value=None)
6750
usb_mock.return_value.mac_stick = MagicMock(return_value="01:23:45:67:AB")
6851

69-
result_2 = await hass.config_entries.flow.async_configure(
52+
result = await hass.config_entries.flow.async_configure(
7053
result["flow_id"], user_input={CONF_USB_PATH: port_select}
7154
)
7255
await hass.async_block_till_done()
73-
assert result_2.get("type") is FlowResultType.CREATE_ENTRY
74-
assert result_2.get("data") == {CONF_USB_PATH: TEST_USBPORT}
56+
assert result.get("type") is FlowResultType.CREATE_ENTRY
57+
assert result.get("data") == {CONF_USB_PATH: TEST_USBPORT}
7558

76-
# Retry to ensure configuring the same port is not allowed
77-
result = await hass.config_entries.flow.async_init(
78-
DOMAIN,
79-
context={CONF_SOURCE: SOURCE_USER},
80-
)
81-
result = await hass.config_entries.flow.async_configure(
82-
result["flow_id"], user_input={CONF_USB_PATH: port_select}
83-
)
84-
assert result.get("type") is FlowResultType.FORM
85-
assert result.get("errors") == {"base": "already_configured"}
59+
# Retry to ensure configuring the same port is not allowed
60+
result = await hass.config_entries.flow.async_init(
61+
DOMAIN,
62+
context={CONF_SOURCE: SOURCE_USER},
63+
)
64+
result = await hass.config_entries.flow.async_configure(
65+
result["flow_id"], user_input={CONF_USB_PATH: port_select}
66+
)
67+
await hass.async_block_till_done()
68+
assert result.get("type") is FlowResultType.FORM
69+
assert result.get("errors") == {"base": "already_configured"}
8670

8771

8872
async def test_user_flow_manual_selected_show_form(hass):
@@ -105,7 +89,7 @@ async def test_user_flow_manual(hass):
10589
DOMAIN,
10690
context={CONF_SOURCE: SOURCE_USER},
10791
)
108-
result_2 = await hass.config_entries.flow.async_configure(
92+
result = await hass.config_entries.flow.async_configure(
10993
result["flow_id"],
11094
user_input={CONF_USB_PATH: CONF_MANUAL_PATH},
11195
)
@@ -118,12 +102,13 @@ async def test_user_flow_manual(hass):
118102
usb_mock.return_value.disconnect = AsyncMock(return_value=None)
119103
usb_mock.return_value.mac_stick = MagicMock(return_value="01:23:45:67:AB")
120104

121-
result_3 = await hass.config_entries.flow.async_configure(
122-
result_2["flow_id"],
105+
result = await hass.config_entries.flow.async_configure(
106+
result["flow_id"],
123107
user_input={CONF_USB_PATH: TEST_USBPORT2},
124108
)
125-
assert result_3.get("type") is FlowResultType.CREATE_ENTRY
126-
assert result_3.get("data") == {CONF_USB_PATH: TEST_USBPORT2}
109+
await hass.async_block_till_done()
110+
assert result.get("type") is FlowResultType.CREATE_ENTRY
111+
assert result.get("data") == {CONF_USB_PATH: TEST_USBPORT2}
127112

128113

129114
async def test_invalid_connection(hass):
@@ -136,12 +121,13 @@ async def test_invalid_connection(hass):
136121
result["flow_id"],
137122
user_input={CONF_USB_PATH: CONF_MANUAL_PATH},
138123
)
124+
await hass.async_block_till_done()
139125

140126
result = await hass.config_entries.flow.async_configure(
141127
result["flow_id"],
142128
{CONF_USB_PATH: "/dev/null"},
143129
)
144-
130+
await hass.async_block_till_done()
145131
assert result.get("type") is FlowResultType.FORM
146132
assert result.get("errors") == {"base": "cannot_connect"}
147133

@@ -156,6 +142,7 @@ async def test_empty_connection(hass):
156142
result["flow_id"],
157143
user_input={CONF_USB_PATH: CONF_MANUAL_PATH},
158144
)
145+
await hass.async_block_till_done()
159146

160147
try:
161148
result = await hass.config_entries.flow.async_configure(
@@ -182,10 +169,12 @@ async def test_failed_connect(hass):
182169
result["flow_id"],
183170
user_input={CONF_USB_PATH: CONF_MANUAL_PATH},
184171
)
172+
await hass.async_block_till_done()
185173
result = await hass.config_entries.flow.async_configure(
186174
result["flow_id"],
187175
user_input={CONF_USB_PATH: "/dev/null"},
188176
)
177+
await hass.async_block_till_done()
189178
assert result.get("type") is FlowResultType.FORM
190179
assert result.get("errors") == {"base": "cannot_connect"}
191180

@@ -202,9 +191,11 @@ async def test_failed_initialization(hass):
202191
result["flow_id"],
203192
user_input={CONF_USB_PATH: CONF_MANUAL_PATH},
204193
)
194+
await hass.async_block_till_done()
205195
result = await hass.config_entries.flow.async_configure(
206196
result["flow_id"],
207197
user_input={CONF_USB_PATH: "/dev/null"},
208198
)
199+
await hass.async_block_till_done()
209200
assert result.get("type") is FlowResultType.FORM
210201
assert result.get("errors") == {"base": "stick_init"}

0 commit comments

Comments
 (0)