Skip to content

Commit 2cd0637

Browse files
authored
Fix NINA flow tests to end a final state (home-assistant#156664)
1 parent 293f8f7 commit 2cd0637

File tree

3 files changed

+126
-109
lines changed

3 files changed

+126
-109
lines changed

homeassistant/components/nina/config_flow.py

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from .const import (
2323
_LOGGER,
24-
ALL_MATCH_REGEX,
2524
CONF_AREA_FILTER,
2625
CONF_FILTERS,
2726
CONF_HEADLINE_FILTER,
@@ -86,6 +85,34 @@ def prepare_user_input(
8685
return user_input
8786

8887

88+
def create_schema(regions: dict[str, dict[str, Any]]) -> vol.Schema:
89+
"""Create the schema for the flows."""
90+
schema_dict: VolDictType = {
91+
**{
92+
vol.Optional(region): cv.multi_select(regions[region])
93+
for region in CONST_REGIONS
94+
},
95+
vol.Required(
96+
CONF_MESSAGE_SLOTS,
97+
default=5,
98+
): vol.All(int, vol.Range(min=1, max=20)),
99+
vol.Required(CONF_FILTERS): section(
100+
vol.Schema(
101+
{
102+
vol.Optional(
103+
CONF_HEADLINE_FILTER,
104+
): cv.string,
105+
vol.Optional(
106+
CONF_AREA_FILTER,
107+
): cv.string,
108+
}
109+
)
110+
),
111+
}
112+
113+
return vol.Schema(schema_dict)
114+
115+
89116
class NinaConfigFlow(ConfigFlow, domain=DOMAIN):
90117
"""Handle a config flow for NINA."""
91118

@@ -116,10 +143,10 @@ async def async_step_user(
116143
await nina.getAllRegionalCodes()
117144
)
118145
except ApiError:
119-
errors["base"] = "cannot_connect"
120-
except Exception as err: # noqa: BLE001
121-
_LOGGER.exception("Unexpected exception: %s", err)
122-
errors["base"] = "unknown"
146+
return self.async_abort(reason="no_fetch")
147+
except Exception: # noqa: BLE001
148+
_LOGGER.exception("Unexpected exception")
149+
return self.async_abort(reason="unknown")
123150

124151
self.regions = split_regions(self._all_region_codes_sorted, self.regions)
125152

@@ -141,33 +168,9 @@ async def async_step_user(
141168

142169
errors["base"] = "no_selection"
143170

144-
regions_schema: VolDictType = {
145-
vol.Optional(region): cv.multi_select(self.regions[region])
146-
for region in CONST_REGIONS
147-
}
148-
149171
return self.async_show_form(
150172
step_id="user",
151-
data_schema=vol.Schema(
152-
{
153-
**regions_schema,
154-
vol.Required(CONF_MESSAGE_SLOTS, default=5): vol.All(
155-
int, vol.Range(min=1, max=20)
156-
),
157-
vol.Required(CONF_FILTERS): section(
158-
vol.Schema(
159-
{
160-
vol.Optional(
161-
CONF_HEADLINE_FILTER, default=NO_MATCH_REGEX
162-
): cv.string,
163-
vol.Optional(
164-
CONF_AREA_FILTER, default=ALL_MATCH_REGEX
165-
): cv.string,
166-
}
167-
)
168-
),
169-
}
170-
),
173+
data_schema=create_schema(self.regions),
171174
errors=errors,
172175
)
173176

@@ -209,10 +212,10 @@ async def async_step_init(
209212
await nina.getAllRegionalCodes()
210213
)
211214
except ApiError:
212-
errors["base"] = "cannot_connect"
213-
except Exception as err: # noqa: BLE001
214-
_LOGGER.exception("Unexpected exception: %s", err)
215-
errors["base"] = "unknown"
215+
return self.async_abort(reason="no_fetch")
216+
except Exception: # noqa: BLE001
217+
_LOGGER.exception("Unexpected exception")
218+
return self.async_abort(reason="unknown")
216219

217220
self.regions = split_regions(self._all_region_codes_sorted, self.regions)
218221

@@ -263,35 +266,12 @@ async def async_step_init(
263266

264267
errors["base"] = "no_selection"
265268

266-
schema: VolDictType = {
267-
**{
268-
vol.Optional(region, default=self.data[region]): cv.multi_select(
269-
self.regions[region]
270-
)
271-
for region in CONST_REGIONS
272-
},
273-
vol.Required(
274-
CONF_MESSAGE_SLOTS,
275-
default=self.data[CONF_MESSAGE_SLOTS],
276-
): vol.All(int, vol.Range(min=1, max=20)),
277-
vol.Required(CONF_FILTERS): section(
278-
vol.Schema(
279-
{
280-
vol.Optional(
281-
CONF_HEADLINE_FILTER,
282-
default=self.data[CONF_FILTERS][CONF_HEADLINE_FILTER],
283-
): cv.string,
284-
vol.Optional(
285-
CONF_AREA_FILTER,
286-
default=self.data[CONF_FILTERS][CONF_AREA_FILTER],
287-
): cv.string,
288-
}
289-
)
290-
),
291-
}
269+
schema_with_suggested = self.add_suggested_values_to_schema(
270+
create_schema(self.regions), self.data
271+
)
292272

293273
return self.async_show_form(
294274
step_id="init",
295-
data_schema=vol.Schema(schema),
275+
data_schema=schema_with_suggested,
296276
errors=errors,
297277
)

homeassistant/components/nina/strings.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"config": {
3-
"error": {
4-
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
5-
"no_selection": "Please select at least one city/county",
3+
"abort": {
4+
"no_fetch": "Cannot connect to NINA service to fetch city or counties. Please try again later.",
65
"unknown": "[%key:common::config_flow::error::unknown%]"
76
},
7+
"error": {
8+
"no_selection": "Please select at least one city/county"
9+
},
810
"step": {
911
"user": {
1012
"data": {
@@ -44,11 +46,13 @@
4446
}
4547
},
4648
"options": {
47-
"error": {
48-
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
49-
"no_selection": "[%key:component::nina::config::error::no_selection%]",
49+
"abort": {
50+
"no_fetch": "[%key:component::nina::config::abort::no_fetch%]",
5051
"unknown": "[%key:common::config_flow::error::unknown%]"
5152
},
53+
"error": {
54+
"no_selection": "[%key:component::nina::config::error::no_selection%]"
55+
},
5256
"step": {
5357
"init": {
5458
"data": {

0 commit comments

Comments
 (0)