mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
google_travel_time: Merge user_input validation (#119221)
This commit is contained in:
parent
f6c6b3cf6c
commit
42e2c2b3e9
@ -180,6 +180,28 @@ class GoogleOptionsFlow(OptionsFlow):
|
||||
)
|
||||
|
||||
|
||||
async def validate_input(
|
||||
hass: HomeAssistant, user_input: dict[str, Any]
|
||||
) -> dict[str, str] | None:
|
||||
"""Validate the user input allows us to connect."""
|
||||
try:
|
||||
await hass.async_add_executor_job(
|
||||
validate_config_entry,
|
||||
hass,
|
||||
user_input[CONF_API_KEY],
|
||||
user_input[CONF_ORIGIN],
|
||||
user_input[CONF_DESTINATION],
|
||||
)
|
||||
except InvalidApiKeyException:
|
||||
return {"base": "invalid_auth"}
|
||||
except TimeoutError:
|
||||
return {"base": "timeout_connect"}
|
||||
except UnknownException:
|
||||
return {"base": "cannot_connect"}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class GoogleTravelTimeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Google Maps Travel Time."""
|
||||
|
||||
@ -195,24 +217,11 @@ class GoogleTravelTimeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
errors = {}
|
||||
errors: dict[str, str] | None = None
|
||||
user_input = user_input or {}
|
||||
if user_input:
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
validate_config_entry,
|
||||
self.hass,
|
||||
user_input[CONF_API_KEY],
|
||||
user_input[CONF_ORIGIN],
|
||||
user_input[CONF_DESTINATION],
|
||||
)
|
||||
except InvalidApiKeyException:
|
||||
errors["base"] = "invalid_auth"
|
||||
except TimeoutError:
|
||||
errors["base"] = "timeout_connect"
|
||||
except UnknownException:
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
errors = await validate_input(self.hass, user_input)
|
||||
if not errors:
|
||||
return self.async_create_entry(
|
||||
title=user_input.get(CONF_NAME, DEFAULT_NAME),
|
||||
data=user_input,
|
||||
@ -233,24 +242,11 @@ class GoogleTravelTimeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
if TYPE_CHECKING:
|
||||
assert entry
|
||||
|
||||
errors = {}
|
||||
errors: dict[str, str] | None = None
|
||||
user_input = user_input or {}
|
||||
if user_input:
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
validate_config_entry,
|
||||
self.hass,
|
||||
user_input[CONF_API_KEY],
|
||||
user_input[CONF_ORIGIN],
|
||||
user_input[CONF_DESTINATION],
|
||||
)
|
||||
except InvalidApiKeyException:
|
||||
errors["base"] = "invalid_auth"
|
||||
except TimeoutError:
|
||||
errors["base"] = "timeout_connect"
|
||||
except UnknownException:
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
errors = await validate_input(self.hass, user_input)
|
||||
if not errors:
|
||||
return self.async_update_reload_and_abort(
|
||||
entry,
|
||||
data=user_input,
|
||||
|
@ -88,7 +88,7 @@ async def test_minimum_fields(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
|
||||
await assert_common_create_steps(hass, result)
|
||||
|
||||
@ -100,7 +100,7 @@ async def test_invalid_config_entry(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_CONFIG,
|
||||
@ -118,7 +118,7 @@ async def test_invalid_api_key(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_CONFIG,
|
||||
@ -136,7 +136,7 @@ async def test_transport_error(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_CONFIG,
|
||||
@ -154,7 +154,7 @@ async def test_timeout(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_CONFIG,
|
||||
@ -171,7 +171,7 @@ async def test_malformed_api_key(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_CONFIG,
|
||||
@ -234,7 +234,7 @@ async def test_reconfigure_invalid_config_entry(
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
RECONFIGURE_CONFIG,
|
||||
@ -269,7 +269,7 @@ async def test_reconfigure_invalid_api_key(hass: HomeAssistant, mock_config) ->
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
RECONFIGURE_CONFIG,
|
||||
@ -303,7 +303,7 @@ async def test_reconfigure_transport_error(hass: HomeAssistant, mock_config) ->
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
RECONFIGURE_CONFIG,
|
||||
@ -337,7 +337,7 @@ async def test_reconfigure_timeout(hass: HomeAssistant, mock_config) -> None:
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
RECONFIGURE_CONFIG,
|
||||
@ -615,7 +615,7 @@ async def test_dupe(hass: HomeAssistant) -> None:
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
@ -633,7 +633,7 @@ async def test_dupe(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
Loading…
x
Reference in New Issue
Block a user