Improve Google Travel Time import and naming logic (#49839)

This commit is contained in:
Raman Gupta 2021-04-29 11:10:03 -04:00 committed by GitHub
parent 5d421c9715
commit 4a95f6c2a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 26 deletions

View file

@ -122,29 +122,27 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is not None:
if await self.hass.async_add_executor_job(
is_valid_config_entry,
self.hass,
_LOGGER,
user_input[CONF_API_KEY],
user_input[CONF_ORIGIN],
user_input[CONF_DESTINATION],
):
await self.async_set_unique_id(
slugify(
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
)
user_input = user_input or {}
if user_input:
await self.async_set_unique_id(
slugify(
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
)
self._abort_if_unique_id_configured()
)
self._abort_if_unique_id_configured()
if (
self.source == config_entries.SOURCE_IMPORT
or await self.hass.async_add_executor_job(
is_valid_config_entry,
self.hass,
_LOGGER,
user_input[CONF_API_KEY],
user_input[CONF_ORIGIN],
user_input[CONF_DESTINATION],
)
):
return self.async_create_entry(
title=user_input.get(
CONF_NAME,
(
f"{DEFAULT_NAME}: {user_input[CONF_ORIGIN]} -> "
f"{user_input[CONF_DESTINATION]}"
),
),
title=user_input.get(CONF_NAME, DEFAULT_NAME),
data=user_input,
)
@ -155,6 +153,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_DESTINATION): cv.string,
vol.Required(CONF_ORIGIN): cv.string,

View file

@ -40,7 +40,6 @@ from .const import (
CONF_TRANSIT_ROUTING_PREFERENCE,
CONF_TRAVEL_MODE,
CONF_UNITS,
DEFAULT_NAME,
DOMAIN,
TRACKABLE_DOMAINS,
TRANSIT_PREFS,
@ -100,11 +99,9 @@ async def async_setup_entry(
async_add_entities: Callable[[list[SensorEntity], bool], None],
) -> None:
"""Set up a Google travel time sensor entry."""
name = None
if not config_entry.options:
new_data = config_entry.data.copy()
options = new_data.pop(CONF_OPTIONS, {})
name = new_data.pop(CONF_NAME, None)
if CONF_UNITS not in options:
options[CONF_UNITS] = hass.config.units.name
@ -129,7 +126,7 @@ async def async_setup_entry(
api_key = config_entry.data[CONF_API_KEY]
origin = config_entry.data[CONF_ORIGIN]
destination = config_entry.data[CONF_DESTINATION]
name = name or f"{DEFAULT_NAME}: {origin} -> {destination}"
name = config_entry.data[CONF_NAME]
if not await hass.async_add_executor_job(
is_valid_config_entry, hass, _LOGGER, api_key, origin, destination

View file

@ -5,6 +5,7 @@
"user": {
"description": "When specifying the origin and destination, you can supply one or more locations separated by the pipe character, in the form of an address, latitude/longitude coordinates, or a Google place ID. When specifying the location using a Google place ID, the ID must be prefixed with `place_id:`.",
"data": {
"name": "[%key:common::config_flow::data::name%]",
"api_key": "[%key:common::config_flow::data::api_key%]",
"origin": "Origin",
"destination": "Destination"

View file

@ -47,8 +47,9 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == f"{DEFAULT_NAME}: location1 -> location2"
assert result2["title"] == DEFAULT_NAME
assert result2["data"] == {
CONF_NAME: DEFAULT_NAME,
CONF_API_KEY: "api_key",
CONF_ORIGIN: "location1",
CONF_DESTINATION: "location2",
@ -281,6 +282,7 @@ async def test_import_flow(hass, validate_config_entry, bypass_update):
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.data == {
CONF_NAME: "test_name",
CONF_API_KEY: "api_key",
CONF_ORIGIN: "location1",
CONF_DESTINATION: "location2",