mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07: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."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@ -90,13 +89,10 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def _async_base_step(
|
||||
self,
|
||||
step_id: str,
|
||||
method: Callable[[WAQIClient, dict[str, Any]], Awaitable[WAQIAirQuality]],
|
||||
data_schema: vol.Schema,
|
||||
user_input: dict[str, Any] | None = None,
|
||||
async def async_step_map(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Add measuring station via map."""
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
async with WAQIClient(
|
||||
@ -104,7 +100,10 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
) as waqi_client:
|
||||
waqi_client.authenticate(self.data[CONF_API_KEY])
|
||||
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:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
@ -113,19 +112,8 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
else:
|
||||
return await self._async_create_entry(measuring_station)
|
||||
return self.async_show_form(
|
||||
step_id=step_id, data_schema=data_schema, errors=errors
|
||||
)
|
||||
|
||||
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(
|
||||
step_id=CONF_MAP,
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(
|
||||
@ -140,26 +128,40 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
}
|
||||
},
|
||||
),
|
||||
user_input,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_station_number(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Add measuring station via station number."""
|
||||
return await self._async_base_step(
|
||||
CONF_STATION_NUMBER,
|
||||
lambda waqi_client, data: waqi_client.get_by_station_number(
|
||||
data[CONF_STATION_NUMBER]
|
||||
),
|
||||
vol.Schema(
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
async with WAQIClient(
|
||||
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]
|
||||
)
|
||||
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(
|
||||
CONF_STATION_NUMBER,
|
||||
): int,
|
||||
}
|
||||
),
|
||||
user_input,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def _async_create_entry(
|
||||
|
Loading…
x
Reference in New Issue
Block a user