Skip to content

Commit 816b214

Browse files
authored
fix: more work on clearning solver URL (#172)
* fix: more work on clearning solver URL * formatting * fix some typing
1 parent 48c0a46 commit 816b214

2 files changed

Lines changed: 100 additions & 95 deletions

File tree

custom_components/gasbuddy/config_flow.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def validate_station(station: int, solver: str | None = None) -> bool:
5757
return True
5858

5959

60-
async def _get_station_list(hass, user_input) -> list | None:
60+
async def _get_station_list(hass, user_input) -> dict[str, Any] | None:
6161
"""Return list of utilities by lat/lon."""
6262
lat = None
6363
lon = None
@@ -93,8 +93,9 @@ async def _get_station_list(hass, user_input) -> list | None:
9393
return stations_list
9494

9595

96-
# pylint: disable-next=unused-argument
97-
def _get_schema_manual(hass: Any, user_input: list, default_dict: list) -> Any:
96+
def _get_schema_manual( # pylint: disable-next=unused-argument
97+
hass: Any, user_input: dict[str, Any], default_dict: dict[str, Any]
98+
) -> Any:
9899
"""Get a schema using the default_dict as a backup."""
99100
if user_input is None:
100101
user_input = {}
@@ -120,8 +121,8 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
120121

121122
def _get_schema_home(
122123
hass: Any, # pylint: disable=unused-argument
123-
user_input: list,
124-
default_dict: list,
124+
user_input: dict[str, Any],
125+
default_dict: dict[str, Any],
125126
) -> Any:
126127
"""Get a schema using the default_dict as a backup."""
127128
if user_input is None:
@@ -142,8 +143,8 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
142143

143144
def _get_schema_home2(
144145
hass: Any, # pylint: disable=unused-argument
145-
user_input: list,
146-
default_dict: list,
146+
user_input: dict[str, Any],
147+
default_dict: dict[str, Any],
147148
station_list: list,
148149
) -> Any:
149150
"""Get a schema using the default_dict as a backup."""
@@ -166,8 +167,9 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
166167
)
167168

168169

169-
# pylint: disable-next=unused-argument
170-
def _get_schema_postal(hass: Any, user_input: list, default_dict: list) -> Any:
170+
def _get_schema_postal( # pylint: disable-next=unused-argument
171+
hass: Any, user_input: dict[str, Any], default_dict: dict[str, Any]
172+
) -> Any:
171173
"""Get a schema using the default_dict as a backup."""
172174
if user_input is None:
173175
user_input = {}
@@ -190,8 +192,8 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
190192

191193
def _get_schema_station_list(
192194
hass: Any, # pylint: disable=unused-argument
193-
user_input: list,
194-
default_dict: list,
195+
user_input: dict[str, Any],
196+
default_dict: dict[str, Any],
195197
station_list: list,
196198
) -> Any:
197199
"""Get a schema using the default_dict as a backup."""
@@ -214,8 +216,9 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
214216
)
215217

216218

217-
# pylint: disable-next=unused-argument
218-
def _get_schema_options(hass: Any, user_input: list, default_dict: list) -> Any:
219+
def _get_schema_options( # pylint: disable-next=unused-argument
220+
hass: Any, user_input: dict[str, Any], default_dict: dict[str, Any]
221+
) -> Any:
219222
"""Get a schema using the default_dict as a backup."""
220223
if user_input is None:
221224
user_input = {}
@@ -260,10 +263,11 @@ async def async_step_manual(self, user_input=None):
260263
self._errors = {}
261264

262265
if user_input is not None:
263-
user_input[CONF_INTERVAL] = 3600
264-
user_input[CONF_UOM] = True
265-
user_input[CONF_GPS] = True
266-
if user_input[CONF_SOLVER] != "":
266+
user_input.setdefault(CONF_SOLVER)
267+
user_input.setdefault(CONF_INTERVAL, 3600)
268+
user_input.setdefault(CONF_UOM, True)
269+
user_input.setdefault(CONF_GPS, True)
270+
if user_input[CONF_SOLVER] is not None:
267271
url_valid = await validate_url(user_input[CONF_SOLVER])
268272
_LOGGER.debug("URL valid: %s", url_valid)
269273
if not url_valid:
@@ -309,11 +313,12 @@ async def async_step_home(self, user_input=None):
309313
self._errors = {}
310314

311315
if user_input is not None:
312-
user_input[CONF_INTERVAL] = 3600
313-
user_input[CONF_UOM] = True
314-
user_input[CONF_GPS] = True
316+
user_input.setdefault(CONF_SOLVER)
317+
user_input.setdefault(CONF_INTERVAL, 3600)
318+
user_input.setdefault(CONF_UOM, True)
319+
user_input.setdefault(CONF_GPS, True)
315320
self._data.update(user_input)
316-
if user_input[CONF_SOLVER] != "":
321+
if user_input[CONF_SOLVER] is not None:
317322
url_valid = await validate_url(user_input[CONF_SOLVER])
318323
_LOGGER.debug("URL valid: %s", url_valid)
319324
if not url_valid:
@@ -338,9 +343,9 @@ async def async_step_home2(self, user_input=None):
338343
self._errors = {}
339344

340345
if user_input is not None:
341-
user_input[CONF_INTERVAL] = 3600
342-
user_input[CONF_UOM] = True
343-
user_input[CONF_GPS] = True
346+
user_input.setdefault(CONF_INTERVAL, 3600)
347+
user_input.setdefault(CONF_UOM, True)
348+
user_input.setdefault(CONF_GPS, True)
344349
self._data.update(user_input)
345350
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)
346351
return await self._show_config_home2(user_input)
@@ -394,9 +399,9 @@ async def async_step_station_list(self, user_input=None):
394399
self._errors = {}
395400

396401
if user_input is not None:
397-
user_input[CONF_INTERVAL] = 3600
398-
user_input[CONF_UOM] = True
399-
user_input[CONF_GPS] = True
402+
user_input.setdefault(CONF_INTERVAL, 3600)
403+
user_input.setdefault(CONF_UOM, True)
404+
user_input.setdefault(CONF_GPS, True)
400405
self._data.pop(CONF_POSTAL)
401406
self._data.update(user_input)
402407
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)

tests/test_config_flow.py

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,74 +1079,6 @@ async def test_reconfigure_server_error(
10791079
assert entry.data.copy() == data
10801080

10811081

1082-
@pytest.mark.parametrize(
1083-
"input,data",
1084-
[
1085-
(
1086-
{
1087-
CONF_INTERVAL: 1600,
1088-
CONF_UOM: True,
1089-
CONF_GPS: True,
1090-
},
1091-
{
1092-
CONF_GPS: True,
1093-
CONF_INTERVAL: 1600,
1094-
CONF_UOM: True,
1095-
},
1096-
),
1097-
],
1098-
)
1099-
async def test_form_options(
1100-
input,
1101-
data,
1102-
hass,
1103-
mock_gasbuddy,
1104-
mock_aioclient,
1105-
):
1106-
"""Test we get the form."""
1107-
mock_aioclient.get(
1108-
GB_URL,
1109-
status=200,
1110-
body=load_fixture("index.html"),
1111-
repeat=True,
1112-
)
1113-
mock_aioclient.post(
1114-
BASE_URL,
1115-
status=200,
1116-
body=load_fixture("location_results.json"),
1117-
repeat=True,
1118-
)
1119-
mock_aioclient.post(
1120-
SOLVER_URL,
1121-
status=200,
1122-
body=load_fixture("solver_response.json"),
1123-
repeat=True,
1124-
)
1125-
entry = MockConfigEntry(
1126-
domain=DOMAIN, title="gas_station", data=CONFIG_DATA, version=2
1127-
)
1128-
1129-
entry.add_to_hass(hass)
1130-
assert await hass.config_entries.async_setup(entry.entry_id)
1131-
await hass.async_block_till_done()
1132-
1133-
result = await hass.config_entries.options.async_init(entry.entry_id)
1134-
1135-
assert result["type"] is FlowResultType.FORM
1136-
assert result["step_id"] == "init"
1137-
1138-
result = await hass.config_entries.options.async_configure(
1139-
result["flow_id"], user_input=input
1140-
)
1141-
await hass.async_block_till_done()
1142-
1143-
assert result["type"] is FlowResultType.CREATE_ENTRY
1144-
assert result["data"] == data
1145-
await hass.async_block_till_done()
1146-
1147-
assert entry.options.get(CONF_INTERVAL) == 1600
1148-
1149-
11501082
@pytest.mark.parametrize(
11511083
"input",
11521084
[
@@ -1288,3 +1220,71 @@ async def test_reconfigure_no_solver(
12881220

12891221
entry = hass.config_entries.async_entries(DOMAIN)[0]
12901222
assert entry.data.copy() == data
1223+
1224+
1225+
@pytest.mark.parametrize(
1226+
"input,data",
1227+
[
1228+
(
1229+
{
1230+
CONF_INTERVAL: 1600,
1231+
CONF_UOM: True,
1232+
CONF_GPS: True,
1233+
},
1234+
{
1235+
CONF_GPS: True,
1236+
CONF_INTERVAL: 1600,
1237+
CONF_UOM: True,
1238+
},
1239+
),
1240+
],
1241+
)
1242+
async def test_form_options(
1243+
input,
1244+
data,
1245+
hass,
1246+
mock_gasbuddy,
1247+
mock_aioclient,
1248+
):
1249+
"""Test we get the form."""
1250+
mock_aioclient.get(
1251+
GB_URL,
1252+
status=200,
1253+
body=load_fixture("index.html"),
1254+
repeat=True,
1255+
)
1256+
mock_aioclient.post(
1257+
BASE_URL,
1258+
status=200,
1259+
body=load_fixture("location_results.json"),
1260+
repeat=True,
1261+
)
1262+
mock_aioclient.post(
1263+
SOLVER_URL,
1264+
status=200,
1265+
body=load_fixture("solver_response.json"),
1266+
repeat=True,
1267+
)
1268+
entry = MockConfigEntry(
1269+
domain=DOMAIN, title="gas_station", data=CONFIG_DATA, version=2
1270+
)
1271+
1272+
entry.add_to_hass(hass)
1273+
assert await hass.config_entries.async_setup(entry.entry_id)
1274+
await hass.async_block_till_done()
1275+
1276+
result = await hass.config_entries.options.async_init(entry.entry_id)
1277+
1278+
assert result["type"] is FlowResultType.FORM
1279+
assert result["step_id"] == "init"
1280+
1281+
result = await hass.config_entries.options.async_configure(
1282+
result["flow_id"], user_input=input
1283+
)
1284+
await hass.async_block_till_done()
1285+
1286+
assert result["type"] is FlowResultType.CREATE_ENTRY
1287+
assert result["data"] == data
1288+
await hass.async_block_till_done()
1289+
1290+
assert entry.options.get(CONF_INTERVAL) == 1600

0 commit comments

Comments
 (0)