mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Remove abstraction in WAQI config flow (#102543)
This commit is contained in:
parent
c9c152d46d
commit
1fd7378caf
@ -1,7 +1,6 @@
|
|||||||
"""Config flow for World Air Quality Index (WAQI) integration."""
|
"""Config flow for World Air Quality Index (WAQI) integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Awaitable, Callable
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -90,13 +89,10 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_base_step(
|
async def async_step_map(
|
||||||
self,
|
self, user_input: dict[str, Any] | None = None
|
||||||
step_id: str,
|
|
||||||
method: Callable[[WAQIClient, dict[str, Any]], Awaitable[WAQIAirQuality]],
|
|
||||||
data_schema: vol.Schema,
|
|
||||||
user_input: dict[str, Any] | None = None,
|
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
|
"""Add measuring station via map."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
async with WAQIClient(
|
async with WAQIClient(
|
||||||
@ -104,7 +100,10 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
) as waqi_client:
|
) as waqi_client:
|
||||||
waqi_client.authenticate(self.data[CONF_API_KEY])
|
waqi_client.authenticate(self.data[CONF_API_KEY])
|
||||||
try:
|
try:
|
||||||
measuring_station = await method(waqi_client, user_input)
|
measuring_station = await waqi_client.get_by_coordinates(
|
||||||
|
user_input[CONF_LOCATION][CONF_LATITUDE],
|
||||||
|
user_input[CONF_LOCATION][CONF_LONGITUDE],
|
||||||
|
)
|
||||||
except WAQIConnectionError:
|
except WAQIConnectionError:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except Exception as exc: # pylint: disable=broad-except
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
@ -113,19 +112,8 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
else:
|
else:
|
||||||
return await self._async_create_entry(measuring_station)
|
return await self._async_create_entry(measuring_station)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id=step_id, data_schema=data_schema, errors=errors
|
step_id=CONF_MAP,
|
||||||
)
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
|
||||||
async def async_step_map(
|
|
||||||
self, user_input: dict[str, Any] | None = None
|
|
||||||
) -> FlowResult:
|
|
||||||
"""Add measuring station via map."""
|
|
||||||
return await self._async_base_step(
|
|
||||||
CONF_MAP,
|
|
||||||
lambda waqi_client, data: waqi_client.get_by_coordinates(
|
|
||||||
data[CONF_LOCATION][CONF_LATITUDE], data[CONF_LOCATION][CONF_LONGITUDE]
|
|
||||||
),
|
|
||||||
self.add_suggested_values_to_schema(
|
|
||||||
vol.Schema(
|
vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(
|
vol.Required(
|
||||||
@ -140,26 +128,40 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
user_input,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_station_number(
|
async def async_step_station_number(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Add measuring station via station number."""
|
"""Add measuring station via station number."""
|
||||||
return await self._async_base_step(
|
errors: dict[str, str] = {}
|
||||||
CONF_STATION_NUMBER,
|
if user_input is not None:
|
||||||
lambda waqi_client, data: waqi_client.get_by_station_number(
|
async with WAQIClient(
|
||||||
data[CONF_STATION_NUMBER]
|
session=async_get_clientsession(self.hass)
|
||||||
),
|
) as waqi_client:
|
||||||
vol.Schema(
|
waqi_client.authenticate(self.data[CONF_API_KEY])
|
||||||
|
try:
|
||||||
|
measuring_station = await waqi_client.get_by_station_number(
|
||||||
|
user_input[CONF_STATION_NUMBER]
|
||||||
|
)
|
||||||
|
except WAQIConnectionError:
|
||||||
|
errors["base"] = "cannot_connect"
|
||||||
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
|
_LOGGER.exception(exc)
|
||||||
|
errors["base"] = "unknown"
|
||||||
|
else:
|
||||||
|
return await self._async_create_entry(measuring_station)
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id=CONF_STATION_NUMBER,
|
||||||
|
data_schema=vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_STATION_NUMBER,
|
CONF_STATION_NUMBER,
|
||||||
): int,
|
): int,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
user_input,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_create_entry(
|
async def _async_create_entry(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user