Skip to content

Commit

Permalink
Try negative WAQI station number before aborting (home-assistant#102550)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Oct 23, 2023
1 parent e27baed commit 8a7de27
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions homeassistant/components/waqi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
CONF_MAP = "map"


async def get_by_station_number(
client: WAQIClient, station_number: int
) -> tuple[WAQIAirQuality | None, dict[str, str]]:
"""Get measuring station by station number."""
errors: dict[str, str] = {}
measuring_station: WAQIAirQuality | None = None
try:
measuring_station = await client.get_by_station_number(station_number)
except WAQIConnectionError:
errors["base"] = "cannot_connect"
except Exception as exc: # pylint: disable=broad-except
_LOGGER.exception(exc)
errors["base"] = "unknown"
return measuring_station, errors


class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for World Air Quality Index (WAQI)."""

Expand Down Expand Up @@ -141,16 +157,16 @@ async def async_step_station_number(
session=async_get_clientsession(self.hass)
) as waqi_client:
waqi_client.authenticate(self.data[CONF_API_KEY])
try:
measuring_station = await waqi_client.get_by_station_number(
user_input[CONF_STATION_NUMBER]
station_number = user_input[CONF_STATION_NUMBER]
measuring_station, errors = await get_by_station_number(
waqi_client, abs(station_number)
)
if not measuring_station:
measuring_station, _ = await get_by_station_number(
waqi_client,
abs(station_number) - station_number - station_number,
)
except WAQIConnectionError:
errors["base"] = "cannot_connect"
except Exception as exc: # pylint: disable=broad-except
_LOGGER.exception(exc)
errors["base"] = "unknown"
else:
if measuring_station:
return await self._async_create_entry(measuring_station)
return self.async_show_form(
step_id=CONF_STATION_NUMBER,
Expand Down

0 comments on commit 8a7de27

Please sign in to comment.