Skip to content

Commit

Permalink
fix: insert 'fake' station when no stations found and provide error m…
Browse files Browse the repository at this point in the history
…essage (#120)

* fix: insert 'fake' station when no stations found and provide error message

* update tests

* linting

* update tests

* formatting
  • Loading branch information
firstof9 authored Dec 12, 2024
1 parent 847c0db commit 6eb2fb2
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
strategy:
matrix:
python-version:
- "3.12"
- "3.13"

steps:
- name: 📥 Checkout the repository
Expand Down
11 changes: 10 additions & 1 deletion custom_components/gasbuddy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ async def _get_station_list(hass, user_input) -> list | None:
full_name = f'{station["name"]} @ {station["address"]["line1"]}'
stations_list[station["id"]] = full_name

if len(stations_list) == 0:
stations_list["-"] = "No stations in search area."

_LOGGER.debug("stations_list: %s", stations_list)
return stations_list

Expand Down Expand Up @@ -140,7 +143,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
{
vol.Required(
CONF_STATION_ID, default=_get_default(CONF_STATION_ID)
): vol.In(station_list),
): vol.All(vol.In(station_list), vol.NotIn(["-"])),
vol.Required(CONF_NAME, default=_get_default(CONF_NAME, DEFAULT_NAME)): str,
}
)
Expand Down Expand Up @@ -239,6 +242,9 @@ async def _show_config_home(self, user_input):

station_list = await _get_station_list(self.hass, user_input)

if "-" in station_list.keys():
self._errors[CONF_STATION_ID] = "no_results"

return self.async_show_form(
step_id="home",
data_schema=_get_schema_home(self.hass, user_input, defaults, station_list),
Expand Down Expand Up @@ -283,6 +289,9 @@ async def _show_config_station_list(self, user_input):

station_list = await _get_station_list(self.hass, self._data)

if "-" in station_list.keys():
self._errors[CONF_STATION_ID] = "no_results"

return self.async_show_form(
step_id="station_list",
data_schema=_get_schema_station_list(
Expand Down
3 changes: 2 additions & 1 deletion custom_components/gasbuddy/strings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"error": {
"station_id": "Invalid station ID"
"station_id": "Invalid station ID",
"no_results": "No stations found in this area."
},
"step": {
"user": {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/gasbuddy/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"error": {
"station_id": "Invalid station ID"
"station_id": "Invalid station ID",
"no_results": "No stations found in this area."
},
"step": {
"user": {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/gasbuddy/translations/pt.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"error": {
"station_id": "ID de estação inválido"
"station_id": "ID de estação inválido",
"no_results": "No stations found in this area."
},
"step": {
"user": {
Expand Down
12 changes: 6 additions & 6 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-r requirements.txt
pytest-homeassistant-custom-component
black==24.10.0
flake8==7.1.1
mypy==1.13.0
pydocstyle==6.3.0
black
flake8
mypy
pydocstyle
isort
pylint==3.3.2
tox==4.23.2
pylint
tox
pytest
aioresponses
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.12
python_version = 3.13
show_error_codes = true
ignore_errors = true
follow_imports = silent
Expand Down
151 changes: 151 additions & 0 deletions tests/fixtures/location_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"data": {
"locationBySearchTerm": {
"stations": {
"count": 92,
"results": [
{
"address": {
"line1": "1520 N Verrado Way"
},
"id": "187725",
"name": "Shell"
},
{
"address": {
"line1": "1101 N Verrado Way"
},
"id": "208656",
"name": "Costco"
},
{
"address": {
"line1": "1419 N 195th Ave"
},
"id": "87490",
"name": "Chevron"
},
{
"address": {
"line1": "721 N 195th Ave"
},
"id": "110402",
"name": "Circle K"
},
{
"address": {
"line1": "19600 W Indian School Rd"
},
"id": "203982",
"name": "Fry's"
},
{
"address": {
"line1": "537 S Watson Rd"
},
"id": "126744",
"name": "Circle K"
},
{
"address": {
"line1": "900 S Watson Rd"
},
"id": "201250",
"name": "QuikTrip"
},
{
"address": {
"line1": "1300 S Watson Rd"
},
"id": "38363",
"name": "Fry's"
},
{
"address": {
"line1": "1610 N Miller Rd"
},
"id": "27487",
"name": "Love's Travel Stop"
},
{
"address": {
"line1": "1850 S Miller Rd"
},
"id": "160044",
"name": "QuikTrip"
},
{
"address": {
"line1": "2075 S Miller Rd"
},
"id": "135437",
"name": "Chevron"
},
{
"address": {
"line1": "16380 W Yuma Rd"
},
"id": "130812",
"name": "Fry's"
},
{
"address": {
"line1": "15535 W McDowell Rd"
},
"id": "200905",
"name": "Circle K"
},
{
"address": {
"line1": "440 N Estrella Pkwy"
},
"id": "85320",
"name": "Safeway"
},
{
"address": {
"line1": "575 N Estrella Pkwy"
},
"id": "155795",
"name": "QuikTrip"
},
{
"address": {
"line1": "307 E US-85"
},
"id": "118417",
"name": "Circle K"
},
{
"address": {
"line1": "825 E Monroe Ave"
},
"id": "154238",
"name": "Chevron"
},
{
"address": {
"line1": "501 E Monroe Ave"
},
"id": "150938",
"name": "Shell"
},
{
"address": {
"line1": "1540 N Bullard Ave"
},
"id": "209199",
"name": "QuikTrip"
},
{
"address": {
"line1": "14175 W Indian School Rd"
},
"id": "27442",
"name": "Safeway"
}
]
}
}
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/no_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"locationBySearchTerm": {
"stations": {
"results": []
}
}
}
}
Loading

0 comments on commit 6eb2fb2

Please sign in to comment.